Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent python from loading a pth file

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 a x.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 run python -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.

like image 900
Korem Avatar asked Dec 05 '25 02:12

Korem


1 Answers

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 here

  • According 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 the pth 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!

like image 125
Korem Avatar answered Dec 06 '25 15:12

Korem