prevent python from loading a pth file
Asked Answered
F

2

3

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.

Faultfinder answered 7/9, 2014 at 13:32 Comment(0)
F
3

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!

Faultfinder answered 7/9, 2014 at 21:20 Comment(0)
C
-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.

Cryptoclastic answered 7/9, 2014 at 13:58 Comment(2)
I never said I wanted to delete it. Regarding search locations - this is not what the documentation of site says - do you have references? And if you say there's no way to ignore said pth file - is there one to remove path from sys.path before load?Faultfinder
And as a side note, and respectfully, answering with "you can't, but I suggest doing what you specifically said you don't want to do", is not very productive.Faultfinder

© 2022 - 2024 — McMap. All rights reserved.