Virtual Environments (venv)

What is a virtual environment

  • Let's say we have 2 Projects that use different versions of FastAPI

  • Let's say Project 2 uses a newer version of FastAPI than Project 1 and there are breaking changes between the 2 versions

  • You have a problem now where you need 2 different versions of the FastAPI framework and you are not able to upgrade

This is where VENVs come into play

  • This is an isolated environment that will not be affected by any other changes

  • It is completely isolated to this project

Creating a Virtual Environment

  • Navigate to the folder where you want to create the VENV

  • Use the following command to set up the VENV

Setting up VENV

Now that you have created the VENV you will need to select the interpreter located in:

  • In our case we will be using VSCode

  • Go to View --> Command Palette

  • Go to Select interpreter

  • Enter the path to the interpreter

circle-info

This should be remembered every time you enter the project, but if it doesn't, repeat this process

Set up VENV to be used in Terminal

  • Go to the project folder

  • Enter the following line:

circle-info

Note: If done correctly you should have a (venv)at the beginning of the terminal line

circle-exclamation

Set up requirements.txt

  • Once you have installed everything that you need to get started within your VENV use the following command

  • If you are on another machine and require the installation of all dependencies use:

Last updated