I’m sure this is self-evident for anyone with halfway decent knowledge of the Unix command-line, but it took me some searching through multiple sources to figure it out. (Kept getting env: python\r: No such file or directory
errors whenever I tried to run the script on its own.)
If you need to run a Python script in the Mac OS X Terminal, save the script somewhere on your hard drive and run the following command:
python path/to/script [options]
Obviously, replace [options] with any options or arguments that the script accepts (or leave it off entirely). Remember that if you’re using bash for your shell, you can hit tab to autocomplete paths (which speeds things up a lot) and you’ll need to escape any spaces in paths with a backslash:
python ~/Documents/My\ Python\ Scripts/script.sh
Please note: I’m using OS 10.5; I believe the above will work with 10.2+ (since I think that’s when Python was bundled with the OS), but your mileage will vary. If you’re having issues, the Python documentation was one of the best resources I found, and of course the help file for the python
command was useful once I figured out I needed to use a command other than running the script (type man python
to access it).
You should be able to add a shebang line to the top of your script to avoid this:
#!/usr/bin/python
Also, don’t forget that on Unix systems the current directory is not usually in the path (on DOS-based systems it is always in the path). So if you are in a directory and want to run a script you have to do it like this:
./somescript.py
;)
Todd
Posted 4:54 PM on May. 18, 2008 ↑
There actually was a line like that although it read #!/usr/bin/env python. I tried dropping the “env” and making it /usr/bin/python (since I’d peeked into the /urs/bin folder and seen what looked like a Python executable), but the script still failed. I’m not entirely sure why. Running it with the python command was the only way I could get it to work.
It might be that I need to configure the $path variable for my bash shell (it looked like it only included /usr/local/bin), but the syntax in the ~/.login file was confusing enough that I figured it would be safer not to mess with it (particularly since I had gotten the script to work).
Posted 5:19 PM on May. 18, 2008 ↑
ah, I bet your script does not have execute permissions on it. An ls -l will show you:
prometheus:bin kinch$ ls -l
…
-rwxr-xr-x@ 1 kinch kinch 1286 Dec 22 10:42 ftp-backup.sh
the first entry is the permission on the file for owner, group, and everyone. You can use chmod +x on the file to add execute permissions.
Todd
Posted 6:48 PM on May. 18, 2008 ↑
Amusingly enough, I had set chmod +x on the file before trying to run it (after setting the permissions in the Finder Get Info window to no effect).
I don’t know what it was with this file, but it just demanded to be run with the python command. :-)
Posted 7:20 PM on May. 18, 2008 ↑
ok, I have one last thought and then I will give up on my obsessive problem solving of this non-issue. I think your file has Mac line endings (\r) instead of Unix line endings (\n), which is confusing your shell.
If you have TextMate you can save with Unix line endings from the save as dialog. If you don’t you just need to find some other way to replace the carriage returns with linefeeds. A simple python script maybe :D
Todd
Posted 3:06 AM on May. 19, 2008 ↑
Ha; a Python script to fix my Python script would definitely solve all of my problems.
Posted 6:07 AM on May. 19, 2008 ↑
you can use the command which name is ‘dos2unix’ and try again the python file. ^^
Posted 6:19 AM on Aug. 21, 2008 ↑
Just a note for anyone trying to use Jonggun Lee’s suggestion: you’d better be using Windows to have it work.
Posted 6:58 AM on Aug. 21, 2008 ↑
Todd, your first suggestion was spot on for me!
./scriptname.py solved my problem, thanks very much.
Not that I understand what is happening, because I can do ls -l at the prompt and see my script file, but entering that script filename at the prompt gets me ‘command not found’.
I guess more reading is called for when I get a new candle.
Barry
Posted 2:06 PM on Sep. 1, 2008 ↑
Somewhere I came across the suggestion that a non-ascii script can have a problem. If UTF-8, there can be a ‘BOM’ marker at the front – invisible characters that get in front of the shebang #! and mess things up.
Barry
Posted 2:13 PM on Sep. 1, 2008 ↑
Thank you! Exactly what I was looking for.
Cheers!
Posted 3:14 AM on Apr. 25, 2010 ↑
Hi,
I would like to know if there is a way to avoid the python launcher once you execute the code in terminal.
Posted 3:21 PM on May. 13, 2010 ↑
I had struggled with trying to run a script from a .py file in idle. This tutorial was leaving me baffled until I followed the instructions in Terminal – Python.
Posted 7:42 PM on Nov. 14, 2010 ↑
The real issue is the end line characters.
‘\r’ error in osx means the file was created with windows style line endings (\r\n). Just convert them to mac style endings, then you’ll be able to do ./ as in Unix.
No need to do ‘python ‘
Posted 9:37 PM on Dec. 13, 2010 ↑
Thank you! I just started trying to learn Python and this was driving me crazy.
Posted 9:56 PM on Feb. 19, 2012 ↑
worked like a charm, much thanks.
Posted 12:56 AM on Sep. 3, 2012 ↑
Thankyou
Posted 7:32 PM on Sep. 5, 2012 ↑
Todd: I, too, am teaching myself Python, and your suggestion (it worked and) finally made me think! All I needed to do was “tell Python to run the script from the Terminal’s Command prompt”. e.g.
myMac$ python /Users//Desktop/miles.py
My problem was caused by trying to run the (above) script from the python prompt (i.e. >>>> /Users//Desktop/miles.py)
Grins. :-))
Posted 9:06 PM on Jan. 1, 2013 ↑
I had to use linux line endings on macosx for the python script to run as an executable.
Posted 6:59 PM on Feb. 23, 2014 ↑
One tip, if you have installed Python 3+ and want to use that as opposed to Python 2+ bundled with OSX, the terminal command is `python3`
Posted 7:47 AM on Apr. 7, 2014 ↑
I used the comment by Terry M 1/1/13 with success.
Thanks!
Posted 9:21 AM on Apr. 11, 2014 ↑
When I run python knockknock.py, in terminal, having switched the directory to ~/downloads/knockknock-master,
I get
File “knockknock.py”, line 9, in
import argparse
ImportError: No module named argparse
Ideas?
Posted 3:37 AM on Oct. 31, 2014 ↑
Sounds like you don’t have the argparse module installed. You are probably using an older version of Python (I think argparse was introduced in Python 3, although they may have backported it to 2.7). A search engine is likely going to be more help than I can be, though.
Posted 10:21 AM on Nov. 26, 2014 ↑
I am trying to execute python file from terminal but its giving me error
In [11]: python test.py
SyntaxError: invalid syntax
What is the right syntax to call a python file ??
Posted 7:22 AM on Jun. 8, 2015 ↑
That syntax works fine for me on my end. Most likely you have a syntax error within the Python file itself.
Posted 9:39 AM on Jun. 11, 2015 ↑