How Can I Run My Python Script From The Terminal In Mac Os X Without Having To Type The Full Path?
Solution 1:
PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:
#!/usr/bin/env python
and run
sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py
Then, you can type just alarm.py
to execute your program.
Solution 2:
change alarm.py to include:
#!/bin/python
as the very first line in the file.
(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python
in the terminal.)
You can then just run alarm.py
instead of python alarm.py
.
e.g.:
~ toby$ alarm.py
And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.
Solution 3:
Which python are you targeting?
Did you install it with brew? It uses a different path.
which python3
or which python
Choose the one you want
Copy that output
Paste it at the top of your python file
add a #!
in front of that path so it looks something like
#!/usr/local/bin/python3
Make sure to change the file permissions
chmod +x filename
Put that file in a folder that is in your path
Not sure if your folder is in your path?
echo $path
How to add that folder to your path?
Find your path first
echo $HOME
If you are using 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 the .py
from your file bc it is not needed in this case
Close and open your terminal, which is sourcing your file by its path
And now you should be able to treat your python file similar to a bash command
You don't need to use python3 filename.py
to run the file, you can just use filename
From anywhere on your filesystem!
Solution 4:
You need to modify the Python specific path variable: PYTHONPATH.
So:
export PYTHONPATH=/Users/tobylieven/Documents/my_scripts
should get you working.
Solution 5:
Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.
I found this.
#! /bin/zsh cd"${0:h}"
Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is. This might or might not help but I use this a lot to make my scripts and .command files work better.
Post a Comment for "How Can I Run My Python Script From The Terminal In Mac Os X Without Having To Type The Full Path?"