Using pyobjc imports in PyDev in Eclipse on Mac OS X

烈酒焚心 提交于 2020-01-03 05:19:06

问题


I've installed and configured PyDev version 1.6.5.2011020317 inside Eclipse, running on Mac OS X 10.6.6:

Version: Helios Service Release 1 Build id: 20100917-0705

I used 'Auto Config' to set up my Python interpreter: it correctly found /usr/bin/python (which is Python version 2.6.1) and added various system folders to the PYTHONPATH, including /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC. Now that path is the correct path to the Foundation module in OS X, as evinced by the command-line interpreter:

$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Foundation
>>> Foundation.__path__
['/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/Foundation']

So why does PyDev complain about "Undefined variable from import: NSDictionary" on this class:

import Foundation

class MyClass(object):

    def __init__(self, projectPath):
        '''
        Constructor
        '''
        self.projectDict = Foundation.NSDictionary.dictionaryWithContentsOfFile_(projectPath)

when I can use that class without any problem from the command-line interpreter?

Update: OK, I found out why it complains, which is that the Foundation module is using ScriptingBridge to dynamically generate the classes - presumably pydev isn't actually importing the module to see what classes are inside, it's just looking for .py[c] files. So let my question not be "why does this happen", but "what do I do to fix it"?


回答1:


Why does this happen?: PyDev has no support for parsing the PyObjC scripting bridge metadata, and therefore has no way of introspecting / extracting symbols for many of the PyObjC classes.

What to do to fix it: In the PyDev source code there are several Python scripts which handle discovery of this metadata. The scripts are executed by Eclipse using the configured interpreter, and they return strings which are used to configure the interpreter, fill in completion lists, show usage tips, etc.

The scripts which seem relevant to your needs are:

  • interpreterInfo.py - called to obtain the list of directories and other default top-level imports for a given interpreter.
  • importsTipper.py - generates usage tips for a given symbol.
  • pycompletion.py - generates a list of completions for a given symbol.

Examples of calls to the above scripts using the system interpreter:

% /usr/bin/python interpreterInfo.py | grep PyObjC
|/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjCINS_PATH

Generate completions for a module:

>>> import pycompletion
>>> print pycompletion.GetImports('os')
@@COMPLETIONS(/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py,(EX_CANTCREAT, 3),(EX_CONFIG, 3),(EX_DATAERR, 3), ....

It seems possible to create a simple macobjc.py library with routines which detect and read the PyObjC.bridgesupport file(s). The PyDev scripts could be modified to call into this library in order to return the list of valid completions for those classes. You'll need to point Eclipse to a local copy of the PyDev source in order to develop and test your patches against these files. Once you're done you can send it upstream; I have to believe the PyDev folks would accept a well-written patch to support PyObjC completions.



来源:https://stackoverflow.com/questions/5234647/using-pyobjc-imports-in-pydev-in-eclipse-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!