Shebang Line Not Working

Ubuntu Python shebang line not working

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

Python - what is the shebang line supposed to do if the stated version is not installed ?? what if shebang is not the first line?

The shebang is what will be run if the script is run with chmod +x test.py and then ./test.py. Then the shell (i.e. bash) will run /usr/bin/env python2 test.py. However, when you explicitly use python3 test.py the shell will not check your shebang, it will just run python3.

Also, if you put the shebang at any line other than the first, it is no longer a shebang. Quoting Wikipedia:

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.

The shsebang can only be at the biginning. If it is not then the shell will not look at it, so it ran test.py as a shell script, where many commands are not found.

shebang line not working

Your file has Windows-type line endings. Convert it to proper Unix-type line endings and you should be good to go.

$ dos2unix test.py


Related Topics



Leave a reply



Submit