# Virtual Environment

You can consult some [helpful Python docs](https://docs.python.org/3/tutorial/venv.html) for more information!&#x20;

{% hint style="info" %}
In the following code blocks, the dollar sign `$` just means you should be typing what follows in your terminal/command prompt!
{% endhint %}

## 1. Install `virtualenv`

Navigate to the directory that you want to be coding in, and make sure you have `virtualenv`installed

```
$ pip install virtualenv
```

## 2. Create a `venv` folder

You could actually name this anything you want, but it's good practice to call it `venv`

You create this folder so you can activate later to essentially "enter your virtual environment"

```
$ python3 -m venv <name of virtual environment>
```

## 3. Activate the `venv` folder&#x20;

You can see if it's been created with `ls` for MacOS or `dir` for Windows

**MacOS** command to activate virtual environment:

```
$ . venv/bin/activate
```

**Windows** command to activate virtual environment:

```
$ .\venv\Scripts\Activate.ps1
```

If you are getting an error that reads: `"cannot be loaded because the execution of scripts is disabled on this system".`, close PowerShell (if open), look up PowerShell on Windows search, click on "run as administrator", then run the command below:

```
$ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
```

If you are curious as to what exactly this is doing to your computer, it is allowing the current user to run scripts. `RemoteSigned` is one of many execution policies which you can read about [here](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2#powershell-execution-policies), and `CurrentUser` is one of many scopes which you can read about [here](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2#execution-policy-scope).

If you're still getting an error, try running these commands on PowerShell instead of Command Prompt.

## 4. Install `requirements.txt`

Now that you're in the environment, your terminal should have a `(venv)` next to your cursor, so you can now install the requirements to setup your virtual environment

```
(venv) $ pip3 install -r requirements.txt
```

Make sure `requirements.txt` exists where you are running this command! (again, you can check this using `ls`/`dir` to list everything in your current directory).

If you are curious, `-r` is a flag that installs the requirements *recursively* to install all requirements defined in `requirements.txt`.3&#x20;

## 5. Run your code!

Now you can run code with the dependencies that were installed by Step 4.&#x20;

Here's just an example:

```
(venv) $ python3 <YOUR FILE NAME>.py 
```

## 6. If you're done, deactivate

If you're done developing for your project for the time being, or if you want to work on another project that has another virtual environment, you can deactivate the one that you're currently in

```
(venv) $ deactivate
$ 
```

{% hint style="info" %}

## What commands to use and when to use them

* Steps **#1, 2, and 4** are only done once for every new project you'll be creating a virtual environment for.&#x20;
* Steps **#3, 5, and 6** are what you'll be using every time after that to use your virtual environment!
  {% endhint %}

## Common Errors

### no such file or directory: `requirements.txt`

There are two issues that cause this problem:

1. You are not in the directory where `requirements.txt` exists. You'll know this is the case if enter `ls` (MacOS) or `dir` (Windows) into your terminal and you don't see `requirements.txt` or if you didn't spell it right in your command.
2. You are trying to do `pip3 install -r requirements.txt` inside of a correctly created and activated `venv`, but **your `venv` is nested within a path that contains a folder that has a space in the name**. Here's a good [Stack Overflow post](https://stackoverflow.com/questions/7911003/cant-install-via-pip-with-virtualenv) that explains the issue.


---

# 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://backend-course.cornellappdev.com/cheat-sheets/virtual-environment.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.
