0

I am logged in as "myuser" which is also the administrator of the computer. I have a file on which I have the following rights:

-r--r--r--   1 myuser  staff  167 17 Feb 14:26 my_python.py

However, when I type in python my_python.py on the command line, I still can run the file. Don't I need execute (x) rights in order to do that?!

The only way I cannot run the file is when I change the rights to:

----------   1 myuser  staff  167 17 Feb 14:26 my_python.py
1

Python only requires the file contents to be read.

Recall that Python is an interpreted language and just processes the contents of that file, rather than executing it; python is the executable here! Therefore, calling the interpreter with the file as input/argument does not require other than read permissions (e.g.,python example.py).

1

It does not have to be executable, because you are running the python executable and pass this file to it.

However if You'd like to execute this file with ./my_python.py this file would have to be executable and has #!/usr/bin/python in the first line (or even better #!/usr/bin/env python).

  • why would that need to be executable if I try ./my_python.py? ./ only means I am in another folder to execute this file.. – Stophface Feb 17 '16 at 14:58
  • You can change ./ to the path to your py file. When You make it executable chmod +x you won't have to execute python because it's going to be run automatically from #!/usr/bin/python line ( en.wikipedia.org/wiki/Shebang_(Unix) ) – Mateusz Szlosek Feb 17 '16 at 15:06
  • Hm, that is still not clear. When I have the rw rights on a .py file and I execute it like python whatever.py I can. But, when I try to run ./python whaterever.py that does not? Why do I need x rights to run that?! – Stophface Feb 17 '16 at 15:18
  • Because python is not located in the current directory (it's probably in /usr/bin/python ). If You set x on the file it can be run without launching the python first (it does not have to be passed to python executable). So when the file has x you can simply run it by /path/to/file.py or if it's in the current dir with ./file.py (remember about shebang). – Mateusz Szlosek Feb 17 '16 at 15:22
  • 1
    @Stophface- ./ means the current working directory. So, ./python whatever.py is telling the shell to look for python is the current working directory. I suggest you read man execve to understand what Mateusz Szolsek is talking about. – fd0 Feb 17 '16 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .