# 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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FJqywG5BJ5sMdq9mLMt12%2Fimage.png?alt=media\&token=9b15b00b-b28e-4d7e-8402-65b1b87b46bc)

#### This is where VENVs come into play

* This is an isolated environment that will not be affected by any other changes&#x20;
* It is completely isolated to this project

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FwgECuKGqwlaHllge36Cw%2Fimage.png?alt=media\&token=0d9f0965-001a-497a-a103-003d33be324f)

## Creating a Virtual Environment

* Navigate to the folder where you want to create the VENV
* Use the following command to set up the VENV

```bash
# For Windows:
py -3 -m venv <venv_name>

# Example:
py -3 -m venv venv

# For Unix/Linux:
python3 -m venv <name>

# Example:
python3 -m venv venv
```

### Setting up VENV

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

```bash
/path/to/project/venv_name/Scripts/python.exe
```

* In our case we will be using VSCode
* Go to View --> Command Palette

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2Fh1xAK6P8hYPE5LgPiGV0%2Fimage.png?alt=media\&token=f0570e1f-2b72-40aa-a38a-111b40230068)

* Go to Select interpreter&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FSAMNhe19aWMYKH916lNA%2Fimage.png?alt=media\&token=a1fa9965-857b-4a90-9252-09d8cadc5ac1)

* Enter the path to the interpreter

```bash
# For Windows:
/path/to/project/venv_name/Scripts/python.exe

# For Unix/Linux:
/path/to/project/venv_name/bin/python

# or
/path/to/project/venv_name/bin/python3
```

{% hint style="info" %}
This should be remembered every time you enter the project, but if it doesn't, repeat this process
{% endhint %}

#### Set up VENV to be used in Terminal

* Go to the project folder
* Enter the following line:

```bash
# For Windows CMD:
venv/Scripts/activate.bat

# For Windows Powershell:
venv/Scripts/Activate.ps1

# For Linux
source venv/bin/activate
# Note that you need to run this command every time
# you open the terminal
```

{% hint style="info" %}
Note: If done correctly you should have a <mark style="color:green;">`(venv)`</mark>at the beginning of the terminal line
{% endhint %}

{% hint style="warning" %}
This will not work if you are using Git Bash for Windows
{% endhint %}

### Set up requirements.txt

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

```bash
pip freeze > requirements.txt
```

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

```bash
pip install -r requirements.txt
```
