导入错误:没有名为 Foundation 的模块
我正在尝试按照“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我也有同样的问题。我的原因是我认为是通过使用 homebrew 安装我自己的 Python 来进行修改的。
因为我担心混合 python 版本,而不是如上所述创建链接,所以我使用以下命令安装了一个新的 pyobjc:
出于兴趣,来自 (http://pythonhosted.org/pyobjc/)
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:
For interest, from (http://pythonhosted.org/pyobjc/)
好吧,事实证明,修改 mjv 的答案后,我可以通过在
执行
./project-tool.py
行之前输入来使其工作。我仍然觉得我必须这样做很荒谬,如果有人能明白为什么,我会很高兴知道。这样做也使生产
线正常工作。
Okay, it turned out that, amending mjv's answer, I was able to get it working by typing
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
line working.
这是因为 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)
对于 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/"
两种情况之一:
Python 在 PythonPath 中查找模块
请参阅此 SO 问题,了解有关如何创建 Python 路径 等的更多详细信息。
One of two things:
Python looks for modules in the PythonPath
See this SO question for more details on how Python Path is created etc.
运行 python -v 来跟踪导入语句,这也适用于交互模式。
Run
python -v
to trace import statements, this work for interactive mode too.我可以通过运行
/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
删除你的 python 或删除 site-packages/Foundation | site-packages/foundation
pip3 install pyobjc
名称 Foundation 与 冲突https://pypi.org/project/foundation/
remove your python or remove site-packages/Foundation | site-packages/foundation
pip3 install pyobjc
the name Foundation is in conflict with https://pypi.org/project/foundation/
在另一条评论中看到它提到,由于通过自制程序安装Python,我也遇到了这个问题。我的 pyobjc 安装最终会转到 Python 自制程序安装,但我的 pythonpath 链接到与 macOS 捆绑在一起的 Python,因此存在很大的脱节,并且我没有运气将 pythonpath 重新路由到.zshrc 或 .zprofile。
最后,这些步骤解决了问题:
brew uninstall python
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:
brew uninstall python
pip3 install -U pyobjc
我在
/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 theAppKit
folder. After renaming it toFoundation
(with uppercase F), the import worked. The Filesystem is not case-sensitive but it seems some part of theimport
implementation is.