How to Setup a Virtual Environment for Python Development

Ikechuku Ukpa
2 min readAug 25, 2019

What is a virtual environment and why do I need it?

You can think of virtual environment as an isolated container or sandbox where everything installed exists only in that container. In other words whatever packages or libraries you install in one virtual environment will not be available outside that virtual environment (you can liken it to variables and variable scopes). More on virtual environments here

How to setup (For Windows)

  1. Download and install python version 3+ (when installing python, make sure to check all the check boxes)
  2. Install virtualenv and virtualenvwrapper
pip install virtualenvwrapper-win

You are now setup to utilize virtual environments

  • to create a new virtual environment
mkvirtualenv myenv
  • to switch to a virtual environment
workon myenv
  • to exit from a virtual environment
deactivate

How to setup (For Ubuntu)

  1. Make sure python 3+ and pip is installed

Ubuntu comes pre-installed with python 2.7. To install the latest version of python run the following command

sudo apt install python3

2. Install pip

sudo apt install python3-pip

3. Install virtualenv and Virtualenvwrapper

sudo pip install virtualenv && virtualenvwrapper

4. Create a directory in your root folder to store all virtual environments

mkdir ~/.virtualenvs

and then point WORKON_HOME to it.

export WORKON_HOME=~/.virtualenvs

5. Edit the~/.bashrc so that the virtualenvwrapper commands are loaded. Use the nano editor to open the ~/.bashrc file

nano ~/.bashrc

scroll to the bottom of the file and add the following line to the end of the file

. /usr/local/bin/virtualenvwrapper.sh

To save the file, press ctr + o (and then enter to confirm save)

then press ctr + x to exit the editor

You are now setup to utilize virtual environments. (the commands to create, switch and exit are the same as windows’ above)

--

--