8

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.

  • 1
    Do you really need to change the python version or do you just need to be able to run certain scripts with a different version? – terdon Nov 03 '21 at 18:47
  • I Need to run script with a differen version – Brokolicový Džuß Nov 03 '21 at 18:48
  • 3
    Please [edit] your question and clarify that because changing the global python is a very bad idea and really not a good solution if you only need this for certain scripts. Are you just looking for `/usr/bin/python3.6 /path/to/script.py`? – terdon Nov 03 '21 at 18:49

2 Answers2

14

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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
8

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)

  1. 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

  1. 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)

  2. Create Virtual Environment in the Home directory.

    cd

    mkdir virtual_env

    /usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7

    source ~/virtual_env/venv_with_python3.7/bin/activate

  3. python --version (Should be python 3.7 now)

  4. 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 deactivate when you need to deactivate.

Hassan A
  • 3
  • 1
Hassan Risvy
  • 81
  • 1
  • 1