Home Setting up Development environment with virtualenv
Post
Cancel

Setting up Development environment with virtualenv

Virtualenv is a Python module which helps to create isolated Python environments for our scripting experiments, which creates a folder with all necessary executable files and modules for a basic Python project.

Virtual environments help to separate dependencies required for different projects, by working inside a virtual environment it also helps to keep our global site-packages directory clean.

But before that we have to install pip3 package. Pip is a package management system used to install and manage software packages written in Python. pip3 is a python3 package manager.

Pip3 Installation

1
apt install python3-pip 

Now install virtualenv

1
pip3 install virtualenv 

Usage

  • Create folder project where you can write your code
1
2
mkdir mycode
cd mycode  
  • Now create virtualenv environment files inside project folder
1
virtualenv name-of-virtual-environment
1
virtualenv venv  
  • To create a virtual environment with a particular python version, use below command
1
virtualenv -p python2.7 venv
  • Activate the virtual environment by
1
2
3
4
5
source venv/bin/activate  

// or 

. venv/bin/activate  

Now you can install any dependencies and use it, which is isolated to this configuration only. To install new packages you can use pip or pip3 etc on this environment. For example

1
pip3 install package_name
  • Exit from the virtualenvironment, type
1
deacticate
This post is licensed under CC BY 4.0 by the author.