0

I am trying to run a python script in linux without calling python explicitly. My goal is to have $ myscript.py run my script. Currently calling $ python myscript.py works but I am looking to not have to type the extra command for ease of use. I added the shebang line to the first line of my script to get the proper python call. I have tried both of the following lines and neither has worked for me.

#!/usr/bin/env python2.7
#!/usr/local/bin/python2.7

The problem is that I get the following behavior

$ python2.7 myscript.py    # This will run

$ myscript.py           # This is the error
$ ./myscript.py         # This will also error
: Permission denied     # Error message

When I do ls -ltr on the file I have executable permission for the script and the executable

-rwxrwxr-x 1 uname users   3544 Jul  7 08:46 myscript.py
-rwxr-xr-x 1 root  root 6231413 Jul  7 00:57 /usr/local/bin/python2.7

I can also call python in the command line by typing what is written in either the shebang lines into it.

/usr/bin/env python2.7
/usr/local/bin/python2.7

both run python in the terminal.

I have ran through the following stackoverflow problems and none seem to answer why this problem is happening to me.

bash permission denied for python

Python script: problems with shebang line (unix)

user2716722
  • 93
  • 11
  • 3
    Tried `./myscript.py` ? – Ocaso Protal Jul 07 '17 at 13:57
  • 1
    What do you see when you do `ls -l /usr/local/bin/python2.7`? – Jack Jul 07 '17 at 13:59
  • "Permission denied" is really odd. Normally `PATH` wouldn't contain `.`, in which case the shell would say something like "command not found". Are you sure about the name of the script? Is your username literally "uname", or is that simply anonymised? Which version of Bash are you running? – l0b0 Jul 07 '17 at 14:02
  • 2
    You need `./file_name` to run it.Look this : [Why do we use “./” to execute a file?](https://unix.stackexchange.com/questions/4430/why-do-we-use-to-execute-a-file),and this :[Executing Shell Script from current directory without '“./filename”](https://stackoverflow.com/questions/14932983/executing-shell-script-from-current-directory-without-filename) – hxysayhi Jul 07 '17 at 14:06
  • Do you have SELinux running? – Raman Sailopal Jul 07 '17 at 14:11
  • @RamanSailopal How do I tell if SELinux is running. I know there are security measures set in place but I don't know how to chek what they are. Also I edited my question to answer the comments. uname is just anominized and my script name call is correct. – user2716722 Jul 07 '17 at 14:39
  • Why are you running your script as `python myscript.py` directly, but use `python2.7` in the shebang? Try with: `#!/usr/bin/env python`. Also, is the posted error verbatim from the console, or you've modified the output? – zwer Jul 07 '17 at 14:40
  • You can check it with "sestatus" and then check current mode. – Raman Sailopal Jul 07 '17 at 14:49
  • The origional console output was `: Permission deniedn2.7` but after trial and error with a couple other things it looked like it was just overwriting my shebang call to write the error. I do call python2.7 in the console I just forgot to add it to my post. Also when I type sestatus it says that it is not found. – user2716722 Jul 07 '17 at 14:53
  • 2
    Does your file using \*nix line endings (LF) or DOS line endings (CR LF)? A number of shebang implementations don't like DOS line endings. – jwodder Jul 07 '17 at 14:55
  • Put a copy of myscript.py in /usr/bin, chmod it to be executable and try again. It may just be a `PATH` issue – Rolf of Saxony Jul 07 '17 at 14:57

2 Answers2

1

Make sure to use LF line endings not CRLF line endings when running on linux! Thank you @jwodder for the suggestion.

I was using sublimetext to edit my files in windows and running the files on the linux machines. I changed the preferences in sublime to use unix line endings (LF) but I already wrote the file in the DOS endings (CRLF). I thought it would switch over the line endings for me. My assumption was wrong. I converted them all to LF and the script ran as expected.

user2716722
  • 93
  • 11
0

I had this same problem.

In fact I had two different python files in the same directory. One would execute as > myfile1.py correctly.

The other would not, as in your explanation above

myfile2.py /home/mylogin/PYTHON/myfile2.py: Permission denied.

It turns our I had created one in notepad++ on windows and the other in vi on linux.

jwodder nailed the problem.

Solution was: dos2unix * for all python files in the directory.

SailorDad
  • 11
  • 2