0

I am trying to import a python module from a particular directory on my system, but cannot get this working. Here is what I have tried:

1) Created a test file at /home/karnivaurus/foo.py which contains just the line print('Hello world')

2) Edited my .bashrc file to contain the line PYTHONPATH="$PYTHONPATH:$HOME"

3) Tested this by opening a new terminal and running echo $PYTHONPATH, which prints out ":/home/karnivaurus"

4) Started a python interpreter in the terminal by running python

5) Importing my test file by running import foo

However, this just returns the following error:

ImportError: No module named foo

What am I doing wrong?


Edit:

I have tried printing sys.path from the interpreter, and it prints out a number of directories, but does not print out /home/karnivaurus. So it seems that sys.path is not being updated by PYTHONPATH.

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • Possible duplicate of [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – R4444 May 02 '19 at 18:55

1 Answers1

2

Yo need to export PYTHONPATH in your .bashrc:

export PYTHONPATH

Exporting enables children processes to inherit the environment variable.

user803422
  • 2,636
  • 2
  • 18
  • 36