Prevent python pth file loading

My situation is this:

  • I have a locally installed version of python. There is also a global, poorly installed one that I do not want to use. (I do not have admin priviliges).
  • There is an x.pth file in /usr/local/lib/site-packages containing the path to the incorrect numpy installation
  • My PYTHONPATH has none of these paths. However, some administrator-created script adds /usr/local and /usr/local/bin to my PATH (this is an assumption, not a known fact).
  • This causes the path with the sys.path error to be added to my sys.path . When I run python -S , it does not exist.
  • site.PREFIXES does not include /usr/local . I do not know why the above pth file is loaded.
  • I tried adding my own pth file to the local site-packages dir directory by doing import sys; sys.path.remove('pth/to/faulty/numpy') import sys; sys.path.remove('pth/to/faulty/numpy') This fails because when this pth file is loaded, the faulty path is not yet in sys.path.

Is there a way to disable loading the specified pth file or remove the path from sys.path before python loads?

I tried to configure virtualenv and this does not suit my current situation.

+2
source share
2 answers

I finally managed to solve this problem. My site package library had an easy_install.pth file, which for some reason contained an erroneous numpy, and not an x.pth file on /usr/local/lib/site-packages .

After a lot of time that I spent on this, I will share some of the things I learned if anyone else ever comes here:

  • If you have locally installed python installed, it will not search in '/ usr / local / lib' by default. If you want to know where he is looking, run:

     import site print site.PREFIXES 

    python is looking for paths here + lib/python2.X/site-packages . Described here

  • According to these docs you can play with your sys.path. If you add the usercustomize module to your local sites ( import site; site.getusersitepackages() ), it will import site; site.getusersitepackages() . However, it took me a while to figure out - this happens after python processes the pth files located on your sites and before it processes their AGAIN. If I add a print statement to this method (lib / site.py addeditedir), 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 
  • As soon as I had a pth file that I missed in the site packages, I could not fix it with usercustomize , since this pth file was able to run one more time after that!

+2
source share

Unable to delete pth file. Standard python will look for pth files in /usr/lib and /usr/local/lib .

You can create isolated python via viartualenv though.

-one
source share

All Articles