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
Answer from Paula Munoz on Stack Exchangescripting - echo $PATH shows path to python scripts, but they cannot be executed - Unix & Linux Stack Exchange
python - How to get the PYTHONPATH in shell? - Stack Overflow
How do I find out my PYTHONPATH using Python? - Stack Overflow
python - how to print contents of PYTHONPATH - Stack Overflow
How do I add PYTHONPATH inside a Python script?
What is Pythonpath used for?
How do I create a Pythonpath variable?
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 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.
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)