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.
asked Nov 3, 2021 at 18:41
3
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
answered Nov 3, 2021 at 19:11
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
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.
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
python --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
deactivate
when you need to deactivate.
answered Aug 19 at 9:16
The following talks about upgrade from 3.6.7 to 3.7.0 but you can use the same process for downgrade. You should not change the system python unless you really know what you're doing
First Install Pyenv
Installlation Instructions are here
Look at Pyenv Options
$ pyenv
pyenv 1.2.14
Usage: pyenv []
Some useful pyenv commands are:
commands List all available pyenv commands
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and deevlopment tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims [run this after installing executables]
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
--version Display the version of pyenv
version Show the current Python version and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help ' for information on a specific command.
For full documentation, see: //github.com/pyenv/pyenv#readme
Look at Python Versions
$ pyenv versions
system
* 3.6.7 [set by /home/taarimalta/.pyenv/version]
Install a new Python
$ pyenv install 3.7.0
Installing Python-3.7.0...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /home/taarimalta/.pyenv/versions/3.7.0
If you run into an issue with _ctypes install libffi-dev library
Now look at the versions
$ pyenv versions
system
* 3.6.7 [set by /home/taarimalta/.pyenv/version]
3.7.0
Select 3.7.0 for local environment
$ pyenv local 3.7.0
See that the version changed
$ pyenv versions
system
3.6.7
* 3.7.0 [set by /home/taarimalta/.python-version]
$ python
Python 3.7.0 [default, Jan 1 2020, 10:52:57]
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Switch to a different folder
cd ../project2
pyenv versions
system
* 3.6.7 [set by /home/taarimalta/.pyenv/version]
3.7.0
The python version may be different here depending on which python version you have set locally
Set pyenv version globally
This globally sets a python version for a user
pyenv global 3.7.0
Note that pyenv sets local version by adding a .python-version file
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
Note that pyenv knows the global version by looking at the ~/.pyenv/version
file
cat ~/.pyenv/version
3.8.2