问题
My situation is as follows:
- I have a locally installed version of python. There exists also a global one, badly installed, which I do not want to use. (I don't have admin priviliges).
- On
/usr/local/lib/site-packages
there is ax.pth
file containing a path to a faulty installation of numpy - My PYTHONPATH does not have any of those paths. However, some admin generated script adds
/usr/local
and/usr/local/bin
to my PATH (this is an assumption, not a known fact). - This somehow results in the faulty-numpy-path added to my
sys.path
. When I runpython -S
, it's not there. site.PREFIXES
does not include/usr/local
. I have no idea why the aforementioned pth file is loaded.- I tried adding a pth file of my own, to the local installation's site-packages dir, doing
import sys; sys.path.remove('pth/to/faulty/numpy')
This fails because when that pth file is loaded, the faulty path is not yet in sys.path.
Is there a way for me to disable the loading of said pth file, or remove the path from sys.path before python is loaded?
I've tried setting up virtualenv and it does not suit my current situation.
回答1:
I finally managed to solve this - my site-packages library had an easy_install.pth
file containing the faulty numpy for some reason, and not the x.pth file on /usr/local/lib/site-packages
.
After the major amount of time I spent on this, I'll share some of the stuff I've learned if someone else ever gets here:
If you have a locally installed python it will not search in '/usr/local/lib' by default. If you want to know where it searches, run:
import site print site.PREFIXES
python searches for the paths here +
lib/python2.X/site-packages
. It's described hereAccording to these docs, you can in fact play around with your sys.path. If you add
usercustomize
module to your local sitepackages (import site; site.getusersitepackages()
), it will be ran. However, and this took me time to realize - it happens after python processes thepth
files located at your site-packages, and before he processes them AGAIN. If I add a print statement to the method that does this (lib/site.py addsitedir), this is what it will print:/home/user/.local/lib/python2.7/site-packages /home/user/py/lib/python2.7/site-packages #This is where your code runs /home/user/py/lib/python2.7/site-packages/numpy-1.9.0-py2.7-linux-x86_64.egg /home/user/Develop/Python/myproject /home/user/lmfit-0.7.2 /home/user/py/lib/python2.7/site-packages #NOTE: this runs a second time
Once I had a pth file that I missed in site-packages, I couldn't fix it with a
usercustomize
, since that pth file got a chance to run one more time afterwards!
回答2:
There is no way to delete pth file.
Standard python will search for pth files in /usr/lib
and /usr/local/lib
.
You can create isolated python via viartualenv though.
来源:https://stackoverflow.com/questions/25710724/prevent-python-from-loading-a-pth-file