I'm trying to make my .py files executable so I can run them using ./filename.py, but its not working for me.

What I did was adding the shebang #!/usr/bin python3 and used the command chmod +x filename.py. When I run ./filename.py then as normal user, I get the error message below

bash: ./filename.py: /usr/bin: bad interpreter: Permission denied

As superuser, it tells me this instead:

sudo: unable to execute ./filename.py: Permission denied

Opening the file the usual method (python3 filename.py) it works fine.

When I changed the shebang to #!/usr/bin/env python3 it tells me this:

/usr/bin/env: ‘python3\r’: No such file or directory

marked as duplicate by muru bash Mar 28 '17 at 1:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please do which python3 to get the location, then #!/usr/bin/python3 without spaces in the file in question... – George Udosen Mar 25 '17 at 19:46
  • #!/usr/bin/env python3 (or less portable #!/usr/bin/python3, but it is recommended to use env) is the correct shebang. Either you have a typo in that line, or your installation is a bit messed up if there is no /usr/bin/env executable. Can you verify it is there using which env and/or /usr/bin/env --version? – Byte Commander Mar 25 '17 at 19:47
  • @ByteCommander, i literally copied and pasted it from the file. no typos there. the exact message i get is /usr/bin/env: ‘python3\r’: No such file or directory. the response for which env is /usr/bin/env. the version is 8.25. – young marx Mar 25 '17 at 20:12
  • @JacobVlijm I created the file and edited it both with Pycharm and vim. the second shebang (the one with env in the path) wasn't wrong as much as i can tell. and it doesn't work any better with the right shebang, i get bash: ./camel_game.py: /usr/bin/python3^M: bad interpreter: No such file or directory when using #!/usr/bin/python3. the working directory is ~/PycharmProjects/filename. – young marx Mar 25 '17 at 20:36
  • FYI in vim, you can use command set ff=unix to change the line endings from DOS style (\r\n) to Unix style (\n) - no need for an external command such as dos2unix – steeldriver Mar 25 '17 at 22:12

The problem are your line ending characters. Your file was created or edited on a Windows system and uses Windows/DOS-style line endings (CR+LF), whereas Linux systems like Ubuntu require Unix-style line endings (LF).

There is a simple tool that can convert the two different styles for you called dos2unix.

Install it by running

sudo apt install dos2unix

After that, you can convert files in either direction using one of the commands



$ cat test.py 
#!/usr/bin/env python3

$ ./test.py 
/usr/bin/env: ‘python3\r’: No such file or directory
$ dos2unix test.py 
dos2unix: converting file test.py to Unix format ...
$ ./test.py 

To also come back to what you tried first, the shebang line

#!/usr/bin python3

is of course wrong. It tries to execute the file /usr/bin with python3 and the filename of your script as arguments. This must obviously fail because /usr/bin is a directory and no executable file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.