Take the 2-minute tour ×

Unable to get shebang line working in Ubuntu for python script. I only get a command not found error each time.


#!/usr/bin/env python

print ('!')


:which python

Played around with different locations for python in the shebang but no luck including what was provided by which python. Any tips on how to troubleshoot this?


share|improve this question
Make sure your line endings are unix \n newlines. –  Martijn Pieters Feb 11 '13 at 21:19
I made the test file in nano. I usually use sublime but this is on my server so just whipped it up in nano. I honestly have no idea how to view special chars in nano. I just assumed since I made it on linux it would handle the new line appropriately. –  DrewK Feb 11 '13 at 21:33
It probably is unix newlines then; is the file executable? (chmod +x scriptname.py) –  Martijn Pieters Feb 11 '13 at 21:35
it is indeed. Not sure if this helps but if I run (/usr/bin/python test.py) it runs like a champ –  DrewK Feb 11 '13 at 21:40
What does file test.py say? –  Martijn Pieters Feb 11 '13 at 21:45

2 Answers 2

up vote 4 down vote accepted

If you are trying to run the command as

$ test.py

the error may not have anything to do with the shebang. Rather, the directory that test.py resides in is not in your PATH. Try

$ ./test.py

to bypass PATH lookup.

(This is in addition to making sure that the script itself is executable.)

share|improve this answer
ugh that was my primary problem in testing, can you explain how the logic of ./test.py works? –  DrewK Feb 11 '13 at 21:51
When your command consists of a single word, the shell looks for that command in each directory stored in PATH, starting with the first, and using the first match it finds. The current directory (.) is typically not in PATH, as it could be a security risk, so you need to specify the exact path as ./test.py (the file test.py in the current directory). –  chepner Feb 11 '13 at 21:54
Not to keep asking but why is PATH a security risk? –  DrewK Feb 11 '13 at 22:03
@DrewK: adding the current directory to PATH is considered a security risk because an attacker would be able to "override" standard commands by placing a script in the current directory. Imagine a custom ls script which acts exactly as normal ls except not showing directories called .evil-hidden-stuff... –  Sergey Feb 11 '13 at 22:29
I've never seen it stated this way, but I think the recommendation against putting '.' in your PATH would extend to any relative path. The idea is that the lookups should conform to a fixed set of directories that does not depend on your current working directory. –  chepner Feb 11 '13 at 23:01

On the python docs page it says:


$ chmod +x script and put an appropriate Shebang line at the top of the script. A good choice is usually

#!/usr/bin/env python which searches for the Python interpreter in the whole PATH. However, some Unices may not have the env command, so you may need to hardcode /usr/bin/python as the interpreter path.

I don't know if this applies for you or not.

share|improve this answer
I thought that my apply and I had tried use those paths and could not get it to run. –  DrewK Feb 11 '13 at 21:35

Your Answer


