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 deniedAs superuser, it tells me this instead:
sudo: unable to execute ./filename.py: Permission deniedOpening 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 5 1 Answer
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 dos2unixAfter that, you can convert files in either direction using one of the commands
dos2unix /PATH/TO/YOUR/WINDOWS_FILE
unix2dos /PATH/TO/YOUR/LINUX_FILEExample:
$ cat test.py
#!/usr/bin/env python3
print("ok")
$ ./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
okTo also come back to what you tried first, the shebang line
#!/usr/bin python3is 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.