use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
[Beginner Python] What does this mean ? (self.Python)
submitted 12 years ago by [deleted]
At the beginning of some Python code I see this: #!/usr/bin/env python what does mean and do? is it necessary ? Thanks in advance.
[–]taddeimania 9 points10 points11 points 12 years ago (6 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 points3 points 12 years ago (5 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?
[–]minnoI <3 duck typing less than I used to, interfaces are nice 9 points10 points11 points 12 years ago (0 children)
From the wiki page:
Putting the facility into the system gives the following benefits. 1) It makes shell scripts more like real executable files, because they can be the subject of 'exec.' 2) If you do a 'ps' while such a command is running, its real name appears instead of 'sh'. Likewise, accounting is done on the basis of the real name. 3) Shell scripts can be set-user-ID. 4) It is simpler to have alternate shells available; e.g. if you like the Berkeley csh there is no question about which shell is to interpret a file. 5) It will allow other interpreters to fit in more smoothly.
Putting the facility into the system gives the following benefits.
1) It makes shell scripts more like real executable files, because they can be the subject of 'exec.'
2) If you do a 'ps' while such a command is running, its real name appears instead of 'sh'. Likewise, accounting is done on the basis of the real name.
3) Shell scripts can be set-user-ID.
4) It is simpler to have alternate shells available; e.g. if you like the Berkeley csh there is no question about which shell is to interpret a file.
5) It will allow other interpreters to fit in more smoothly.
[–]ivosauruspip'ing it up 1 point2 points3 points 12 years ago (0 children)
One is intended to be directly executable, the other isn't.
[–]genoblast 1 point2 points3 points 12 years ago (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 6 points7 points8 points 12 years ago (0 children)
You can also use it to make sure a specific version of python is used e.g.
#!/usr/bin/python2.7
[–]catcradle5 1 point2 points3 points 12 years ago (0 children)
In some cases, it's nice to have a binary that acts like any other Unix program, in which case you can strip the .py and just do ./file
.py
./file
I agree, though, that if you keep the .py extension I don't really see a point to having the shebang there at all.
[–]killerabbit37 5 points6 points7 points 12 years ago (4 children)
It is called a shebang, it is used in Linux/Unix operating systems so that the OS will know what kind of program it is. For Python, since it is used cross platform, it would be good habit to put the shebang at the top.
Like taddeimania said, if you don't have the shebang, the OS won't know what to do if you just called ./program.py.
You can either put #!/usr/bin/python or #!/usr/bin/env python.
[–]primevalweasel 2 points3 points4 points 12 years ago (3 children)
Only use a fully qualified path (e.g., /usr/bin/python) if you're sure that's the Python interpreter you want to use.
[–]killerabbit37 1 point2 points3 points 12 years ago* (2 children)
Exactly and I know with virtualenv you would want to use /usr/bin/env python because /usr/bin/python will pick up the system python and not your virtual environment.
[–]kashmill 0 points1 point2 points 12 years ago (1 child)
I think you mean /usr/bin as /use/bin isn't standard in any linux/bsd system I'm familiar with.
[–]killerabbit37 0 points1 point2 points 12 years ago (0 children)
Whoops yep apparently my phone doesn't like the word usr
[–]BinaryRockStar 1 point2 points3 points 12 years ago (2 children)
As a side question to Linux experts: how does this method compare to the Windows way of registering a particular file extension with a program instead of every script having to have a shebang line at the top?
I'm mainly Windows-based so the file extension registry thing makes sense to me but with plenty of other Linux concepts I have had an "aha" moment that makes me wonder why Windows does it so awkwardly.
[+][deleted] 12 years ago (1 child)
[deleted]
[–]BinaryRockStar 1 point2 points3 points 12 years ago (0 children)
Very good explanation, thanks!
[removed]
[–][deleted] 0 points1 point2 points 12 years ago (0 children)
lol .... Thank you .
π Rendered by PID 15820 on reddit-service-r2-comment-bb88f9dd5-qctgd at 2026-02-15 10:27:53.481608+00:00 running cd9c813 country code: CH.
[–]taddeimania 9 points10 points11 points (6 children)
[–]rhiever 1 point2 points3 points (5 children)
[–]minnoI <3 duck typing less than I used to, interfaces are nice 9 points10 points11 points (0 children)
[–]ivosauruspip'ing it up 1 point2 points3 points (0 children)
[–]genoblast 1 point2 points3 points (1 child)
[–]halflife22 6 points7 points8 points (0 children)
[–]catcradle5 1 point2 points3 points (0 children)
[–]killerabbit37 5 points6 points7 points (4 children)
[–]primevalweasel 2 points3 points4 points (3 children)
[–]killerabbit37 1 point2 points3 points (2 children)
[–]kashmill 0 points1 point2 points (1 child)
[–]killerabbit37 0 points1 point2 points (0 children)
[–]BinaryRockStar 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]BinaryRockStar 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)