导入错误:没有名为 Foundation 的模块

发布于 2024-08-08 22:40:25 字数 1713 浏览 2 评论 0原文

我正在尝试按照“PyObjCdevelopment with Xcode 3.2”的已接受答案的说明进行操作。我将在这里重新发布它们,因为我没有足够的代表来评论实际问题:


这是我为让 PyObjC 在 Snow Leopard 中工作所做的工作:

  • 使用 Finder,我转到 Go >;连接到服务器... 并连接到 http:// /svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/ 作为访客。

  • 然后,我在本地系统上创建了一个名为 Xcode 的文件夹,位置为 ~Library/Application Support/Developer/Shared/Xcode/。 (您可能已经有了这个文件夹,但我还没有为自己自定义任何内容)。

  • 我将File Templates文件夹从红豆服务器复制到我的新Xcode文件夹中。

  • 已将Project Templates文件夹复制到其他位置,例如桌面。

  • 使用终端,导航到桌面上的临时项目模板文件夹并运行此命令来“构建”模板。:

    使用终端

$ cd ~/桌面/项目\模板/

$ ./project-tool.py -k -v --template ~/Desktop/Project\ Templates/Cocoa-Python\ Application/CocoaApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Application ~/Library/Application\支持/开发者/共享/Xcode/Project\ Templates/Cocoa-Python\ Application


当我尝试运行以 ./project-tool.py 开头的行时,我在终端中出现以下错误:

Traceback (most recent call last):  
  File "./project-tool.py", line 22, in <module>  
     from Foundation import NSDictionary  
 ImportError: No module named Foundation

我正在运行 Snow Leopard 并已安装 Xcode 3.2.1,并且已了解到该模块应该已安装并正常工作。我读到您可以通过运行 >>> 来测试 PyObjC 模块是否正常工作在 Python 命令行中导入 objc。当我运行这个时,我得到:

>>> import objc
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
ImportError: No module named objc  

有人能帮我消除这个错误吗?看起来我应该能够通过我的 Snow Leopard 安装自动完成所有这些工作,但我不能。

I am trying to follow the instructions for the accepted answer to "PyObjC development with Xcode 3.2". I will repost them here since I don't have enough rep to comment on the actual question:


