开发者

ImportError: No module named Foundation

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,开发者_StackOverflow社区 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.


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.


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.


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)


for python 2.7

export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/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.


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


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


  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/


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