3 ms·
One fix would have been for distros to have a special symlink like `/usr/bin/pythonx`. This could be a link to any version of Python, guaranteed to exist as lon
by kazinator 5y ago
One fix would have been for distros to have a special symlink like `/usr/bin/pythonx`. This could be a link to any version of Python, guaranteed to exist as long as at least one Python is installed, to be used by programs that can work with any version.
Probably too late for that; if you add this, you'are just complicating things: scripts then have to worry about pythonx not existing.
The env program could have a feature to help here. GNU Coreutils could adopt an env feature whereby it searches for multiple programs. Because of argument limits in the hash bang command line this would have to be encoded as one argument.
#!/usr/bin/env python|python3
So here, env sees there is a pipe in the middle of the program name. It splits the program name into two, and first searches the PATH for python. If that is not found, it searches the path for python3. Alternately, rather than giving preference to the left item, it could prefer the first one that occurs in the PATH. It could search the PATH once, stopping whenever any of the indicated names is found.
Or, how about supporting regex in env, if the first character is circumflex:
#!/usr/bin/env ^python3?
The advantage is that the specification is condensed. On some systems, there is a strict limit, like 32 characters, into which the hash bang line has to fit, so this could be helpful against that. The problem with that is that *env now has to read directory entries in user space to do the matching; it can't do a simple access() test on the name in every directory. This wholesale directory reading fails to take advantage of whatever name lookup caching mechanisms there are in the underlying kernel.
- jfrunyon 5y agoThat's what /usr/bin/python is, though? A link to any version of Python so long as any version is installed.
- kazinator 5y agoIf that were the case, the "I just want to run this one Python script" article and its trick would have no basis. The issue is that on some systems /usr/bin/python does not mean "any version of python", but specifically Python 2. And so, in those systems, if the Python 2 package is not installed, then there is no /usr/bin/python. They can't easily change the meaning of /usr/bin/python, because the distro users adopted to the convention and depend on unqualified python being python2, and python not being found as an indication that Python2 is not installed.
- teddyh 5y ago> /usr/bin/pythonx Maybe /usr/bin/sensible-python or /etc/alternatives/python?
- kazinator 5y agoThe sensible-python name carries connotations which are not there; this link could be any version of python whatsoever and differ from installation to installation. That version of python is not necessarily "sensible" compared to the others. Nor is writing in a reduced Python subset more sensible than using newer features; it's just different tradeoffs. The Debian-style alternatives mechanism could work here. I had that in the back of my mind when I wrote the comment, but I wasn't sure whether or not it's already buggered in some way by the Python2/3 thing. (Like if there are multiple Python2 packages somehow, using /etc/alternatives/python to have the semantics of which of the two is the preferred Python2.) If the /etc/alternatives/python name is available, it could be used this way: the preferred python (for running code that isn't specific to any python version). It works this way for some existing programs. For instance /etc/alternatives/yacc could be GNU Bison or Berkeley Yacc. Either implementation does not handle some inputs that the other does, and there is a POSIX yacc subset of the yacc language that should work with both. If you have a very plain yacc file that conforms to POSIX and stays away from corner cases in the spec, it should just work with the yacc command.