Why Ubuntu doesn’t support certain form of shebang for Python?

GatoLoko gatoloko at gmail.com
Tue Nov 11 17:02:03 UTC 2014


El 23/10/14 a las 00:31, vova escribió:
> PEP 394 and 397 suggest a simplified form `#!python2` — it works on
> Windows 7 and 8 just fine.

None of those PEPs suggest the use of a line like that. PEP 397 is about 
a launcher, and that launcher could support that kind of line.

Windows doesn't know about shebangs, and couldn't care less about them. 
Windows associates files based on their extension, in this case ".py", 
to a known executable, and when you double click on a file, it launches 
the executable and passes the file as an argument. Just the same as it 
does with any non-executable file (.doc, .jpg, ...). That is why it works.

Linux/Unix, on the other hand, doesn't care about file extensions, and 
identify the files based on their content, specifically the first two 
bytes. In this case, finding the shebang (#!), treats the file as a 
script to be interpreted, and parses the first line expecting it to 
contain a full path, preferably an absolute path, to an interpreter, and 
optionally arguments. Absolute path means you CAN'T use something like 
"python2", but "/usr/bin/python2". That's why it doesn't work on Ubuntu 
(nor any other linux/unix environment).

> So I guess that form of shebang isn’t supported, which is unfortunate
> since it questions portability of programs written in Python.

That line simply isn't a proper shebang because it doesn't have a full 
path. Use a proper one and your program will be portable. Windows will 
keep ignoring it. Linux/Unix will use it when it's a real shebang.

Since different distributions and unix systems may have different paths, 
you may want to use the env utility in a shebang like "#!/usr/bin/env 
python2".


-- 
Raúl Soriano






More information about the Ubuntu-devel-discuss mailing list