Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Most distros ship with python3 (though `python` will refer to python2).

Is there a reason you cannot just say `#!/usr/bin/env python3` in your scripts? I don't see why you require python3 to be the default, am I missing something here?



sort by: page size:

Exactly. I often use "#!/usr/bin/env python2" to specify python 2.x on my systems where python 3 is the default.

Came here to ask the same question. Under archlinux python 3 is the default, which breaks scripts who don't specify that their are python 2 only and start with #!/usr/bin/python and not #!/usr/bin/python2. But even there as user I don't see the need for this tool.

I think it's recommended for distros to modify the shebang to /usr/bin/env python3 for script that are supposed to run under python 3, although this does pose a problem for scripts that want to run under both without modification. If the distro ships with python3 and most of the system scripts run under python3 I'd say its the default.Ubuntu for one is planning on not shipping a python2 or /usr/bin/python binary by default(although it will still be in repos) eventually.

If you don't care which version of python it runs under, you do this:

    #!/usr/bin/env python
If you do, then `python2` or `python3` will be around, and you should specify them explicitly instead of `python`. I'm happy to be corrected on this one, but I always slap this at the top of my scripts:

    #!/usr/bin/env python3
I'm not aware of anywhere that wouldn't invoke a Python 3 interpreter if one is installed.

The original post just seems like one big faff.


My worry is that not everyone will have python2.7 so I'd rather stick to "#!/usr/bin/env python". It would be great if this works but it doesn't

#!/usr/bin/env python2


I think a number of distros include python3 by default, but -- per python's own standards (adopted as a result of disruption caused by Arch having python3 as /usr/bin/python -- it isn't linked to /usr/bin/python but to /usr/bin/python3; /usr/bin/python is (except on Arch) always python2. That's not a matter of which is "default python" (e.g., even if, as Ubuntu plans to, python3 is the only python installed (not merely "the default python"), it will only be /usr/bin/python3, not /usr/bin/python.)

This is about consistency given that major versions aren't intended to be backward compatible so scripts either fail or find a compatible python when using a shebang, not about what is "default".


As the GP said, Python3 is installed by default on any big distro for 5-10 years already.

It's not the default because /bin/python is synlinked to python2. You have to explicitly run it.


When you have several Python programs, where does /usr/bin/python point to?

I must admit, that I did not think to much about it until now, but normally there is only /usr/bin/python in the scripts.

Of course, if everybody has /usr/bin/python3 inside his Python3 programs, everything could work (unless some of your apps need special environment vars like PYTHONPATH, what could make it troublesome again).

But still, the standard installation of Debian (and others, as much I know) does not include Python3 by default, you at least have to add it to your installation. That is something, you must tell your customers, when you ship Python3 programs.


'python' might not be a valid command because 'python3' exists and 'python' does not.

When designing things for 'just work' scripts you have to take esoteric environments into account.

There is no guarantee that '#!/usr/bin/env python' will not error in an environment with only python3 installed.


The apps in question use a shebang of `#!/usr/bin/env python3`. I believe it is working as designed. This is a Fedora system, which switched to Python 3 a couple of revisions back. But, I would assume this would affect any system that has utilities that use that shebang.

Edit: Out of curiosity, I grepped for other system utilities that might have this quirk, and it seems like nearly all Python utilities call /usr/bin/python3 directly, but ten of them (on my system) use `env`. That might be a bug worth filing with the Fedora folks. I can't imagine they want it to act this way when a custom Python is installed. Though it's been that way for at least a couple of releases.


As a full time Arch user the change was a little annoying at first but now I am perfectly happy using Python3 as the default because all my Python shell scripts and system installed packages work under Python3. I can simply work in a Python2 virtualenv when I need python2.

I'm not an expert, but I believe just s/python/python2/. Also put it in the documentation as a requirement.

Edit: Hee hee

[aeikum@aeikum howdoi]$ howdoi use python2 on default python3

/usr/bin/env python2.7


Arch Linux uses Python 3 by default I believe, which is rather annoying as everything Python 2 related is suffixed with a 2, i.e "$ python2 file.py".

Ubuntu uses Python 3 where possible, but /usr/bin/python is 2.7.

Interesting, that's not recommended by python devs. Ubuntu follows the guideline of keeping /usr/bin/python as Python 2 (which will not be installed by default), and /usr/bin/python3 as Python 3.

ETA: Arch Linux actually did this before there was an "official" guideline. http://www.wefearchange.org/2012/04/python-3-on-desktop-for-...


If I can't make the executable, by default on installing, be 'python2', I'm adding quite a lot of pain to users (in particular, all those users with #!/usr/bin/env python2 in scripts).

I doubt very much Ubuntu has made python3 /usr/bin/python, since the official recommendation of the Python project is to not do that.

Still true of course that it doesn't have to be installed by default even if some applications still need it.


I have no problem with Arch having a "only install python3 by default" rule. They can even not distribute python2 for all I care.

But, don't make /usr/bin/python run python3, that just confuses programs which have made the (reasonable based on past experience, and official python advice) assumption that it /usr/bin/python will run python2.


This has been a long time coming. Not being able to use Python3.x for web development was a large barrier for me switching completely. Now Python3.x just needs to be the default for more linux distros.
next

Legal | privacy