Configuring the Default Version of Python
We recommend that you specify a default Python interpreter for all users. Setting a default Python interpreter ensures that:
- All users are using a consistent version of Python. Users must manually specify if they wish to use another version.
- Users do not mistakenly invoke the operating system’s Python distribution.
You can set the default Python interpreter by modifying the system PATH. The recommended method to add Python to the PATH is to append the version of Python that you installed to the system-wide PATH variable. For example, this can be defined in a script within the /etc/profile.d/ directory:
/etc/profile.d/python.sh
PATH=/opt/python/<PYTHON-VERSION>/bin:$PATHWhere <PYTHON-VERSION> is the version of Python that you installed. Scripts in /etc/profile.d/run when a user logs in. To verify that the change has taken effect, re-login and run the following:
which python3
/opt/python/<PYTHON-VERSION>/bin/python3The python3 command should now point to the Python interpreter you have set as the default.
python vs. python3
Some users may be used to invoking Python using the python command instead of python3. One or both of these commands may be available depending on how you installed Python. You can check all the available commands by reviewing your Python installation’s bin directory. For example:
ls -l /opt/python/3.10.4/bin | grep python
-rwxr-xr-x 1 root root 237 Aug 16 14:16 ipython
-rwxr-xr-x 1 root root 237 Aug 16 14:16 ipython3
lrwxrwxrwx 1 root root 10 Aug 16 14:16 python -> python3.10
lrwxrwxrwx 1 root root 10 Aug 16 14:16 python3 -> python3.10
lrwxrwxrwx 1 root root 10 Aug 16 14:16 python3.1 -> python3.10
-rwxrwxr-x 1 root root 16809792 Aug 16 14:16 python3.10
-rwxrwxr-x 1 root root 3487 Aug 16 14:16 python3.10-config
lrwxrwxrwx 1 root root 17 Aug 16 14:16 python3-config -> python3.10-configIn the example above, you could invoke Python using any of python, python3, python3.1, or python3.10.