How to run my python script from a terminal in Mac OS X without entering the full path?

I am on Mac OS 10.6 Snow Leopard and I am trying to add a directory to my PATH variable, so I can run the little script that I wrote by simply typing: python alarm.py at the terminal command line.

I put the path in the .profile file and it seems to appear when I echo $ PATH, but python still can not find the script that I put in this directory.

Here is the contents of my .profile file in my home directory:

~ toby$ vim .profile export PATH=/Users/tobylieven/Documents/my_scripts:$PATH 

Here's the output of echo $ PATH, where everything looks good:

 ~ toby$ echo $PATH /Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin 

Here's the script I'm trying to run:

 ~ toby$ ls /Users/tobylieven/Documents/my_scripts -rwxrwxrwx@ 1 tobylieven staff 276 17 Jan 21:17 alarm.py 

Here is the command I'm trying to use to run the script and the error message I get:

 ~ toby$ python alarm.py python: can't open file 'alarm.py': [Errno 2] No such file or directory 

If anyone has an idea what I can do wrong, that would be great. Many thanks.

+8
source share
5 answers

PATH is for executables only, not python scripts. Add to the top of your Python script:

 #!/usr/bin/env python 

and run

 sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py 

Then you can enter just alarm.py to execute your program.

+18
source

modify alarm.py to enable:

 #!/bin/python 

as the very first line in the file.

(or / usr / bin / python, depending on where the python interpreter is located). You can figure this out by typing: which python in the terminal.)

Then you can just run alarm.py instead of python alarm.py .

eg:.

 ~ toby$ alarm.py 

And the fig that beat me for a few seconds is right, you need to add execution permissions (via chmod) to alarm.py.

+4
source

You need to change the specific Python path variable: PYTHONPATH.

So:

 export PYTHONPATH=/Users/tobylieven/Documents/my_scripts 

should make you work.

See: Python module search path

+3
source

What python are you aiming for?

Did you install this with brew? He uses a different way.

which python3 or which python

Choose the one you want

Copy this output

Paste it at the top of your Python file

add #! before this path so it looks like

#!/usr/local/bin/python3

Be sure to change file permissions

chmod +x filename

Put this file in a folder that is in your path

Not sure if your folder is in your path?

echo $path

How to add this folder to your path?

Find your way first

echo $HOME

If you use bash or zsh, you might have something like this

In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file

export PYTHON_UTILS="$HOME/code/python/utils"

export PATH="$PYTHON_UTILS:$PATH"

Consider removing .py from your file, as he is not needed in this case

Close and open your terminal, which receives your file by its path

And now you should be able to process your python file similarly to the bash command.

You do not need to use python3 filename.py to run the file, you can just use filename

From anywhere in your file system!

+1
source

Something interesting that I really came across in OS X from Window is that it is very difficult for you to get the directory of your current script.

I found this.

 #! /bin/zsh cd "${0:h}" 

Now you can execute the python file relative to the executed script instead of knowing the exact path where your python file is. This may or may not help, but I use it to improve the performance of my scripts and .command files.

-one
source

All Articles