Reputation: 1875
Utilizing Mac OSX 10.8.5 and Python 2.7.8. Python was installed using MacPorts and I've verified that the MacPorts install is currently the one I'm working off of. I have problems importing certain packages to my MacPorts install, especially when one of the installed packages conflicts with an older versions utilized by the System Python. As an example, I've verified that a System installed package in my sys.path causing problems importing a current version of Numpy.
python
>>> import numpy
>>> numpy.__version__
'1.6.1' #Bad Version
python
>>> import sys
>>> sys.path.remove('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python')
>>> import numpy
>>> numpy.__version__
'1.9.0' #Good Version
The problem is that '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python' is never explicitly added to $PYTHONPATH or $PATH by conventional means so I do not know how it is being added to sys.path. I would like to permanently block importing things from this path.
What I've already tried:
I read about site.py on the answers here What sets up sys.path with Python, and when? However when I attempt the solutions in the question, they do not work. I tried manually editing site.py and adding the statement sys.path = filter (lambda a: not a.startswith('/System'), sys.path)
to main() function. However the erroneous path still appears even after putting the statement in site.py
The erroneous path comes from a directory utilized by my computer's OS, so deleting the numpy path under /System/Library is not an option. Is there any way I can fix my problem, keeping files under '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python' from automatically being added to sys.path?
Upvotes: 4
Views: 9671
Reputation: 1875
It turns out that the solution proposed by damirv in What sets up sys.path with Python, and when? actually does work. I implemented his suggestion incorrectly in my first attempt. One must find the correct site.py being utilized by the working python version and put sys.path = filter (lambda a: not a.startswith('/System'), sys.path)
at the very end of the main() function. For my MacPorts Python installation, that path was /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py.
Earlier when attempting this solution I mistakenly added the line of code to the system installed Python at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/site.py, which obviously did nothing to fix my imports when starting the MacPorts installed version.
Upvotes: 1