How to Change the Default Python Version
Installing a newer version of Python does not always update the default Python version. In this case, it's confusing when the default Python version is not the expected one. This guide will explain how to change the default Python version and resolve this issue.
[Updated for 2021]
Regardless of platform (Mac, Linux, or Windows):
-
Use python3 command: On most platforms, the python3 command leaves your python2 installation untouched. This allows you to execute Python3 without affecting Python2.
Historically:
-
Backwards compatibility: Python2 and Python3 have significant compatibility differences, so for older systems and scripts, python2 was expected. Changing the default would break these.
Circa Year 2021:
-
Explicit Python version calls: Many software now explicitly specify python2 or python3, making it less necessary to maintain the old default python command.
-
pep-394: This PEP outlines the handling of the python command on Unix-like systems. It recommends using a virtualenv instead of changing the default.
Shell Alias:
- Create a custom shell alias, such as alias py=python3 in your shell startup file. This sets python3 as the default for py and is specific to your local computer.
Multiple Python Versions:
- If you encounter two different versions like Python 3.1 and Python 3.2, check your installation methods and use your OS's program management tools to uninstall or manage the older version.
- Otherwise, modify your $PATH variable to prioritize the desired Python version.
Understanding $PATH:
- $PATH is an environment variable that lists the directories where the system searches for executable files.
- The first matching command in the first directory is executed.
- Python2 and Python3 may have symbolic links in $PATH to their actual versions.
Additional Notes:
- It's generally not recommended to change default system settings for Python.
- Virtual environments allow you to isolate different Python versions for specific projects.
- The default Python version can vary depending on your operating system and shell configuration.
The above is the detailed content of Why Isn't My Default Python Version the Latest One I Installed?. For more information, please follow other related articles on the PHP Chinese website!