Here's what I have done to get PyObjC working in Snow Leopard:

  • Using the Finder, I went to Go > Connect to Server... and connected to http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/ as a guest.

  • I then made a folder called Xcode on my local system at ~Library/Application Support/Developer/Shared/Xcode/. (You may already have this folder, but I hadn't customized anything for myself yet).

  • I copied the File Templates folder from the red-bean server into my new Xcode folder.

  • Copied the Project Templates folder to some other place, for example, the Desktop.

  • Using the Terminal, navigated to the temporary Project Templates folder on my Desktop and ran this command to "build" the template.:

$ cd ~/Desktop/Project\ Templates/

$ ./project-tool.py -k -v --template ~/Desktop/Project\ Templates/Cocoa-Python\ Application/CocoaApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Application ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/Cocoa-Python\ Application


When I try to run the line that starts with ./project-tool.py, I get the following error in Terminal:

Traceback (most recent call last):  
  File "./project-tool.py", line 22, in <module>  
     from Foundation import NSDictionary  
 ImportError: No module named Foundation

I am running Snow Leopard and have installed Xcode 3.2.1 and have read that this module should already be installed and working. I've read that you can test if the PyObjC modules are working by running >>> import objc in the Python command-line. When I run this, I get:

>>> import objc
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
ImportError: No module named objc  

Could anyone help me dispel this error? It seems like I should be able to do all of this automatically with my Snow Leopard installation, but I can't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

岁吢 2024-08-15 22:40:25

我也有同样的问题。我的原因是我认为是通过使用 homebrew 安装我自己的 Python 来进行修改的。

因为我担心混合 python 版本,而不是如上所述创建链接,所以我使用以下命令安装了一个新的 pyobjc:

$ pip install pyobjc

出于兴趣,来自 (http://pythonhosted.org/pyobjc/)

PyObjC 项目旨在提供 Python 和 Objective-C 编程语言之间的桥梁。

I had the same problem. Mine was caused I think by using homebrew to install my own Python to tinker with.

Because I was worried about mixing python versions, rather than creating the link as described above, I installed a new pyobjc using:

$ pip install pyobjc

For interest, from (http://pythonhosted.org/pyobjc/)

The PyObjC project aims to provide a bridge between the Python and Objective-C programming languages.

李不 2024-08-15 22:40:25

好吧,事实证明,修改 mjv 的答案后,我可以通过在

export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PyObjC/"

执行 ./project-tool.py 行之前输入来使其工作。我仍然觉得我必须这样做很荒谬,如果有人能明白为什么,我会很高兴知道。

这样做也使生产

>>> import objc

线正常工作。

Okay, it turned out that, amending mjv's answer, I was able to get it working by typing

export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PyObjC/"

before executing the ./project-tool.py line. I still find it ridiculous that I had to do this and if anyone can see why, I would be delighted to know.

Doing this also got the

>>> import objc

line working.

新一帅帅 2024-08-15 22:40:25

这是因为 PyObjC 在那里:
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC

编辑:
我找到了如何使“导入 objc”工作,只需:
export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/"

它将把所有目录添加到 python 路径 (sys.path)

It's because PyObjC is there :
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC

Edit :
I found how to make "import objc" work, just :
export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/"

It will add all the directories to the python path (sys.path)

爱你不解释 2024-08-15 22:40:25

对于 python 2.7

导出 PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/"

for python 2.7

export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/"

一绘本一梦想 2024-08-15 22:40:25

两种情况之一:

  • Fundation 模块不存在
  • 或者 Python 解释器不知道何时找到此文件

Python 在 PythonPath 中查找模块

请参阅此 SO 问题,了解有关如何创建 Python 路径 等的更多详细信息。

One of two things:

  • Either the Fundation module doesn't exists
  • Or Python interpretor doesn't know when to find this file

Python looks for modules in the PythonPath

See this SO question for more details on how Python Path is created etc.

半世蒼涼 2024-08-15 22:40:25

运行 python -v 来跟踪导入语句,这也适用于交互模式。

Run python -v to trace import statements, this work for interactive mode too.

仅此而已 2024-08-15 22:40:25

我可以通过运行 /usr/bin/python file-to-run.py 在我的 OSX 上使用 Foundation 访问 Python 安装

I could access a Python installation with Foundation on my OSX by running /usr/bin/python file-to-run.py

思慕 2024-08-15 22:40:25
  1. 删除你的 python 或删除 site-packages/Foundation | site-packages/foundation

  2. pip3 install pyobjc

名称 Foundation 与 冲突https://pypi.org/project/foundation/

  1. remove your python or remove site-packages/Foundation | site-packages/foundation

  2. pip3 install pyobjc

the name Foundation is in conflict with https://pypi.org/project/foundation/

与君绝 2024-08-15 22:40:25

在另一条评论中看到它提到,由于通过自制程序安装Python,我也遇到了这个问题。我的 pyobjc 安装最终会转到 Python 自制程序安装,但我的 pythonpath 链接到与 macOS 捆绑在一起的 Python,因此存在很大的脱节,并且我没有运气将 pythonpath 重新路由到.zshrc 或 .zprofile。

最后,这些步骤解决了问题:

  1. brew uninstall python
  2. pip3 install -U pyobjc

Saw it mentioned in another comment and I too ran into this problem due to installing Python via homebrew. My pyobjc installation wound up going to the Python homebrew installation, yet my pythonpath was linked to the Python that comes bundled with macOS, so there was this big disconnect and I had no luck getting pythonpath re-routed in .zshrc or .zprofile.

In the end, these steps resolved the issue:

  1. brew uninstall python
  2. pip3 install -U pyobjc
物价感观 2024-08-15 22:40:25

我在 /usr/local/lib/python3.9/site-packages/ 中的 AppKit 文件夹旁边找到了 foundation 文件夹。将其重命名Foundation(使用大写F)后,导入成功了。文件系统不区分大小写,但似乎 import 实现的某些部分是区分大小写的。

I found the foundation folder in /usr/local/lib/python3.9/site-packages/ next to the AppKit folder. After renaming it to Foundation (with uppercase F), the import worked. The Filesystem is not case-sensitive but it seems some part of the import implementation is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文