best way to ensure './' is at beginning of sys.path?

Steve D'Aprano steve+python at pearwood.info
Fri Feb 3 20:16:21 EST 2017


On Sat, 4 Feb 2017 03:06 am, Neal Becker wrote:

> I want to make sure any modules I build in the current directory overide
> any
> others.  To do this, I'd like sys.path to always have './' at the
> beginning.
> 
> What's the best way to ensure this is always true whenever I run python3?


For some definition of "always"... 

I don't know about "best", but you can do this:


1. In your .bashrc file, or equivalent, set the environment 
   variable PYTHONPATH:

export PYTHONPATH='./;$PYTHONPATH'

* * * 

If you need this to be site-wide, rather than per person, another
possibility, untested, would be to get your site administrator to create a
sitecustomize.py file. Suppose your Python is installed in 

/usr/local/lib/python3.5/

Get your site admin to create the following file:

/usr/local/lib/python3.5/site-packages/sitecustomize.py

containing the following code:

import sys
if './' not in sys.path:
    sys.path.insert(0, './')

* * * * 

Alternatively, a per-user solution is to create your own usercustomize.py
file, containing the same code as above. 

By default, the per-user site packages directory will be:

# Unix:
~/.local/lib/pythonX.Y/site-packages

# Mac:
~/Library/Python/X.Y/lib/python/site-packages

# Windows:
%APPDATA%\Python\PythonXY\site-packages

where X Y are the major and minor version numbers, e.g. 3 5.



See the documentation for the site module for more detail:

https://docs.python.org/3/library/site.html




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list