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.
-
Possible duplicate of Importing files from different folderR4444– R44442019年05月02日 18:55:43 +00:00Commented May 2, 2019 at 18:55
1 Answer 1
Yo need to export PYTHONPATH in your .bashrc:
export PYTHONPATH
Exporting enables children processes to inherit the environment variable.