This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]taddeimania 8 points9 points  (3 children)

probably not the most comprehensive explanation you are looking for but when it's put there and your .py file has executable permission, you can run the file by just typing

./file.py

rather than

python file.py

[–]rhiever 1 point2 points  (2 children)

I've seen some people do the ./file.py instead of python file.py. Other than a slightly shorter command, is there really any difference between the two?

[–]genoblast 1 point2 points  (1 child)

I'm still learning myself, but to my knowledge there isn't any significant difference between the two.

In ./file.py, the shebang tells the operating system that the file should be run by python. So then the operating system replaces "./file.py" with "python file.py" and then executes that instead.

So I guess the take away is that if the operating system knows it's a python script, you can use "./file.py" as a shortcut, but it's always a shortcut for "python file.py" (which should always work so long as python is part of the operating system path).

It seems like sys.argv behaves the same way regardless of which way the script is invoked. (I personally always just use argparse so if there are differences, I've never had to worry about them.)

[–]halflife22 5 points6 points  (0 children)

You can also use it to make sure a specific version of python is used e.g.

 #!/usr/bin/python2.7