Search code examples
pythonpython-3.xshebang

is `python3` always installed with Python 3?


I usually start all my scripts with the shebang line

#!/usr/bin/env python

However our production server has Python 2 as the default python, while all of our new scripts and programs are being built under Python 3. To help keep people from accidentally running the script with the default Python 2, I am considering switching all my shebangs from now on to this;

#!/usr/bin/env python3

On our server, python3 indeed points to Python 3, and our basic scripts will run correctly on it. However I am not clear if this is something specific to our installation, or if python3 is always available if Python 3 is installed?

I know this probably will not help a user who runs $ python myscript.py when the default Python is loaded, but its better than nothing and is clear enough in letting a user who inspects the script realize they are using the wrong Python version. Though now I also realize that, with Python being on version 3.8, a Python 4 is imminent... at the same time, I am not sure I am ready to embed code in every single script to check if Python >= 3 is loaded...


Solution

  • Yes, this is a safe bet.

    PEP 394 recommends Python 3 be available under the binary name python3 and most Linux distributions follow this recommendation. In fact, this is the only name under which Python 3 has been available in most distributions (the only outlier being Arch Linux, but even that also provides a python3 binary), and plans to make the ‘plain’ python binary also refer to Python 3 have only been made quite recently. The article ‘Revisiting PEP 394’ on LWN.net has more details.