4

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.

5

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
  • Thanks for the answer. It makes sense! But when I followed what you said, I get this error message: name 'chmod' is not defined... – pat_krat Feb 13 '16 at 12:31
  • That's an OS shell command. – techraf Feb 13 '16 at 12:33
  • 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.. – pat_krat Feb 13 '16 at 12:44
  • You don't need to repeat chmod. Check if the folder is still defined in $PATH – techraf Feb 13 '16 at 12:46
  • It works. For some reason my $PATH contained python_function instead of python_functions.. Thanks so much for your help, I really appreciate it! – pat_krat Feb 13 '16 at 13:00
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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