Reading Time: 5 minutes

In this tutorial, we are going to cover how to set up a Python virtual environment on CentOS. A Python virtual environment makes it possible to install Python packages into a discreet Python ecosystem that is entirely separate from your system’s default Python framework. This means that you do not have to worry about overwriting the installation of any current packages that might be defaulted to the existing version of Python on your system.

Another one of the outstanding aspects that makes Python such a joy to work with is its natural ability to encapsulate and manage dependencies. The primary way to integrate dependencies in any Python project is by using a virtual environment. Additionally, you can easily pull the specific dependencies for your project directly into your virtual environment, allowing for additional functionality. We will be exploring this idea and how to enable a virtual environment on your personal server. 

How to Set Up A Python Virtual Environment On CentOS

Pre-flight Check

  • These instructions are being performed as the root user on a Liquid Web Self-Managed CentOS 8 server.
  • These instructions will work on either a CentOS 7 or 8 server.
  • These instructions assume you already have a working installation of Python 2 and Python 3 versions 3.3 or higher, with pip, on your server. If you’re looking for instructions on how to get Python 3 installed on your CentOS 7 box, check out this tutorial!

Venv

As of Python version 3.3 and later, a module named Venv has been included in the Python standard library. The Venv module allows us to create virtual environments alongside our base Python installation. This means we do not have to introduce another third-party package or other dependencies to our project to get up and running with an alternative Python virtual environment.

Step 1: Update The Environment

To make sure that we are working with the most up to date environment possible in terms of our packages, we can run the following command:

[root@centos8 ~]# yum update -y

Step 2: Create the Virtual Environment

Now that the environment is up to date, we can go ahead and create the virtual environment:

[root@centos8 ~]# python3 -m venv python3-virtualenv

There you have it! Utilizing the Venv module, we have just created a Python virtual environment.

Step 3: Activate the Virtual Environment

The fantastic thing about Python virtual environments is the control it provides around dependency management. Before we go over activating the new virtual environment we’ve created, let’s check out what packages are installed on the system’s Python 3.

[root@centos8 ~]# pip3 freeze
asn1crypto==0.24.0
cffi==1.11.5
configobj==5.0.6
cryptography==2.3
decorator==4.2.1
gpg==1.10.0
idna==2.5
iniparse==0.4
netifaces==0.10.6
pciutils==2.3.6
perf==0.1
ply==3.9
pycparser==2.14
pygobject==3.28.3
pyOpenSSL==18.0.0
python-dateutil==2.6.1
python-dmidecode==3.12.2
python-linux-procfs==0.6
pyudev==0.21.0
rhnlib==2.8.6
rpm==4.14.2
schedutils==0.6
six==1.11.0
slip==0.6.4
slip.dbus==0.6.4
syspurpose==1.23.8

As you can see, there are a number of packages that are already installed. Now, let’s activate the Python virtual environment we just created.

[root@centos8 ~]# source python3-virtualenv/bin/activate
(python3-virtualenv) [root@centos8 ~]#

You’ll notice that (python3-virtualenv) precedes your command prompt now. This indicates that the Python virtual environment is active. We can also verify the version of Python while the virtual environment is live by running this command:

(python3-virtualenv) [root@centos8 ~]# python --version
Python 3.6.8

Now, let’s go ahead and install a package in our virtual environment and upgrade pip while we are at it:


(python3-virtualenv) [root@centos8 ~]# pip install -U pip requests
Collecting pip
 Downloading https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl (1.4MB)
  100% |████████████████████████████████| 1.4MB 832kB/s
Collecting requests
 Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl
Requirement already up-to-date: certifi>=2017.4.17 in ./python3-virtualenv/lib/python3.6/site-packages (from requests)
Requirement already up-to-date: idna<2.9,>=2.5 in ./python3-virtualenv/lib/python3.6/site-packages (from requests)
Requirement already up-to-date: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./python3-virtualenv/lib/python3.6/site-packages (from requests)
Requirement already up-to-date: chardet<3.1.0,>=3.0.2 in ./python3-virtualenv/lib/python3.6/site-packages (from requests)
Installing collected packages: pip, requests
 Found existing installation: pip 9.0.3
  Uninstalling pip-9.0.3:
   Successfully uninstalled pip-9.0.3
Successfully installed pip-19.3.1 requests-2.22.0

Now let’s take a look at the packages installed in the virtual environment:

