Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu Python shebang line not working

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

test.py

#!/usr/bin/env python

print ('!')

Ran

:which python
/usr/bin/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?

Thanks

like image 997
DrewK Avatar asked Feb 11 '13 21:02

DrewK


People also ask

How do you use shebang in python?

Shebang is a special character sequence represented by #! (hash and exclamation) characters followed by a path indicating what program should be used to execute a script. The shebang line, also called the interpreter directive, comes at the beginning of a script file.

Where is shebang written in a python file?

#!/usr/bin/env python """ The first line in this file is the "shebang" line. When you execute a file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the "bang".

Do you need shebang for python script?

You should add a shebang if the script is intended to be executable. You should also install the script with an installing software that modifies the shebang to something correct so it will work on the target platform.

What is the correct shebang line for a bash script?

What is bash shebang? The #! syntax is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The directive must be the first line in the Linux shell script and must start with shebang #! .


4 Answers

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.)

like image 63
chepner Avatar answered Oct 06 '22 06:10

chepner


On the python docs page it says:

To easily use Python scripts on Unix, you need to make them executable, e.g. with

$ 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.

like image 40
ajon Avatar answered Oct 06 '22 06:10

ajon


Apart from executing the script with a preceding dot or making it executable, there might be another issue:

If you try to use a script written with a windows editor, it may contain windows line endings. Removing these can make the shebang work again.

To remove such line endings, refer to How to convert Windows end of line in Unix end of line (CR/LF to LF) for instance.

See also my general remarks on failed shebang evaluations at my other answer.

like image 34
DomTomCat Avatar answered Oct 06 '22 07:10

DomTomCat


Make sure that the "FIRST LINE" is the shebang. Do not give any newline character in the beginning of the file. "No newline character in beginning"

like image 29
Ruhil Jaiswal Avatar answered Oct 06 '22 06:10

Ruhil Jaiswal