Contents

How to use python virtual environments with Git

This article explains how to set and properly use a virtual environment with source control.

What’s a virtual environment

Configuring a virtual environement is an important step in the development proccess. A virtual environment is a tool that allows us to keep the dependencies of different projects seperated by creating isolated environment for each project.

Steps

First, set up a new Github repo and make sure to include the Python .gitigonre file.

/posts/my-new-post/git.png

Then, clone the repo to your local machine

git clone project_url

Go inside the newly created folder and do the following:

Note: This guide is specific to macOS. How to create and activate the virtual environment on Windows 10 is different.

  1. Create a Python 3 virtual environment in a folder named “venv”: $ python3 -m venv venv You can acctually name your virtual environment as you like but using “venv” is a convention. Alsom venv has been included in the default Github .gitingore file so that you won’t accidentally push the virtual environment folder to Github.

  2. Activate the virtual environment: $ source venv/bin/activate

  3. Create a requirement.txt file and add the packages you need for the project.

  4. You can install the packages from the requirements file $ pip install -r requirements.txt

  5. You can exit from the virtual environment with $ deactivate