[Python-ideas] Migration of /usr/bin/python to python3

Andrew Barnert abarnert at yahoo.com
Thu Mar 12 21:38:01 CET 2015


> On Mar 12, 2015, at 8:51 AM, random832 at fastmail.us wrote:
> 
> Show me where POSIX defines "recognized, loadable executable format" to
> exclude #! as a magic number with its usual behavior.

I already quoted the standard earlier in the thread, and I already explained how something like linux's pluggable binfmt provides a way around it that's still at least according to the letter of the standard. I don't see much point in repeating what I already wrote. But notice that in the RATIONALE section of the 2001, 2004, 2008, and 2013 versions of the standard (e.g., http://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html) effectively describes that as legacy behavior in contrast to the behavior that's "now required by IEEE Std 1003.1-2001":

> Another way that some historical implementations handle shell scripts is by recognizing the first two bytes of the file as the character string "#!" and using the remainder of the first line of the file as the name of the command interpreter to execute.

At any rate, this is getting way off track. The point here is that if you're looking for a standard on what #! does everywhere, you're not going to find one. The working group on executable scripts for POSIX-2001 eventually decided to add nothing more than the statement that scripts that start with #! have behavior undefined by the standard, and the recommendation apps that want to use #! should include an installer that sed's the script based on what's on the PATH at install time. Which makes the standard useless if you want to write portable shbang scripts.

But practicality beats purity. If all of the *nix platforms that Python currently supports do shbang processing, all have env in /usr/bin (also specifically not required by POSIX, which explicitly says that path locations are not determined by the standard), and none of them swallow all args (as early BSD did), but some drop all args after the first, some pass all, and some pass a single arg with spaces, then that's what you have to work with. You can use "#!/usr/bin/env python", but you can't use "#!/usr/bin/env python -m ipython".

Of course if you're building a Mac installer or packages for a particular linux distro or the like, you don't have to worry about portability at all, and can do what makes sense for your platform. (This also means not having to worry about other distros that provide a Python 3.x as /usr/bin/python, etc.)


More information about the Python-ideas mailing list