Python Forum
Is the #! (shebang) line obsolete?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is the #! (shebang) line obsolete?
#1
Hi!

It looks like the Python language is advancing much much quicker than books on Python, so I find that although some sites explaining python are somewhat behind, a much bigger gap is normally found with books on python, to the point that there is usually a better way to do things in python than what you find in books.

I just saw on a book dealing with python 3, that you need to start a program with a #! (shebang) line which tells your computer that you want Python to execute this program. The shebang line begins with #!, but the rest depends on your operating system:
  • On Windows, the shebang line is #! python3.
  • On OS X, the shebang line is #! /usr/bin/env python3.
  • On Linux, the shebang line is #! /usr/bin/python3.

So, I'm using Python 3.7.4 on a computer running Windows 10, and it seems to me that with or without the shebang line, I have to proceed the same way to run a program, that is to say, I have to click 'Run Module' from the dropdown menu 'Run' on the IDLE environment.

That makes arise some questions:

1) What does the #! (shebang) line exactly do?
2) Is the #! (shebang) line obsolete?

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#2
newbieAuggie2019 Wrote:1) What does the #! (shebang) line exactly do?
The shebang line is not related to the python language. It is a Unix system feature that tells the unix shell which command interpreter to use to execute the file containing the shebang line. So if a file spam.py starts with #!/usr/bin/python3 it is an indication to the OS that this program must be run by the executable program /usr/bin/python3.

Linux and MacOS inherited this feature from Unix, and Windows adopted this feature more recently because as the years go by, Microsoft has realized that it needs to include an ever increasing number of Linux features to keep its market share.

newbieAuggie2019 Wrote:2) Is the #! (shebang) line obsolete?
The shebang line is by no means obsolete, only you don't need it to run python programs if you always tell the computer which interpreter to use.
Reply
#3
1) Books are always outdated the moment they're printed, there's nothing special about python in that regard.
2) The shebang line is useless on windows, because windows runs programs based on file extension. Other operating systems peek at the first line couple lines to look for which interpreter should be used.
3) No, the shebang is not obsolete, and using #!/usr/bin/env python3 will help ensure your program runs on the most platforms.
Reply
#4
(Oct-29-2019, 09:37 PM)Gribouillis Wrote: The shebang line is by no means obsolete, only you don't need it to run python programs if you always tell the computer which interpreter to use.

Thank you!

Do you mean by 'interpreter', IDLE, bash, Cmder or PyScripter, for example?

(Oct-29-2019, 09:39 PM)nilamo Wrote: 1) Books are always outdated the moment they're printed, there's nothing special about python in that regard.

Thank you!

I agree, but when I decided to start learning a programming language and chose Python, I saw some graphics comparing the evolution of several programming languages, and it seems that Python evolves quicker than others, so I guess that means that books on python are outdated quicker than books on other languages. For what I saw, I might be wrong, I would say that a book from the time Python 2 was updated and Python 3 was advised to be used instead, many books on python were outdated, while I think a book on C from that period still could be used nowadays without major problems.

(Oct-29-2019, 09:39 PM)nilamo Wrote: 3) No, the shebang is not obsolete, and using #!/usr/bin/env python3 will help ensure your program runs on the most platforms.

So if I use as a first line #!/usr/bin/env python3, will my program run on Windows, Mac and Linux, for instance?

Thanks again to both of you, and all the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#5
newbieAuggie2019 Wrote:So if I use as a first line #!/usr/bin/env python3, will my program run on Windows, Mac and Linux, for instance?
In Linux and Mac, this line will invoke the env program which in that case will find the correct python3 executable in your environment and run it with your script as program argument. I'm unsure about what it does in Windows. It probably won't run out of the box.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020