Python interpreters#
Most computers have multiple Python interpreters installed that need to be distinguished and managed.
System Python#
Depending on the operating system and version, your computer may have zero, one or even multiple Python interpreters pre-installed. This is referred to as system Python, as it ships as part of the operating system.
Warning
If you use the system Python, never install packages globally (with a naive pip install <package>
). Otherwise you may end up breaking your operating system because of incompatibilities between the package versions you installed and what the system requires to work properly.
Let’s consider the example of our D-PHYS Linux server login.phys.ethz.ch
running Ubuntu. Use the which -a
command to list all available commands found in the $PATH
.
johndoe@phd-login1:~$ which -a python3
/usr/bin/python3
/bin/python3
Given that /bin
is a symlink to /usr/bin
, both executables refer to exactly the same file.
Next we determine the actual Python version
johndoe@phd-login1:~$ python3 --version
Python 3.10.6
Lastly we check what lies behind the generic python
command.
johndoe@phd-login1:~$ which -a python
/usr/bin/python
/bin/python
johndoe@phd-login1:~$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 7 Apr 15 2020 /usr/bin/python -> python3*
So in this particular case python
is identical to python3
as it is a symlink. But this may not be the case on other systems.
You may notice that there is still a python2
installed.
johndoe@phd-login1:~$ python2 --version
Python 2.7.18
However always use Python 3+ exclusively, as the old version 2 is end-of-life and no longer developed since January 2020 (PEP 373).
The latest macOS ships only Python 3 as part of the operating system (or more precisely, its Xcode command line tools).
john@macbook:~$ /usr/bin/python3 --version
Python 3.9.6
But before macOS 12.3 /usr/bin/python
was a symlink to the obsolete python2
.
There is no Python installation included with Windows.
User Python#
If the operating system comes without Python or ships an outdated version, the user has to manually install a Python interpreter. In contrast to the system Python this is commonly referred to as user Python.
Typically the system Python is sufficient for most users. Especially on our D-PHYS Linux workstations, where the user has no admin rights and cannot mess up the system-wide package installation. Otherwise we recommend to use pyenv.
The recommended way is to not touch the system Python at all and install a user Python with homebrew.
john@macbook:~$ brew install python
It even allows to install multiple (major) versions with brew install python@3.x
, while keeping them easily up-to-date with brew upgrade
. Experienced users may even use pip install <package>
to install packages globally inside the homebrew Python.
Python can be installed directly from the store or using the official packages. Alternatively you may use the Anaconda distribution.
Selecting the proper interpreter#
Note
From now on, whenever we use the generic python
command, we mean the Python 3 interpreter and you may replace it with python3
if needed.
On Unix-like systems Python programs are typically started from the command line. The actual shell command is the Python interpreter followed by the name of the Python script.
$ python myscript.py
As mentioned above (and in the Linux modules), python
refers to the first command found in the list of directories in your shell’s $PATH
environment variable. You can use which -a python
to see the full list. You can either adapt the $PATH
or explicitly call a different Python interpreter by using its full path.
$ /usr/local/bin/python myscript.py
Alternatively a shebang can be used as first line in the Python script, to tell the system which interpreter to use when executing this file. Even though this could be the full path to a specific interpreter, it’s best practice to let the shell’s environment decide which interpreter to pick. This allows other users to select their favorite interpreter by adapting the $PATH
variable. Also note how we explicitely use python3
to avoid picking up an obsolete Python 2.x on some systems.
#!/usr/bin/env python3
print("Hello World")
With a correct shebang, you can make the script executable with chmod
and run it directly.
$ chmod a+x myscript.py
$ ./myscript.py
Despite the shebang, you are still free to run it with any other interpreter.
$ /path/to/some/python myscript.py
The selection of the interpreter in the Windows command line is conceptually similar to Unix. You also run a Python script using the Python executable.
$ python myscript.py
If multiple python
commands are installed, a path environment variable defines the order of precedence of the various folders. The actual syntax depends on the shell used.
Command Prompt (cmd):
$ echo %PATH%
$ where python
PowerShell:
$ $Env:Path
$ Get-Command python -all