I need to change my python version from 3.8 to 3.6 ?
How can I achieve this in Ubuntu 20.04. I tried pyenv, but when I try to use pyenv like pyenv global 3.6.0 then I do python3 and I have still 3.8 verision.
2 Answers
Do not downgrade the system version: it's likely that some parts of the system would stop working. Never change /usr/bin/python3, and avoid putting an older version of python3 before it in the $PATH.
The deadsnakes archive provides packages of most supported Python versions for currently supported Ubuntu LTS versions. To make these packages available, follow the usual instructions to enable a PPA. Then install the package(s) you want.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
You can then create a virtual environment for your chosen Python version and with a chosen set of packages.
python3.6 -m venv ~/python/foo-3.6
sh -c '.export PYTHONNOUSERSITE=1; ~/python/foo-3.6/bin/activate; pip install …'
To run a program in this environment, source the bin/activate script in a shell.
$ bash
$ export PYTHONNOUSERSITE=1
$ . ~/python/foo-3.6/bin/activate
$ ./my_python_program
-
1When setting up the venv, it reports and error with ensurepip something non-zero exit status. The package python3.9 installed from deadsnakes doesn't include pip. How do I solve that?Pui Ho Lam– Pui Ho Lam2023-06-04 16:21:30 +00:00Commented Jun 4, 2023 at 16:21
-
@PuiHoLam Ask a new question about your problem and copy-paste the full output.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2023-06-04 17:33:58 +00:00Commented Jun 4, 2023 at 17:33
-
In my case, the non-zero exit code of ensurepip could be resolved by installing additional packages like
sudo apt install python3.9 python3.9-venv python3.9-devBatteryBackupUnit– BatteryBackupUnit2024-08-14 05:21:59 +00:00Commented Aug 14, 2024 at 5:21
Alternatively, you can create a virtual environment.
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is (not to downgrade) to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps: (Checked August 2022)
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
NB: If you'll get errors like:
E: Couldn’t find any package by glob ‘python3.7’ , stating that the packages can not be installed.
run the following commands below, then re-run the install command above:
apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
Find out where your python 3.7 is located by this command:
which python3.7(Should be something like /usr/bin/python3.7, if not found, then install python 3.7 manually)Create Virtual Environment in the Home directory.
cdmkdir virtual_env/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7source ~/virtual_env/venv_with_python3.7/bin/activatepython --version(Should be python 3.7 now)Done. Python 3.7 can be used in this virtual environment. Type
which python, you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.Run
deactivatewhen you need to deactivate.
/usr/bin/python3.6 /path/to/script.py?