(python3-virtualenv) [root@centos8 ~]# pip freeze
certifi==2019.11.28
chardet==3.0.4
idna==2.8
requests==2.22.0
urllib3==1.25.7

You should notice, the list is much smaller than your system’s Python 3. That’s because the only package installed in the Python virtual environment we just created is the requested package we just installed along with its dependencies. That’s it! Now, to finish up, let’s deactivate the virtual environment:

(python3-virtualenv) [root@centos8 ~]# deactivate
[root@centos8 ~]#

Virtualenv

There are circumstances where it might be necessary to work with a version of Python that doesn’t include the Venv module. This is where the virtualenv package comes into play. Virtualenv is a third-party Python module that can be used to create a new virtual Python environment that behaves nearly identically to the environment created via the Venv module.

It is important to note that although the Venv module is more streamlined than virtualenv, virtualenv is more feature-rich. One such feature that virtualenv offers is the ability to specify which Python version of the virtual environment should use.

Step 1: Install Virtualenv

In this case, we will work with our Python 2 installation. Installing virtualenv is as simple as installing any other Python package via pip.

[root@centos8 ~]# pip2 install virtualenv
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip2 install --user` instead.
Collecting virtualenv
 Downloading https://files.pythonhosted.org/packages/05/f1/2e07e8ca50e047b9cc9ad56cf4291f4e041fa73207d000a095fe478abf84/virtualenv-16.7.9-py2.py3-none-any.whl (3.4MB)
  100% |████████████████████████████████| 3.4MB 316kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.9

Step 2: Create the Virtual Environment

Once virtualenv is installed, we can go ahead and create a virtual environment. In this case, let’s specify that we want to base the virtual environment on Python 2.

[root@centos8 ~]# virtualenv -p $(which python2) python2-virtualenv
Running virtualenv with interpreter /usr/bin/python2
Already using interpreter /usr/bin/python2
 No LICENSE.txt / LICENSE found in source
New python executable in /root/python2-virtualenv/bin/python2
Also creating executable in /root/python2-virtualenv/bin/python
Installing setuptools, pip, wheel...
done.

Step 3: Activate and Verify the Virtual Environment

Next, let’s activate the virtual environment. One aspect that’s worth mentioning is that activating the Python virtual environment is the same regardless of whether it was created via virtualenv or Venv.

[root@centos8 ~]# source python2-virtualenv/bin/activate
(python2-virtualenv) [root@centos8 ~]#

Now, we can verify what version of Python we are running.

(python2-virtualenv) [root@centos8 ~]# python --version
Python 2.7.15

We can also go ahead and install a package while the virtual environment is active.

(python2-virtualenv) [root@centos8 ~]# pip install requests
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting requests
 Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in ./python2-virtualenv/lib/python2.7/site-packages (from requests) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in ./python2-virtualenv/lib/python2.7/site-packages (from requests) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./python2-virtualenv/lib/python2.7/site-packages (from requests) (1.25.7)
Requirement already satisfied: certifi>=2017.4.17 in ./python2-virtualenv/lib/python2.7/site-packages (from requests) (2019.11.28)
Installing collected packages: requests
Successfully installed requests-2.22.0

You’ll notice the deprecation warning at the beginning of the output for the package installation. Again, some legacy software may still rely on Python 2, but for now, all new projects should be based on Python 3. Finally, let’s take a look at the package list in the Python 2 virtual environment.

(python2-virtualenv) [root@centos8 ~]# pip freeze
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
certifi==2019.11.28
chardet==3.0.4
idna==2.8
requests==2.22.0
urllib3==1.25.7

Once again, the only packages we see are the requests package we installed and its dependencies. The virtual environment created with virtualenv is also deactivated in a manner identical to those created with Venv:

(python2-virtualenv) [root@centos8 ~]# deactivate
[root@centos8 ~]#

Save Big Today!

Utilizing a virtual python environment can allow you the flexibility and versatility to develop and grow a web based application easily! Additionally, our servers have the flexibility to expand and evolve with your business as your traffic and interactions increase with your userbase!

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of these techniques today!

Avatar for Justin Palmer

About the Author: Justin Palmer

Justin Palmer is a professional application developer with Liquid Web

Latest Articles

Blocking IP or whitelisting IP addresses with UFW

Read Article

CentOS Linux 7 end of life migrations

Read Article

Use ChatGPT to diagnose and resolve server issues

Read Article

What is SDDC VMware?

Read Article

Best authentication practices for email senders

Read Article