Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu add directory to Python path

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
like image 416
pogibas Avatar asked Jun 04 '13 08:06

pogibas


2 Answers

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.

like image 172
fedorqui 'SO stop harming' Avatar answered Oct 25 '22 00:10

fedorqui 'SO stop harming'


@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
like image 45
Elena Carballido Avatar answered Oct 25 '22 02:10

Elena Carballido