# 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

![](/files/FlPURoV30JZK62YFxEld)

#### 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

![](/files/diowpEmJy1Utrs36SHJi)

## 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

![](/files/ZBxm1z9MwhYGwfx66jJV)

* Go to Select interpreter&#x20;

![](/files/zxhYj3ZhemHW0rD4Mwek)

* 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/python/virtual-environments-venv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
