Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PYTHONPATH on Linux [closed]

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?
like image 949
user2580401 Avatar asked Aug 15 '13 06:08

user2580401


People also ask

How do I permanently set Pythonpath in Linux?

Type open . bash_profile. In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar. Save the file, restart the Terminal, and you're done.

How use Pythonpath Linux?

Setting Path at Unix/LinuxIn the csh shell − type setenv PATH "$PATH:/usr/local/bin/python3" and press Enter. In the bash shell (Linux) − type export PYTHONPATH=/usr/local/bin/python3. 4 and press Enter. In the sh or ksh shell − type PATH = "$PATH:/usr/local/bin/python3" and press Enter.

Where is the Python path in Linux?

One of the most important things to note when you are adding Path to Python in Unix or Linux is that, /usr/local/bin/python is the default path of the Python directory.


2 Answers

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.

like image 122
mgilson Avatar answered Sep 20 '22 21:09

mgilson


  1. PYTHONPATH is an environment variable
  2. Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
  3. /usr/lib/python2.7 on Ubuntu
  4. you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
  5. if you use pip or setuptools, then you don't need to set PYTHONPATH explicitly

If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.

like image 30
mpenkov Avatar answered Sep 19 '22 21:09

mpenkov