Ubuntu add directory to Python path
Asked Answered
I

3

14

I want to run third part tool written in python on my ubuntu machine (corgy tool).

However I don't know how to add additional modules to Python path.

cat doc/download.rst         
There is currently no setup.py, so you need to manually add
the download directory to your PYTHON_PATH environment variable.

How can I add directory to PYTHON_PATH?

I have tried:
export PYTHON_PATH=/home/user/directory:$PYTHON_PATH && source .bashrc
export PATH=/home/user/directory:$PATH && source .bashrc

python
import sys
sys.path.append("/home/user/directory/")

But when I try to run this tool I get:

Traceback (most recent call last):
File "examples/dotbracket_to_bulge_graph.py", line 4, in <module>
import corgy.graph.bulge_graph as cgb
ImportError: No module named corgy.graph.bulge_graph
Interrex answered 4/6, 2013 at 8:29 Comment(0)
I
14

Create a .bash_profile in your home directory. Then, add the line

PYTHONPATH=$PYTHONPATH:new_dir
EXPORT $PYTHONPATH

Or even better:

if [ -d "new_dir" ] ; then
  PYTHONPATH="$PYTHONPATH:new_dir"
fi
EXPORT $PYTHONPATH

The .bash_profile properties are loaded every time you log in.

The source command is useful if you don't want to log in again.

Indifference answered 4/6, 2013 at 8:34 Comment(5)
could you explain how this is different from the export call?Lyricist
@Lyricist export just sets the variable for your current session. This should also work but only until you log out. And sourceing .bashrc of course makes no sense.Folderol
And, what is more important, you have to use export when setting variables in .bash_profile.Folderol
There is a nice explanation in https://mcmap.net/q/13196/-what-39-s-the-difference-between-bashrc-bash_profile-and-environment/1983854 . I did not know it is necessary to EXPORT in bash_profile. I update my answer accordingly.Indifference
that's my point. If it doesn't work with a simple export, i don't see how adding it the bash_profile helps. unless the script spawns a new sessionLyricist
E
5

@fedorqui's answer above was almost good for me, but there is at least one mistake (I am not sure about the export statement in all caps, I am a complete newbie). There should not be a $ sign preceding PYTHONPATH in the export statement. So the options would be:

Create a .bash_profile in your home directory. Then, add the line

PYTHONPATH=$PYTHONPATH:new_dir
export PYTHONPATH

Or even better:

if [ -d "new_dir" ] ; then
  PYTHONPATH="$PYTHONPATH:new_dir"
fi
export PYTHONPATH
Essay answered 9/2, 2021 at 12:13 Comment(0)
E
0
sudo ln -s /your/path/to/python /usr/local/bin/python
Examinee answered 27/12, 2023 at 13:42 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Revolutionary

© 2022 - 2024 — McMap. All rights reserved.