Python Virtual Environment

PythonServer Side ProgrammingProgramming

Introduction

Developers often deal with python projects where they have to use module and packages which were not part of the python standard library and they need it for this particular application only. Consider a case, where you have installed the current version of python (let’s assume its python-3.6) but your project requires specific python version--2.7 for this particular application, so it's not just the new packages but requirement may come for a particular version of your already installed application. Then the requirements are in conflict and installing either version 2.7 or 3.6 will leave one application unable to run.

One solution to resolve these circumstances is to create a virtual environment for your project. Virutalenv is a kind of tool that allows us to create isolated python environments. Virtualenv creates a self-contained folder which contains all the required executables to use the packages that a Python project would require in its project.

Why we want it?

The main purpose of using virtualenv (virutal environment tool) is to resolve the issues of dependencies, versions (of python packages) and indirectly permissions.

Requirements for installing Virtual Environment

First thing first, you need to have the python installed in your machine (not necessarily the latest version) and pip package manager. However, if you are using python 3.4 version or higher, pip is included by default (comes as python standard library). In case you don’t have any one of them, it’s recommended to installing it first.

Creating Virtual Environments

Open your command prompt(type cmd in your run terminal). Now go to the directory path(location), where you want to install the virtual environment.

In case you are not using python 3.x, then you need to install the virtualenv tool with pip.

Shell

pip install virtualenv

In case virtualenv is already installed (either you are using python 3 version or you’re not aware of virtualenv installed already), then running above command will give you message something like,

>pip install virtualenv
Requirement already satisfied: virtualenv in c:\python\python361\lib\site-packages (15.1.0)

Start by creating a new folder to work with

Shell

mkdir python-virtual-environments && cd python-virtual-environments

After running above command, you are inside the newly created folder. Now create a new virtual environment inside the directory (the directory you created above).

Shell

#For python 2.x version −

virtualenv myenv

# For python 3.x version −

python -m venv myenv

Above script will create a new folder name myenv with a couple of directories and lots of files, with a directory structure similar to below -

├── Include
│ ├── abstract.h
│ ├── accu.h
│ ├── asdl.h
│ ├── ast.h
│ ├── bitset.h
………
├── Lib
│ ├── __future__.py
│ ├── __pycache__
│ ├── _bootlocale.py
│ ├── _collections_abc.py
│ ├── _dummy_thread.py
│ ├── _weakrefset.py
│ ├── abc.py
│ ├── base64.py
│ ├── bisect.py
│ ├── codecs.py
……
├── pip-selfcheck.json
├── Scripts
│ ├── activate
│ ├── activate.bat
│ ├── activate.ps1
│ ├── activate_this.py
│ ├── deactivate.bat
│ ├── easy_install.exe
│ ├── easy_install-3.6.exe
│ ├── pip.exe
│ ├── pip3.6.exe
│ ├── pip3.exe
│ ├── python.exe
│ ├── python36.dll
│ ├── pythonw.exe
│ └── wheel.exe

Where −

  • Include (directory): C headers that compile the python package

  • Scripts(directory): files that interact with the virtual environment

  • Lib(directory): Contains the python version copy and site-packages directory where each dependency is installed.

How to activate the virtual environment?

One of the interesting files is the activate scripts in the scripts directory. The activate scripts used the environment’s python executables and its site-package by default to set up your shell.

However, to use this virtual environment “myenv” packages or resources in isolation, you need to “activate” it first. To activate your virtual environment run the command as shown in the screenshot.

Once the virtual environment is active, we can install all the project related packages and other dependencies isolated from the outside world. For example, if we are working with a data science project, we can install all the required packages and their dependencies at once simply, by typing below command like below −

(myenv) C:\Users\rajesh\python-virtual-environments>pip install numpy scipy matplotlib ipython jupyter pandas

Once we are done with our project, we can come out of the virtual environment simply by deactivating it.

(myenv) C:\Users\rajesh\python-virtual-environments>deactivate
C:\Users\rajesh\python-virtual-environments>

Now we are back in the windows command shell (like above).

raja
Published on 19-Feb-2019 12:15:18
Advertisements