Installing a Python Virtual Environment (Part I)

Prashant Vats
2 min readJun 25, 2020

Python is one of the advance programming languages which uses module and package heavily. This enables Python to become one of the easiest languages to get started, accomplish a requirement quickly and modular.

A Python application or code snippet will require respective modules with its version for their execution. The application may require a specific version of the module for the successful execution. Local System being used as a war zone for multiple application development, we may need a different version of the same module/library/package for different application.

This demands an isolated environment for each application to have a different version of the same module. Python Virtual Environment solves this requirement by creating multiple environments on the same OS.

Requirements

  • Python
  • pip (Python Package Installer)
~ » python3 --version                                                                                                                                                                                                
Python 3.7.3

~ » pip --version
pip 20.1.1 from /Users/Prashant/Library/Python/3.7/lib/python/site-packages/pip (python 3.7)

Install virtualenv package

virtualenv is a python package which helps us to manage the isolated virtual environment on my OS.

~ » pip install virtualenv                                                                                                                                                                                           
Defaulting to user installation because normal site-packages is not writeable
Collecting virtualenv
Downloading virtualenv-20.0.25-py2.py3-none-any.whl (4.7 MB)
|████████████████████████████████| 4.7 MB 3.4 MB/s
Collecting filelock<4,>=3.0.0
Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting importlib-metadata<2,>=0.12; python_version < "3.8"
Downloading importlib_metadata-1.6.1-py2.py3-none-any.whl (31 kB)
Collecting appdirs<2,>=1.4.3
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Requirement already satisfied: six<2,>=1.9.0 in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site-packages (from virtualenv) (1.12.0)
Collecting distlib<1,>=0.3.0
Downloading distlib-0.3.0.zip (571 kB)
|████████████████████████████████| 571 kB 4.2 MB/s
Collecting zipp>=0.5
Downloading zipp-3.1.0-py3-none-any.whl (4.9 kB)
Building wheels for collected packages: distlib
Building wheel for distlib (setup.py) ... done
Created wheel for distlib: filename=distlib-0.3.0-py3-none-any.whl size=340427 sha256=59f4a4cd572fd478c265180b4544904e53a0c0ef994f60bdee0f3a60f9309f30
Stored in directory: /Users/Prashant/Library/Caches/pip/wheels/a2/19/da/a15d4e2bedf3062c739b190d5cb5b7b2ecfbccb6b0d93c861b
Successfully built distlib
Installing collected packages: filelock, zipp, importlib-metadata, appdirs, distlib, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.0 filelock-3.0.12 importlib-metadata-1.6.1 virtualenv-20.0.25 zipp-3.1.0

Create a virtual environment

~/python-virtual-env » virtualenv csm                                                                                                                                                                                
created virtual environment CPython3.7.3.final.0-64 in 451ms
creator CPython3macOsFramework(dest=/Users/Prashant/python-virtual-env/csm, clear=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/Prashant/Library/Application Support/virtualenv)
added seed packages: pip==20.1.1, setuptools==47.3.1, wheel==0.34.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

~/python-virtual-env » ls
~/python-virtual-env » csm

Here csm is an arbitrary isolated environment name. Running virtualenv csm will create a csm directory in filesystem and stores default package. Moving forward after activating this virtual environment, all the new package will be installed in this directory.

Activate Virtual Environment

For activating a virtual environment we need to run a binary file by name activate inside the csm directory.

$ ~/python-virtual-env >> source csm/bin/activate

Deactivate Virtual Environment

For deactivating a python virtual environment and help in context switching.

$ deactivate

--

--