Add a directory to the python path in Linux

To add a directory to the python path in Linux you can add the path to the startup script in .profile or .bashrc (dependig on which shell you are using) .These files will be found in your home directory. So open up a terminal (Ctrl+Alt+T) and type pwd to make sure you are in your home directory. Then type:

cp .profile .profile_old

This will make a copy of the file you are about to modify so if you mess it up you can revert to the old one.(always a good thing to do before modifying a config file). Now add the following to the end of the file and save and exit.

export PYTHONPATH=$PYTHONPATH:/home/user/directory

Where /home/user/directory is the path of the directory you want to add. This file (.profile or bashrc) depending on the shell you use is run every time you log on to the system so the path will be added the next time you log on. So log out of the system and log back in and you can check to see if the path has been added by using the following commands in the idel interpreter:

import sys, pprint

pprint.pprint(sys.path)

 

 

Tags: 
Programming: