pyobjc
pyobjc copied to clipboard
Error Foundation module not found
I have been using the Foundation module in a tkinter Python project to change the application title in the OS X menubar.
from sys import platform
# Check if we're on OS X, first.
if platform == 'darwin':
from Foundation import NSBundle
bundle = NSBundle.mainBundle()
if bundle:
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
if info and info['CFBundleName'] == 'Python':
info['CFBundleName'] = "AnimalTrakker"
that works just fine under Python 3.7 using pyobjc 6.1 in the PyCharm development environment PyCharm 2021.2.3 (Professional Edition) under MacOS 10.15.7
I just started a new project under the same Pycharm development environment but using Python 3.8 and pyobjc 8.1 and now I get this error:
ModuleNotFoundError: No module named 'Foundation'
From StackOverflow I've been pointed to pyobjc as the issue although I remain unconvinced of that. So I am posting here in case it is something with the latest version of pyobjc that is causing the error.
However, I am very open to suggestions for other places to look to fix the issue.
The most likely problem is that PyObjC is installed in a different python installation or virtual environment than the one you use to run the script.
Is the package pyobjc-framework-Cocoa installed? Likewise for pyobjc-core.
This importing Foundation work from an interactive python shell?
Yes there are actually 2 different virtual environments PyObjC is installed in each of them at the version sshow above
Yes the packages pyobjc-framework-Cocoa and pyobjc-core are both there, 2 copies, 1 in each venv directory but different versions
No importing Foundation does not work from an interactive python shell as best as I can tell. I start up a python console and tried it but get the same error. OTOH I never use a console so I could easily be doing something wrong there too. I always run things out of PyCharm.
That's weird. Does 'import objc' work?
One thing you could try is to print sys.path before importing Foundation, and then check if the Foundation package is actually on sys.path (using a file browser or a script that does os.listdir on every directory on sys.path).
So I did this in a python console
import os.path
print (os.path)
and got this as a result
<module 'posixpath' from '/usr/local/bin/../../../Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/posixpath.py'>
That made no sense but I did go down into the venv and to the python 3.8 folder and the site packages folder and I see a folder foundation note: the name is all lower case. Inside that there is a file _Foundation.cpython-38-darwin.so
So I tried to use this command in my script instead thinking the error might be the case issue
from foundation import NSBundle
and now I get a new error
/Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/venv/bin/python /Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/AnimalTrakker_Registry.py
Traceback (most recent call last):
File "/Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/AnimalTrakker_Registry.py", line 57, in <module>
main()
File "/Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/AnimalTrakker_Registry.py", line 24, in main
from foundation import NSBundle
ImportError: cannot import name 'NSBundle' from 'foundation' (unknown location)
The lower-case name is wrong, renaming it to Foundation might work. But: this is an indication that something is wrong and may not be the only problem with file names. In particular, the __init__.py
in Foundation imports some other parts of PyObjC, some of which also have a name that starts with a capital letter.
How did you install PyObjC? Did you use PyPI or was PyObjC installed through a tool like homebrew?
I've checked the wheels on PyPI and those all contain a Foundation folder with a capital F.
It's probably useful to uninstall and reinstall pyobjc, assuming you're using PyPI and binary wheels.
I installed via PyCharm install package. The reason is it has to go into the venv of Pycharm and be kept separate from the other venv's and my standard mac os pythin install that is still a 2. version
I've never managed to install anything using homebrew.
And the statement " assuming you're using PyPI and binary wheels." also didn't make sense to me as I've never used that either.
up update I have uninstalled and reinstalled, Foundation came in properly but I still am getting an error but it's different.
File "/Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/AnimalTrakker_Registry.py", line 25, in main from PyObjCTools.FndCategories import NSBundle ImportError: cannot import name 'NSBundle' from 'PyObjCTools.FndCategories' (/Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/venv/lib/python3.8/site-packages/PyObjCTools/FndCategories.py)
That still looks like a problem with the pyobjc installation on your system. I honestly have no idea why it doesn't work for you.
Just FYI. I'm seeing the same issue with Python 3.9. Installed Python via Brew on macOS and then pip3 install pyobjc. See all frameworks installed including Cocoa but importing either Cocoa or Foundation into a test program fails with "ModuleNotFoundError: No module named 'Foundation'". Dumping list of know packages shows other modules in pyobjc set (i.e. CoreFoundation or AVFoundation) but not Foundation.
This is on Macos 12.3.1
@ronaldoussoren ronaldoussoren Any further thoughts on this? The issue appears to be that Foundation is not being picked up from the Python package. Other packages are seen correctly but Foundation is not recognised as a namespace. I have installed through pip and (after removal) built from source code. Neither appears to see the Foundation module.
I have no idea what's going on here and cannot reproduce this on my machines.
The correct name of the Python package is "Foundation", and that's the name included in the source distribution and binary wheels. For some reason this name is changed to "foundation" (lower case) on some machines.
@nycnewman : Your issue might be different. Have to checked that you installed PyObjC into the same Python installation as you're trying to use? Check "python3 -m "import sys; print(sys.prefix)".
In the list of installed packages ("pip3 list") you won't see Foundation, the Foundation library is part of the PyPI distribution "pyobjc-framework-Cocoa".
OK I've done an extensive set of tests. I cannot get it to work in any version of Python that uses the Intel installer. (I created a bunch of separate virtuel environments and then tested my program in those environments,
However, using the Python 3.9.8 Universal Version installer in a separate virtual environment using PyCharm to do all the installation of the packages I was able to get everything to work properly.
I am on a 2013 iMac running Catalina 10.15.7
Maybe that's aclue as to the issue? universal vs intel installers?
The interesting part is it seems to find other modules within the Coca package:
<module 'posixpath' from @./3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/posixpath.py'>
@./Frameworks/Python.framework/Versions/3.9
Traceback (most recent call last):
File "/Users/edwardnewman/src/pycreateuserpkg/test.py", line 15, in
This comes just from:
import os.path print (os.path)
import sys print (sys.prefix)
import AVFoundation
Looks like it finds AVFoundation but not Foundation.
Edward Newman CISO Digital Asset https://digitalasset.com/, creators of Daml https://daml.com/
On Apr 15, 2022, at 14:48, Oogie McGuire @.***> wrote:
OK I've done an extensive set of tests. I cannot get it to work in any version of Python that uses the Intel installer. (I created a bunch of separate virtuel environments and then tested my program in those environments,
However, using the Python 3.9.8 Universal Version installer in a separate virtual environment using PyCharm to do all the installation of the packages I was able to get everything to work properly.
I am on a 2013 iMac running Catalina 10.15.7
Maybe that's aclue as to the issue? universal vs intel installers?
— Reply to this email directly, view it on GitHub https://github.com/ronaldoussoren/pyobjc/issues/408#issuecomment-1100120896, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD6MV37SWE4FPOUZBMYSPIDVFFXTRANCNFSM5KBSJVCQ. You are receiving this because you were mentioned.
-- This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.digitalasset.com/emaildisclaimer.html http://www.digitalasset.com/emaildisclaimer.html. If you are not the intended recipient, please delete this message.
I've checked the release wheels once again and those contain uppercase names.
Anyone running in this: please try installing in a clean virtualenv, and also check that there are no other project that install a "foundation" library (with lower-case F) as Apple's filesystems are case-preserving but case-insensitive