7

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.

Florian Braun
  • 355
  • 3
  • 11
  • Nicely presented, thus the upvote. Alas, a duplicate, thus the close. The keyword you missed in your search was probably `virtualenv`. – msw Sep 10 '16 at 05:49

1 Answers1

10

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/"

xrisk
  • 3,790
  • 22
  • 45