Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .pth files

Tags:

python

I am trying to make a module discoverable on a system where I don't have write access to the global site-packages directory, and without changing the environment (PYTHONPATH). I have tried to place a .pth file in the same directory as a script I'm executing, but it seems to be ignored. E.g., I created a file extras.pth with the following content:

N:\PythonExtras\lib\site-packages 

But the following script, placed and run in the same directory, prints False.

import sys print r"N:\PythonExtras\lib\site-packages" in sys.paths 

The only directory in sys.path to which I have write access is the directory containing the script. Is there another (currently non-existent) directory where I could place extras.pth and have it be seen? Is there a better way to go about this?

I'm using python 2.7 on Windows. All .pth questions I could find here use the system module directories.

Edit: I've tracked down the Windows per-user installation directory, at %APPDATA%\Python\Python27\site-packages. I can place a module there and it will be imported, but if I put a .pth file there, it has no effect. Is this really not supposed to work, or am I doing something wrong?

like image 291
alexis Avatar asked Mar 04 '13 18:03

alexis


People also ask

What is Sitecustomize?

Uses for sitecustomize include extending the import path and enabling coverage, profiling, or other development tools. For example, this sitecustomize.py script extends the import path with a directory based on the current platform.

Where are python site packages?

there is a site package directory in a virtualenv. You can get the directory for site-specific modules inside/outside virtualenv using python -c "from distutils. sysconfig import get_python_lib; print(get_python_lib())" (it works on both Python 2 and 3 too). This should be in any basic tutorial on using python.

What is SYS path in Python?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.


1 Answers

As described in the documentation, PTH files are only processed if they are in the site-packages directory. (More precisely, they are processed if they are in a "site directory", but "site directory" itself is a setting global to the Python installation and does not depend on the current directory or the directory where the script resides.)

If the directory containing your script is on sys.path, you could create a sitecustomize.py in that directory. This will be loaded when Python starts up. Inside sitecustomize.py, you can do:

import site site.addsitedir('/some/dir/you/want/on/the/path') 

This will not only add that directory, but will add it as a "site directory", causing PTH files there to be processed. This is handy if you want to create your own personal site-packages-like-directory.

If you only need to add one or two directories to the path, you could do so more simply. Just create a tiny Python library that manipulates sys.path, and then import that library from your script. Something like:

# makepath.py import sys sys.path.append('/whatever/dir/you/want')  # script.py import makepath 

Edit: Again, according to the documentation, there is the possibility of a site-specific directory in %APPDATA%\Python\PythonXY\site-packages (on Windows). You could try that, if in fact you have write access to that (and not just to your script directory).

like image 178
BrenBarn Avatar answered Oct 05 '22 22:10

BrenBarn