在Mac OS X中的Eclipse中,在PyDev中使用pyobjc导入 [英] Using pyobjc imports in PyDev in Eclipse on Mac OS X

查看:159
本文介绍了在Mac OS X中的Eclipse中,在PyDev中使用pyobjc导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Eclipse中安装并配置了PyDev 1.6.5.2011020317版本,在Mac OS X 10.6.6上运行:


版本: Helios Service Release 1
Build id:20100917-0705


我用'Auto Config'来设置我的Python解释器:它正确地发现 / usr / bin / python (这是Python 2.6.1版),并将各种系统文件夹添加到 PYTHONPATH ,包括 /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC 。现在,这条路径是OS X中 Foundation 模块的正确路径,由命令行解释器证明:

  $ python 
Python 2.6.1(r261:67515,2010年6月24日,21:47:49)
[GCC 4.2 .1(Apple Inc. build 5646)]在darwin
有关详细信息,请输入help,copyright,credits或license。
>>> import Foundation
>>>基础.__路径_
['/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/Foundation']
  import Foundation 

class MyClass(object):

def __init __(self,projectPath):
'''
构造函数
'''
self.projectDict = Foundation.NSDictionary.dictionaryWithContentsOfFile_(projectPath)

当我可以使用该类没有任何问题从命令行解释器?



更新:好的,我发现了为什么它抱怨,这是 Foundation 模块正在使用ScriptingBridge动态生成类 - 大概pydev实际上并不是导入模块以查看哪些类在里面,它只是寻找 .py [c] 文件。所以让我的问题不是为什么会发生这种情况,而是我该如何解决它?

解决方案

strong>为什么会发生这种情况?:PyDev不支持解析PyObjC脚本桥元数据,因此无法为许多PyObjC类进行内省/提取符号。



修复方法:在PyDev 源代码< a>有几个Python脚本处理这个元数据的发现。脚本由Eclipse使用配置的解释器执行,并返回用于配置解释器的字符串,填写完成列表,显示使用提示等。



与您的需求相似的脚本是:





上述脚本的调用示例u唱系统解释器:

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

为模块生成完成:

 >>> 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),....

似乎有可能创建一个简单的 macobjc.py 库,其中包含检测和读取PyObjC.bridgesupport文件的例程,可以修改PyDev脚本以调用该库,以返回这些类的有效完成列表,您需要将Eclipse指向PyDev源的本地副本,以便根据这些文件开发和测试补丁。完成后,您可以将其发送到上游;我必须相信PyDev的人会接受一个写得很好的补丁来支持PyObjC的完成。


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"?

解决方案

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.

这篇关于在Mac OS X中的Eclipse中,在PyDev中使用pyobjc导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