How to use virtualenv

What is covered in this tutorial.

  • Introduction
  • Installing pip
  • Installing virtualenv
  • Activate, Install modules & Deactivate

Introduction

What is pip?

Pip is a package management system used to install and manage python modules. Once pip is installed it can be used to install/manage versions/uninstall software written in python.

What is virtualenv?

Different application requires different python versions. And, not all python modules are available for all python versions. Your default python version may be chosen by OS provider. Maintaining different versions is a tedious task. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It’s also possible that changes to the system Python will break other OS features that depend on it.

Virtual environments, or “virtualenvs” are lightweight, self-contained Python installations, designed to ease the developers work without requiring extensive configuration or specialized knowledge.

virtualenv sets up a local environment for installing pip packages. By activating package installation will be sandboxed and will not effect any global installations or other virtual environments.

Installing pip

#Debian and most other distributions include a python-pip package
apt-get install python-pip
#or 
python3 -m pip install --user --upgrade pip

Installing virtualenv

pip install --user virtualenv

Create, Activate, Install modules & Deactivate

Creating new virtual env

virtualenv mypython

Activating the created virtual env

#This will call the "mypython environment"
source mypython/bin/activate

Install new modules

#This installtion will effect locally
#e.g. 
pip install py2neo==2.0
#If need to install multiple packages at once
#Have them listed in a text file 
e.g. 
numpy==1.15.0 
scipy==1.1.0 
#
pip install -r <text-file> 

Deactivate the virtual env

#Disables local environment and enables Global environment
deactivate

1 Comment on "How to use virtualenv"

  1. Hello! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your articles. Susanna Cosimo Boyden

Leave a comment

Your email address will not be published.


*