PYTHONPATH on Linux

user2580401 picture user2580401 · Aug 15, 2013 · Viewed 295.1k times · Source

I'm novice in this, and I have started learning Python, but I have some questions that I'm not be able to understand,

  1. What exactly is the PYTHONPATH (on Ubuntu)? Is it a folder?
  2. Is Python provided by default on Ubuntu, or does it have to be installed explicitly?
  3. Where is the folder in which all modules are (I have a lot folders called python_)?
  4. If I wish a new module to work when I'm programming (such as pyopengl) where should I go to introduce all the folders I've got in the folder downloaded?
  5. Coming back from the PYTHONPATH issue, how do I configure the PYTHONPATH in order to start working on my new module?

Answer

mgilson picture mgilson · Aug 15, 2013

1) PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:

# make python look in the foo subdirectory of your home directory for
# modules and packages 
export PYTHONPATH=${PYTHONPATH}:${HOME}/foo 

Here I use the sh syntax. For other shells (e.g. csh,tcsh), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).

2) Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.

3) The folder where your modules live is dependent on PYTHONPATH and where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the command ls -- where does ls live? /usr/bin? /bin? 99% of the time, you don't need to care -- Just use ls and be happy that it lives somewhere on your PATH so the shell can find it.

4) I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.

5) Configure PYTHONPATH to include the directory where your module resides and python will be able to find your module.