Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to using the shell, and wanted to create a directory in $HOME where I can put all my python scripts, set a path to that directory, so that I can go into any folder on my Mac and execute those scripts on certain files, without the script having to be contained inside the same directory as the file that would serve as the input to those scripts.

I have read around and added this to my .zshrc file:

export PATH="$HOME/python_functions/bin:$PATH"

Then I added a script called sleep_plotter.py to python_functions/bin, which is where I am planning to put all my future scripts as well.

However, when I navigate to the folder that contains the text file I want to use as input to that script, and type python sleep_plotter.py 113testCtM113.txt, the last argument being the text file input to my script, I get the following error message:

python: can't open file 'sleep_plotter.py': [Errno 2] No such file or directory

But when I call the path using echo $PATH, I see this:

/Users/myname/python_functions/bin: 

From this, I gathered that python is looking in that directory when I execute a Python command, so it should be able to run sleep_plotter.py even when I am in a different folder that doesn't contain this file.

I am using Mac OSX 10.11.2, zsh, and Anaconda 2.3.0.

share|improve this question
up vote 3 down vote accepted

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
share|improve this answer
    
Thanks for the answer. It makes sense! But when I followed what you said, I get this error message: name 'chmod' is not defined... – user46807 Feb 13 at 12:31
    
That's an OS shell command. – techraf Feb 13 at 12:33
    
OK, it works. Thank you! – user46807 Feb 13 at 12:35
    
After closing and re-opening the iTerm, it doesn't work anymore, and I get zsh: command not found: sleep_plotter.py, even after executing chmod +x /Users/myname/python_functions/bin/sleep_plotter.py. The shebang sequence is still at the top of my script.. – user46807 Feb 13 at 12:44
    
You don't need to repeat chmod. Check if the folder is still defined in $PATH – techraf Feb 13 at 12:46

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.