Florian Braun Florian Braun - 3 months ago 19
Linux Question

PYTHONPATH not included in sys.path on Ubuntu 16.04

After struggling with this for quite a few hours I am at my wit's end.

I want to include the Python modules from my project into the PYTHONPATH, so that the Python interpreter can resolve them and make them available for

import
.

My project folder looks like this:

my_project/
module1/
__init__.py
module1.py
module2/
__init__.py
module2.py


I've exported PYTHONPATH in
/etc/bash.bashrc
like this:

PYTHONPATH="${PYTHONPATH}:/home/john/my_project/"


After restarting my shell I can echo it:

$ echo $PYTHONPATH
:/home/john/my_project/


Then I fire up a Python command line and look at what
sys.path
became:

$ source /home/john/my_env/bin/activate
(my_env)$ python3
>>> import os
>>> sys.path
['', '/home/john/my_env/lib/python35.zip', '/home/john/my_env/lib/python3.5', '/home/john/my_env/lib/python3.5/plat-x86_64-linux-gnu', '/home/john/my_env/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/home/john/my_env/lib/python3.5/site-packages']


As you can clearly see,
/home/john/my_project/
wasn't included and
import module1
will fail.

The last thing I can think of is that Python can't see my PYTHONPATH variable, hence not adding it's content to
sys.path
.

Do you guys find my mistake?

Thanks in advance.

Answer

As you can see from here:

Use export in bash to set variables for the current shell and for all processes spawned from the current shell. So you should use:

export PYTHONPATH="${PYTHONPATH}:/home/john/my_project/"