Reputation: 77404
In my current system, all directories such as .local/lib/python2.7/site-packages
are under global admin control and are standardized across users.
However, I would like to add a collection of my own directories to be brought into the PYTHONPATH on startup, preferably in an easy-to-manage way like using a .pth
file within a secondary site directory.
Is there a way I can achieve this through normal Python channels, but without needing to make any kind of change to the standard locations where Python looks for path elements?
I'm specifically wondering if there is a way to just place a file like sitecustomize.py
or usercustomize.py
anywhere in a folder that is in the PYTHONPATH and expect that they will be automatically executed? (Rather than getting this behavior by placing such files within an admin directory like site-packages
, which is what I want to avoid.)
My goal is to place a text file, paths.pth
in some directory, like /home/my_configs/python_stuff/
and then just need to place that directory only in the PYTHONPATH via, e.g. a .bashrc
file. Then, at least, I only manually maintain one path to be added, and it's easier to version control or parameterize what gets loaded by changing what is in that .pth
file.
If the admin limitations weren't in place, I could trivially do this in site-packages
but I need a solution that works totally outside of that.
In /.bashrc
I have this:
export PYTHONPATH=/home/ems/python_paths/:$PYTHONPATH
In the directory /home/ems/python_paths/
I have just two files, sitecustomize.py
and paths.pth
(but I've also tried adding an __init__.py
there too, which (as expected) didn't help). These two files look like this:
sitecustomize.py
import site
site.addsitedir("/home/ems/python_paths/")
paths.pth
/home/ems/ws/Research/Projects/python-util/src/util/
/home/ems/ws/Research/Projects/python-efficacy/src/efficacy/
The contents of paths.pth
are just exactly what used to be directly exported in .bashrc
and, for instance, permits me to do from util.DataManager import DataManager
as a top-level import.
But after setting up the various files above, commenting out exports
to PYTHONPATH
in .bashrc
, and getting a fresh terminal, I see this:
ems@computer ~ $ python
Enthought Python Distribution -- www.enthought.com
Version: 7.3-2 (64-bit)
Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 11 2012, 17:52:16)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "credits", "demo" or "enthought" for more information.
Hello
>>> from util.DataManager import DataManager
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named util.DataManager
Upvotes: 3
Views: 2835
Reputation: 17158
There was PEP 370 specifically addressing the creation of per-user site-packages
directories, to deal with the situation where the user has no admin access to the system-wide site-packages
.
For example on Unix (including Mac OS), and assuming one is using Python 3.6, one can create the following directory and place .pth
files inside there
~/.local/lib/python3.6/site-packages
Upvotes: 1