Dev ❤ Ops

How to install Virtual Environment – Python

A virtual environment is one of the very important tools for testing Python code without affecting the system python and its dependency. We can consider this as a temporary sandbox environment where we can install and use any version of python dependency without modifying system packages.

Step 1: Open terminal
Step 2: Check whether pip package manager is available, if not install it.
[DevOpsForU@devopsforu.com ~]$ which pip
/usr/bin/which: no pip in (/home/DevOpsForU/.local/bin:/home/DevOpsForU/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/ucb:/bin:/usr/bin)
[DevOpsForU@devopsforu.com ~]$ which pip3
/usr/bin/pip3
[DevOpsForU@devopsforu.com ~]$ pip3 --version 
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
Step 3: Install the virtual environment and check
[DevOpsForU@devopsforu.com ~]$ sudo pip3 install virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/9e/34/e86fc6a8f84329b49321a532b3c1fef103c67765df957fbb3852eea39d00/virtualenv-20.14.1-py2.py3-none-any.whl (8.8MB)
    100% |████████████████████████████████| 8.8MB 146kB/s 
  Downloading https://files.pythonhosted.org/packages/b1/78/dcfd84d3aabd46a9c77260fb47ea5d244806e4daef83aa6fe5d83adb182c/platformdirs-2.4.0-py3-none-any.whl
  Downloading https://files.pythonhosted.org/packages/ac/a3/8ee4f54d5f12e16eeeda6b7df3dfdbda24e6cc572c86ff959a4ce110391b/distlib-0.3.4-py2.py3-none-any.whl (461kB)
    100% |████████████████████████████████| 471kB 2.4MB/s 
Installing collected packages: platformdirs, zipp, importlib-resources, typing-extensions, importlib-metadata, filelock, distlib, virtualenv
Successfully installed distlib-0.3.4 filelock-3.4.1 importlib-metadata-4.8.3 importlib-resources-5.4.0 platformdirs-2.4.0 typing-extensions-4.1.1 virtualenv-20.14.1 zipp-3.6.0
Step 4: Create a virtual environment
[DevOpsForU@devopsforu.com ~]$ virtualenv mytestenv
created virtual environment CPython3.6.8.final.0-64 in 344ms
  creator CPython3Posix(dest=/home/DevOpsForU/mytestenv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/DevOpsForU/.local/share/virtualenv)
    added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Step 5: Activate the virtual environment and check
[DevOpsForU@devopsforu.com ~]$ source mytestenv/bin/activate
(mytestenv) [DevOpsForU@devopsforu.com ~]$ python --version
Python 3.6.8
Step 6: Install dependency and verify the virtual environment

systools is one of the example shown below this will be only installed under the current virtual environment and will not be present once will be out of the current virtualenv.

(mytestenv) [DevOpsForU@devopsforu.com ~]$ pip install systools
Collecting systools
  Downloading systools-0.1.0-py3-none-any.whl (266 kB)
     |████████████████████████████████| 266 kB 6.9 MB/s            
Collecting parsedatetime
  Downloading parsedatetime-2.6-py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 277 kB/s  
Step 7: Deactivate the virtual environment
(mytestenv) [DevOpsForU@devopsforu.com ~]$ deactivate 
[DevOpsForU@devopsforu.com ~]$ 

Once the virtual environment is activated, it gets visible on the left side of the terminal (Eg: (mytestenv) [DevOpsForU@devopsforu.com ~]$). We can have multiple virtual environments with different-different of python versions and select them as per need.

This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at contribute@devopsforu.com. You can also reach out to us from Contact-Us Page.

For more information please visit python.org

Follow us on LinkedIn for updates!

Leave a comment

Your email address will not be published. Required fields are marked *