I put my Flask project on the back burner during a busy month while I attended to few other projects, one of which involved upgrading Python to version 3.7.

When I returned to development I noticed that the virtual environment I had created was no longer working. This short post is about setting up a virtual environment for a python project.

Since I was careful to save all the dependant modules the agile estimator uses in a file requirements.txt it will be easy to restore things to the way they were.

Why Use a Virtual Environment

Python’s dependency management is not as mature as other programming languages. Java has Maven, for example, which is good at keeping things in order, but even modest sized applications can have hundreds of nested dependencies.

When using pip to install modules with a virtual environment, it will place them where they be accessed by any application. Things will quickly get complicated if different versions of the same module are required by different projects.

This is especially a problem if you are downloading and trying to run someone else’s code – they may have written it with completely different versions to those you have installed.

That’s were venv steps in. By creating and then activating a virtual environment, everything installed with pip will be placed locally in the environment’s segregated area. Switching between projects is as easy as deactivating the first and activating the second.

Creating a Virtual Environment

First consider where to place the folder. It should not be a part of the committed source code. If you place it alongside your code, make sure to add a setting to ignore it on check in. For Git this goes in the .gitignore file at the base of the module.

I prefer to place the folder up one level, alongside the module’s code. Within my projects folder I first create a container for the whole project, within which sits the virtual environment and the source code.


To create the environment run python3 -m venv env-estimator and it will create the folder and everything you need to get started.

Activating and Deactivating

To activate the environment from the estimator folder, run . ../env-agile-estimator/bin/activate and notice that the bash prompt will change to show that you are now in a virtual environment.

I use a MacBook Air, so the default version of Python is 2.7.10. By specifying to create the environment using python3 and leaving the defaults as they are, my new environment will use version 3. So in my activated environment, I no longer need to add the "3" to the command.

Ronans-MacBook-Air:estimator rkilleen$ python --version
Python 2.7.10
Ronans-MacBook-Air:estimator rkilleen$ . ../env-estimator/bin/activate
(env-estimator) Ronans-MacBook-Air:estimator rkilleen$ python --version
Python 3.7.2
(env-estimator) Ronans-MacBook-Air:estimator rkilleen$ deactivate 
Ronans-MacBook-Air:estimator rkilleen$ python --version
Python 2.7.10


The Python application is not the only thing that is activated in the new environment, pip is also now pointing at the correct version so no need to type pip3.

I saved the dependencies into a file call requirements.txt so it is easy to get things set up just like they were with:

pip install -r requirements.txt 

If you are wondering where all the installed packages go, you can find them under the folder created for the environment. In my case this is env-estimator/lib/python3.7/site-packages/.

That’s all for now. Since I now have my code back in order, I can get back into development of my agile estimation tool.