python – Shebang doesnt work with python3

python – Shebang doesnt work with python3

Generally, take care of some pitfalls:

  1. set the executable flag on the script: chmod u+x test.py

  2. try to execute with a preceding dot ./, so call ./test.py otherwise it might execute some other script from within your PATH

  3. also make sure you dont have windows line endings, this seems to prevent the shebang evaluation, too. There are some suggestions around, e.g. in this answer, on how to convert the format.

    If python3 test.py works, then the windows line endings are probably your problem.

  4. #!/usr/bin/env python3 is the best way to define the shebang (i.e. use this as first line of your script), since the python binary may be installed somewhere else. env will inspect the PATH environment to find the binary

  5. As @ShaileshKumarMPatel has pointed out in the comments here, make sure, theres no wrong line beginnings (color characters etc)

EDIT: The OPs kind of error looks like windows line endings to me. Ive had them, too, with different output though

If the python script has come into contact with a windows machine it is worth trying to clean up the line endings with:

$ dos2unix programmer.py

python – Shebang doesnt work with python3

Windows end line was my problem too. Writing the same program with a linux editor like VI solved it for me. Also, using less command , i was able to see the windows end line characters

Leave a Reply

Your email address will not be published.