1

I want to add a folder permanently to sys.path but it doesn't work.

On my Debian Stretch, when I execute :

$ echo $PYTHONPATH

it looks empty

In the same time, when I print the content of sys.path, there is 10 different paths which aren't the same than those contained into $PATH (and obviously in $PYTHONPATH). How can it be explained ?

(I had to create a virtualenv for a framework but I don't think I'm using it right now so it's probably not related but I'm not confortable with that concept so maybe I miss something)

Also I've tried to modify .bashrc to add a path to $PYTHONPATH so I've know the following line :

PYTHONPATH=$PYTHONPATH:~/bin 

After rebooting, it works, I mean $PYTHONPATH contains it but nothing change for sys.path.

Also, $ env | grep ^PYTHON returns nothing.

So, how sys.path is filled and how can I add permanently a folder to it ?

  • PYTHONPATH and PATH has nothing to do with each other. PYTHONPATH is where python looks for modules. sys.path is initialized by python and may be further modified by modules and the main script. What is the issue that you are experiencing? – Kusalananda May 29 '18 at 17:08
  • I know that there aren't related but, in the same time, I can't figure out where that list of folder comes from. I thought I was clear about the issue, I can't add a folder to sys.path by modifying $PYTHONPATH. – snoob dogg May 29 '18 at 17:17
  • Did you export PYTHONPATH? – Kusalananda May 29 '18 at 17:22
3

sys.path in Python is a "list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default." (taken from the Python online documentation).

For sys.path to be affected by PYTHONPATH, the latter has to be an environment variable. If it's not an environment variable but just a shell variable, it will not be visible by the python interpreter. You turn it into an environment variable by exporting it after setting its value, or at the same time, as in

export PYTHONPATH=/some/path:/goes/here
  • By the way, since my PYTHONPATH was empty of have you an idea, how does the other folders paths were determined ? – snoob dogg May 29 '18 at 18:53
  • @snoobdogg It's dependent on the installation of Python. For a slightly longer explanation, see here: stackoverflow.com/a/38403654/931027 – Kusalananda May 29 '18 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.