Python Virtual Environments

By Matthew Harris

About Me

I currently work for AnnuityCheck, a Financial Technology company.

Prior to that, I worked for the International Rescue Committee, creating reports for the United Nations (UNHCR) and US State Dept.

What is python virtual environment

A seperate python 'installation' designed to work with an individual project

Simular Ideas in other languages: Node: npm, Ruby: RVM, golang: workspace, java: Maven (or many others).

The concept of Package Management is universal, the implmentation changes by language

Major Advantages

  • Work on Multiple Projects without fear
  • Keep packages up to date
  • Code Sharing

Freeze!

pip freeze > requirements.txt

This is important, anytime you add / remove a package, refreeze

How to Install (windows)

Install Python (exe)

Fix permissions; open powershell as admin and run:

Set-ExecutionPolicy Unrestricted -Force

How to Install (mac)

Install Python (homebrew), and its auto installed!

How to Install (linux)

$ sudo apt install python3

$ sudo apt install python3-venv

Live Demo!

Lets create a virtual env and use it for great good

Win/Mac: python -m venv venv
Linux: python3 -m venv venv
Win: ./venv/Script/activate
Mac/Linux: . venv/bin/activate
All: deactivate

Common Pitfalls

Dont move the virtual env, just recreate

Dont commit the virtual env, be sure to add venv to .gitingore (using a standard .gitignore, it's in there)

Dont delete version numbers from the requirements.txt file