ImportError: No module named Foundation
Asked Answered
A

10

25

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.

Appear answered 23/10, 2009 at 16:46 Comment(2)
is the Foundation module something specific to the Mac platform?Caton
@Caton yes, it is.Appear
A
6

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.

Appear answered 23/10, 2009 at 23:3 Comment(1)
If it helps, I got the same error when executing my script with just "python"... but if I execute it with /usr/bin/python, then it works (without having to export any additional paths).Fiance
A
31

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.

Autoplasty answered 4/8, 2013 at 13:35 Comment(0)
A
6

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.

Appear answered 23/10, 2009 at 23:3 Comment(1)
If it helps, I got the same error when executing my script with just "python"... but if I execute it with /usr/bin/python, then it works (without having to export any additional paths).Fiance
P
5

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)

Portulaca answered 24/7, 2010 at 16:33 Comment(0)
L
4

for python 2.7

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

Lejeune answered 30/3, 2014 at 18:45 Comment(0)
B
3

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.

Barthelemy answered 23/10, 2009 at 16:50 Comment(1)
Thank you for this. It is useful to know how to edit the Python Path. Unfortunately, though, I have no idea what path I should be adding in order to incorporate Foundation.Appear
P
2

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

Piracy answered 23/10, 2009 at 17:0 Comment(1)
Sorry, I ran this and I did get output. I just don't understand what I am supposed to be looking for.Appear
I
2

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

Incalescent answered 27/8, 2017 at 12:44 Comment(0)
E
1
  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/

Epidemiology answered 10/6, 2020 at 14:42 Comment(0)
W
1

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
Weksler answered 23/8, 2022 at 13:40 Comment(0)
U
0

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.

Unlearned answered 14/6, 2022 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.