When should I use the Python shebang line #!/usr/bin/env python3 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

When should I use the Python shebang line #!/usr/bin/env python3 ?

In the most Python files which I see here on sololearn there is no shebang line #!/usr/bin/python used. I am curious in which use cases I need this. Is this only needed on certain operation systems? the sequence "#!/usr/bin/env python3" seems to be a linux specific path. When I code on Linux, then I run my codes directly from IDLE and they run without problems without the shebang. Please enlighten me.

11th Oct 2020, 5:08 AM
Jan Markus
2 Answers
+ 9
You can use the shebang to make your script directly executable from linux command line. So it will be similar to any bash command. https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-JUMP_LINK__&&__python__&&__JUMP_LINK-scripts-and-what-form-should-it-take
11th Oct 2020, 6:06 AM
Tibor Santa
Tibor Santa - avatar
+ 5
I use it if, for whatever reason, I coded in python 2.7 instead of 3.x. the shebang above really doesnt do much because it would just route the code to run through the default python version of the system. it doesnt specify wich version it uses. they most likely copied from a computer basically you can route your code through your python version of choice
11th Oct 2020, 5:13 AM
Slick
Slick - avatar