Permanently remove something from python sys.path

Using Mac OSX 10.8.5 and Python 2.7.8. Python was installed using MacPorts and I have confirmed that the MacPorts installation is currently the one I am working with. I'm having trouble importing certain packages to install MacPorts, especially when one of the installed packages conflicts with older versions used by System Python. As an example, I've verified that a system-installed package in my sys.path is causing problems importing the 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 in normal ways, so I don't know how this is added in sys.path. I would like to permanently block the import of things from this path.

What I've already tried:

I read about site.py about the answers here What sets sys.path with Python and when? However, when I try to solve the problems in this question, they don't work. I tried to manually edit the site.py file and add a statement sys.path = filter (lambda a: not a.startswith('/System'), sys.path)

to main (). However, the erroneous path still appears even after posting the instruction on the .py site

The wrong path comes from the directory used by my computer OS, so deleting the numpy path in / System / Library is not an option. Is there a way to fix my problem keeping files under "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python" from being automatically added to sys.path?

+3


source to share


1 answer


It turns out the solution suggested by damirv in What sets sys.path with Python, and when? really works. I applied his suggestion incorrectly on my first try. You need to find the correct site.py that the working version of python is using and place it sys.path = filter (lambda a: not a.startswith('/System'), sys.path)

at the very end of the main () function. For my MacPorts Python installation, this path was /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py.



Earlier when trying this solution, I mistakenly added a line of code to the Python installed system in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/site.py which clearly did nothing to fix my import when running the installed version of MacPorts.

+1


source







All Articles