System-wide module path

Zachary Ware zachary.ware+pylist at gmail.com
Mon Oct 24 23:42:54 EDT 2016


On Mon, Oct 24, 2016 at 6:49 PM, Rob Gaddi
<rgaddi at highlandtechnology.invalid> wrote:
> On a Linux (Ubuntu) system, with no concerns for Python < 3.4, how do I
>   a) Add a directory to the system-wide (rather than per-user) module
> path?

This is the trickier part.  There are a couple of ways to do it, but
which is better is a matter of some debate (at least in my head).  The
first option is to stick a `.pth` file in the regular site-packages
directory that points to /usr/highland/lib/python3.4/site-packages.
Another option is to set PYTHONPATH; this is one of the very few
exceptions to the rule that you never want to permanently set
PYTHONPATH.  The main differences between the two options are which
end of sys.path the directory is added to, and whether it's added when
the -S interpreter option is used (say, by system utilities).  The
`.pth` option is cleaner and safer, but `.pth` files just feel icky.

>   b) Tell pip that I would like to install a given module to there.

Use the --prefix option: `pip install --prefix /usr/highland/
alembic`.  This tells pip to install the package(s) as though Python
was installed at /usr/highland/, so libraries go in
/usr/highland/lib/python3.4/site-packages and executable stubs go in
/usr/highland/bin/.

Hope this helps,
-- 
Zach



More information about the Python-list mailing list