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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.