ImportError: No module named Foundation

2019-01-17 10:47发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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)



回答4:

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.



回答5:

for python 2.7

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



回答6:

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



回答7:

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