The environment variable PYTHONPATH is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:
python -c "import sys; print(sys.path)"
Or if want the output in the UNIX directory list style (separated by :) you can do this:
python -c "import sys; print(':'.join(x for x in sys.path if x))"
Which will output something like this:
/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/ python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib /python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist- packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0: /usr/lib/pymodules/python2.7Answer from Hubro on Stack Overflow
The environment variable PYTHONPATH is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:
python -c "import sys; print(sys.path)"
Or if want the output in the UNIX directory list style (separated by :) you can do this:
python -c "import sys; print(':'.join(x for x in sys.path if x))"
Which will output something like this:
/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/ python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib /python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist- packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0: /usr/lib/pymodules/python2.7
Just write:
just write which python in your terminal and you will see the python path you are using.
PATH variable defines the directories which are searched when executing commands. However when you execute python sleep_plotter.py 113testCtM113.txt, sleep_plotter.py is an argument to the python program (command). Shell uses PATH to find python, but not its arguments.
You can add an executable attribute to your script:
$ chmod +x /Users/myname/python_functions/bin/sleep_plotter.py
Add a shebang sequence to the top (first line) of your Python script:
#!/usr/bin/env python
And run the script directly as a command:
$ sleep_plotter.py 113testCtM113.txt
If you want to extend python to search through additional directories, then you need to do two things: set the PYTHONPATH variable and use the -m option in Python.
In ~/.zshrc (note that there is no $PATH in there):
export PYTHONPATH="$HOME/python_functions/bin"
And in the terminal use:
python -m sleep_plotter 113testCtM113.txt
which as much typing as you did before, because you have to leave out the .py extension on the python file.
The variable PYTHONPATH that you echo in the terminal is added to the other paths of python. So if you don't have any particular path set in your .profile or .bashrc file (or locally), the variable will be empty.
To see the path that python uses do in a python shell
import sys
print(sys.path)
Or as @mgilson suggestes, you can run from terminal
python -c 'import sys; print(sys.path)'
A note: If you decide to install by hand a package using python setup.py install --user you don't need to add $HOME/.local/lib/pythonX.X/site-packages to PYTHONPATH, as it is already in sys.path
If you want Python to have some extra set of paths in it's sys.path in every Python session apart from the default ones (site-packages etc) you add it to the $PYTHONPATH environment (local or system) variable.
Most probably you don't need it right now, leave it as it is.
Plus you'll know when you really need it populate it.
If you use site.addsitedir("path") in almost every Python path then you can add that "path" to $PYTHONPATH.
Check virtualenv.
You can type which python on the ubuntu terminal and it will give the Python installed location path.
First, I hope you don't really set PYTHONPATH=/etc, /etc is for configuration files, not python libraries.
You can see what an environment variable is set to by using echo, e.g.: echo $PYTHONPATH. If the variable has not been set it will be blank. You can also use env to get a list of all environment variables, and couple with grep to see if a particular one is set, e.g. env | grep PYTHONPATH.
You would probably also want this:
import sys
print(sys.path)
Or as a one liner from the terminal:
python -c "import sys; print('\n'.join(sys.path))"
Caveat: If you have multiple versions of Python installed you should use a corresponding command python2 or python3.
sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
I suggest not to rely on the raw PYTHONPATH because it can vary depending on the OS.
Instead of the PYTHONPATH value in the os.environ dict, use sys.path from the sys module. This is preferrrable, because it is platform independent:
import sys
print(sys.path)
The value of sys.path is initialized from the environment variable PYTHONPATH, plus an installation-dependent default (depending on your OS). For more info see
https://docs.python.org/2/library/sys.html#sys.path
https://docs.python.org/3/library/sys.html#sys.path
If PYTHONPATH hasn't been set then that's expected, maybe default it to an empty string:
import os
print(os.environ.get('PYTHONPATH', ''))
You may also be after:
import sys
print(sys.path)
I don't have a windows computer, but you could probably use os.environ to get it without relying on an external utility -- On *NIX it looks something like:
import os
print (os.environ['PATH'])
mgilson has the better advice, but anyway, FYI, echo is a builtin command of cmd.exe:
subprocess.call("CMD /C ECHO %PATH%")
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it, Please add
C:\Python27;
along with it. Else it will never work.
Windows 7 Professional I Modified @mongoose_za's answer to make it easier to change the python version:
- [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
- Click [New] under "System Variable"
- Variable Name: PY_HOME, Variable Value:C:\path\to\python\version

- Click [OK]
- Locate the "Path" System variable and click [Edit]
Add the following to the existing variable:
%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;

Click [OK] to close all of the windows.
As a final sanity check open a command prompt and enter python. You should see
>python [whatever version you are using]
If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.
For Mac/Linux;
PYTHONPATH=/foo/bar/baz python somescript.py somecommand
For Windows, setup a wrapper pythonpath.bat;
@ECHO OFF
setlocal
set PYTHONPATH=%1
python %2 %3
endlocal
and call pythonpath.bat script file like;
pythonpath.bat /foo/bar/baz somescript.py somecommand
import sys
sys.path.append('your certain directory')
Basically sys.path is a list with all the search paths for python modules. It is initialized by the interpreter. The content of PYTHONPATH is automatically added to the end of that list.
It's not clear what your problem could be, but note that you don't need to add the default value of sys.path to your PYTHONPATH variable. The directories you put in PYTHONPATH are additional directories to search; the system default is appended to your PYTHONPATH. In other words, roughly speaking:
sys.path = ":".split( os.environ['PYTHONPATH'] ) + sys.path
Showing the exact value of PYTHONPATH and the resultant errors would help us determine the problem.
On Unix Systems, it should translate to /usr/local/lib/python** where ** is Python version...Like 2.7 and so on...
Your answer actually lies in the definition of PYTHONPATH and PYTHONHOME variables.
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONHOME
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.
I would suggest you also try this
import sys
sys.path.append('Directory you wanna add to the path')
Hope this helps.