Introduction

By default every Linux system has a specific version of python installed. For instance, python2.7 is installed on CentOS 7.

Changing the default python version will create a lot of problems because many applications lay on this python version (for example yum won’t work anymore).

The solution : install another python in a separate folder. This python will coexist with the python of the OS. And then create a virtual environment with this new python.

Install the new python version, for instance python 3.9.7 build from the source

Note : by default sqlite3 is inside python installation since python2.5, but when you build python from the source code as it is shown here, you have to install sqlite-devel (yum install sqlite-devel) before building python. Otherwise you’ll encounter the message error “No module named ‘_sqlite3’.

yum groupinstall "Development Tools" -y
yum install openssl-devel libffi-devel bzip2-devel -y

Now gcc is available :

gcc --version
yum install wget -y
wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
tar -xvzf Python-3.9.7.tgz
cd Python-3.9.7
./configure --enable-optimizations
make altinstall
python3.9 --version
pip3.9 --version

Create a virtual environment for your Django Project

Go into your Django project

cd path_to_django_project

And then execute the following command : python<version> -m venv <virtualDirectoryEnvName> (this is the valid command since python version 3.3)

We use the python version we want to get in our virtual environment.

python3.9 -m venv .venv

Activating the virtual environment :

source .venv/bin/activate
# you can check your python version (exit() to quit) :
(env) python -v
# and check your pip version :
(env) pip --version

Now you are in your python virtual environment, you can installed your django project dependencies inside this virtual environement. For instance if you have a requirements.txt in your project you will do :

pip install -r requirements.txt