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.
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.
-
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. -
Activate the virtual environment:
$ source venv/bin/activate
-
Create a
requirement.txt
file and add the packages you need for the project. -
You can install the packages from the requirements file
$ pip install -r requirements.txt
-
You can exit from the virtual environment with
$ deactivate