PYTHONPATHis 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}/fooHere I use the
shsyntax. 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).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.
The folder where your modules live is dependent on
PYTHONPATHand 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 commandls-- where doeslslive?/usr/bin?/bin? 99% of the time, you don't need to care -- Just uselsand be happy that it lives somewhere on yourPATHso the shell can find it.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.
Configure
PYTHONPATHto include the directory where your module resides and python will be able to find your module.
PYTHONPATHis 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}/fooHere I use the
shsyntax. 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).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.
The folder where your modules live is dependent on
PYTHONPATHand 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 commandls-- where doeslslive?/usr/bin?/bin? 99% of the time, you don't need to care -- Just uselsand be happy that it lives somewhere on yourPATHso the shell can find it.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.
Configure
PYTHONPATHto include the directory where your module resides and python will be able to find your module.
PYTHONPATHis an environment variable- Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7on Ubuntu- 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).
- if you use pip or setuptools, then you don't need to set
PYTHONPATHexplicitly
If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.
ubuntu - How to set up Python path? - Unix & Linux Stack Exchange
Are the paths to the python standard library and to the dist-packages different for different linux distributions(Debian, Redhat, Slackware, ...)? - Stack Overflow
Python Library Path - Stack Overflow
setuptools - How to find out the Python library installation path? - Stack Overflow
The site module documentation and Modifying Python's Search Path seem to be what you're looking for.
As far as I understand it, those entries are being added to sys.path by:
/usr/lib/python2.6/site.py/usr/lib/python2.6/dist-packages/site.py
(Change 2.6 to your version of Python.)
The easiest way to change it is to add a file /usr/local/lib/python2.6/dist-packages/site-packages.pth containing ../site-packages.
Alternatively, maybe you can teach the package to use site.getsitepackages()?
I'd like to summarize my findings about python's path modification. There are two ways to do it.
- .pth file
PYTHONPATH
Any .pth file which is found on the default path (see bellow) will get its content included into sys.path.
Format of said .pth file is simple: one (folder) path per line. Surprisingly, the paths can be absolute or relative to the .pth file.
Default path is where the interpreter resides and <some-prefix>/lib/python<version>/site-packages where <some-prefix> is usually /usr/.
PYTHONPATH is environmental variable of your operating system. On unix systems you list them by env. Global modification of such variables is done through .sh scripts inside /etc/profile.d/ folder as mentioned by @TestUser16418.
I'm assuming that when you installed anaconda 2, you manually set the PYTHONPATH environment variable, by putting something like
PYTHONPATH=/home/user/anaconda2/python
export PYTHONPATH
in your .bash_profile or .bash_rc.
But since you deleted the /home/user/anacanda2/ directory, that path no longer exists.
Thus you want to change PYTHONPATH to point to the executable in /usr/lib, by changing the above to
PYTHONPATH=/usr/lib/my_python_distribution
export PYTHON
execute the command:
echo $PATHroot1@master:/usr/lib/python2.7$ echo $PATH /home/root1/anaconda3/bin:/home/root1/NAI/Execution/HDE/x86_64.linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/root1/java/jdk1.8.0_74/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/root1/NAI/hadoop-2.7.3/binRemove your anaconda3 from your path variable that is
/home/root1/anaconda3/bin:Again set PATH variable with remaining information like below
export PATH=/home/root1/NAI/Execution/HDE/x86_64.linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/root1/java/jdk1.8.0_74/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/root1/NAI/hadoop-2.7.3/binExecute python command and should redirect to your python interpreter
root1@master:/usr/lib/python2.7$ python Python 2.7.14 (default, Sep 18 2017, 00:00:00) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
This message is not asking for the python libraries themselves, it's asking for the headers to link against them. You can install then with:
sudo apt-get install python-dev
You are probably lacking some other dependencies.
You should be able to find it in the following locations
/usr/lib/
/usr/local/lib/
Yes it's different, in CentOS 5.6, for example, python external modules are placed in /usr/lib/python2.6/site-packages. Actually dist-packages is debian specific directory.
You can read more into this subject from here http://www.aosabook.org/en/packaging.html, $ it talks allot about packaging and library locations if we want to deploy a package to the software repository.
I have talked a little bit about this in my blog, please have a look and give me some feedback
http://insidepython.wordpress.com/2012/08/03/quickintro/
Cheers
How python finds its modules
Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).
By default, Python looks for its modules and packages in $PYTHONPATH.
To find out what is included in $PYTHONPATH, run the following code in python (3):
import sys
print(sys.path)
How to add a directory
Occasionally
From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
For example:
if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:
import sys
sys.path.insert(0, "/home/myname/pythonfiles")
And I can simply import the file module_1.py by:
import module_1
When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py
For example:
To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `
from <packagename> import module_2
Adding directories to $PYTHONPATH permanently:
Add the following line to your ~/.profile file.
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Subdirectories
From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:
import packagename.subfolder.module_3
Given the fact that all subfolders in the package include their own __init__.py file.
When a module is in the same directory as the script or application
There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.
Example:
If I have a folder, containing script.py and module.py, I can simply import the module by:
import module
The correct way to use relative import is:
from ..Common import foo1
And you also need a __init__.py in all your folders.
If the packages have already been installed, you could just import them, and look into their __file__ property:
>>> import mymodule
>>> print mymodule.__file__
'/path/to/mymodule.py'
Nearly found a solution to this problem. I refereed to this question and this question so thanks to answers given there.
Firstly you'll need to get a list of all installed modules into a list. Use this question to capture the output of the solution to this question.
Now you have a list of all installed python modules. You will need to see the format of list and then format it properly to get individual elements as the names of the modules.
Then you can import the modules from their names as strings as explained here. Then as alejandro already said mymodule.__file__ contains the paths.
This is one solution that should work. Not very elegant but I am just a Python beginner who is better at google search than Python
I found a much easier way to find where modules are. This might be the "elegent" solution that OP was looking for.
import sys
print sys.path
From the Python docs about sys module sys.path contains
A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.