While working with Python, it’s not uncommon for developers to need different versions for different projects. In this article, we will explore the process of downgrading Python from version 3.10 to 3.9 on various operating systems including Windows, Linux, and Mac operating systems. We will also discuss common problems and their solutions that may arise during the downgrade process.
For compatibility and functionality, it is crucial to have an appropriate Python version installed. If you find that your current Python version is causing issues or is not compatible with specific libraries, downgrading to a previous version might be the last solution.
[lwptoc]
Downgrading Python on Windows
Following are the ways to downgrade Pythong on the Windows operating system:
Uninstall the current Python version
- First, open the Control Panel.
- Find and click on “Uninstall a program” under “Programs.”
- Find your current Python installation (Python 3.10) and click “Uninstall.”
- Then complete the uninstallation process by following the steps.
Install desired Python version
- Go to the Python Releases for Windows page.
- Download the Python 3.9 installer for your Windows version (32-bit or 64-bit).
- Run the installer and follow the on-screen instructions to complete the installation process.
Downgrading Python on Linux
Using update-alternatives
- Open the terminal.
- Check the available Python versions using the following command:
sudo update-alternatives --list python
- If Python 3.9 is listed, you can set it as the default version using the following command:
sudo update-alternatives --set python /usr/bin/python3.9
If Python 3.9 is not listed, proceed to the next method.
Using pyenv
- Install
pyenv
using the following command:
curl https://pyenv.run \| bash
- Add the following lines to your shell configuration file (e.g.,
~/.bashrc
or~/.zshrc
):
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
- Restart your terminal or run
source ~/.bashrc
(orsource ~/.zshrc
). - Install Python 3.9 using the following command:
pyenv install 3.9.0
- Set Python 3.9 as the global default version using the following command:
pyenv global 3.9.0
Downgrading Python on macOS
Using brew
- Install Homebrew if you haven’t already by following the instructions on brew.sh.
- Install Python 3.9 using the following command:
brew install python@3.9
- Add the following line to your shell configuration file (e.g.,
~/.bashrc
,~/.zshrc
, or~/.bash_profile
):
export PATH="/usr/local/opt/python@3.9/bin:$PATH"
- Restart your terminal or run
source ~/.bashrc
(orsource ~/.zshrc
orsource ~/.bash_profile
).
Using pyenv
- Install
pyenv
using Homebrew with the following command:
brew install pyenv
- Add the following lines to your shell configuration file (e.g.,
~/.bashrc
,~/.zshrc
, or~/.bash_profile
):
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
- Restart your terminal or run
source ~/.bashrc
(orsource ~/.zshrc
orsource ~/.bash_profile
). - Install Python 3.9 using the following command:
pyenv install 3.9.0
- Set Python 3.9 as the global default version using the following command:
pyenv global 3.9.0
Common Problems and Solutions
Here are some common problems you may face while downgrading the Python version with their possible solutions:
ImportError: No module named ‘xyz’
This error occurs when the required module is not installed. To fix this, install the module using pip
:
pip install xyz
ModuleNotFoundError: No module named ‘xyz’
Similar to the ImportError, this error occurs when the required module is not installed. Install the module using pip
:
pip install xyz
AttributeError: ‘module’ object has no attribute ‘xyz’
This error occurs when you are trying to access an attribute or function that does not exist in the imported module. Check the module’s documentation to ensure you are using the correct attribute or function name.
ValueError: unknown locale: UTF-8
This error occurs when your system does not recognize the UTF-8 locale. To fix this, add the following lines to your shell configuration file (e.g., ~/.bashrc
, ~/.zshrc
, or ~/.bash_profile
):
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
Restart your terminal or run source ~/.bashrc
(or source ~/.zshrc
or source ~/.bash_profile
).
Conclusion
You can easily downgrade Python from version 3.10 to 3.9 on Windows, Linux, and macOS by following the steps outlined in the above article.