SaltyCrane Blog — Notes on JavaScript and web development

/usr/bin/python: bad interpreter: Permission denied error

I have a Python script, myscript.py with a #!/usr/bin/python shebang* at the top and tried to execute it on Ubuntu Linux using ./myscript.py. I got the following error message:

bash:  ./myscript.py: /usr/bin/python: bad interpreter: Permission  denied 

Here are things to check:

  • The file should be executable (use chmod +x myscript.py)
  • The file shoud have Unix line endings
  • The file shouldn't be on a fat32 or ntfs filesystem. Apparently, bash can't handle scripts that are stored on fat32 or ntfs
  • The shebang line must be less than 128 characters (on Linux)

UPDATE 2015-09-23: 7 years later, I ran into this error again on our Jenkins server... A shebang line that is too long will also cause this error. On Linux, the maximum length is 128 characters. See:


* #!/usr/bin/env python would be the more portable shebang.

Comments


#1 Skawaii commented on :

I had the same problem when transfer a python script that I had started in Windows to my Macbook. Good old dos2unix to the rescue, and presto! It worked.


#2 Eliot commented on :

Skawaii, I haven't used dos2unix myself, but glad to hear that it worked well for you.


#3 JackChen commented on :

I got the same error in RedHat. Python 2.7.3 is configured and made by myself. [root@Ifx installer]# pip install Django -bash: /usr/local/bin/pip: /usr/local/bin/python2.7: bad interpreter: Permission denied

Solution: In /usr/local/bin/pip, replace first line #!/usr/local/bin/python2.7 with your actual Python path #!/root/installer/Python-2.7.5/python