> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/prog/python/virtual-environments-venv.md).

# Virtual environments - venv

The `venv` module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their [`site`](https://docs.python.org/3/library/site.html#module-site) directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available.

When used from within a virtual environment, common installation tools such as [pip](https://pypi.org/project/pip/) will install Python packages into a virtual environment without needing to be told to do so explicitly.

A virtual environment is (amongst other things):

* Used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project (library or application). These are by default isolated from software in other virtual environments and Python interpreters and libraries installed in the operating system.
* Contained in a directory, conventionally named `.venv` or `venv` in the project directory, or under a container directory for lots of virtual environments, such as `~/.virtualenvs`.
* Not checked into source control systems such as Git.
* Considered as disposable – it should be simple to delete and recreate it from scratch. You don’t place any project code in the environment.
* Not considered as movable or copyable – you just recreate the same environment in the target location.

See [**PEP 405**](https://peps.python.org/pep-0405/) for more background on Python virtual environments.

## Activate a virtual environment

To activate (select to work with) a virtual environment

```bash
cd <dir_with_venv>
source ./bin/activate
```

The prompt will change to include the name of the virtual environment within parentheses

```bash
┌──(PwnTools)─(kali㉿kali)-[~/Python_venvs/PwnTools]
└─$ 
```

## Create a new virtual environment

### Install venv support

Make sure virtual environment support is installed

```bash
sudo apt install python3-venv
```

### Create virtual environment

Create a virtual environment with the current Python version

```bash
python3 -m venv <name_of_venv>
```

This will create a directory with the same name that includes the virtual environment.

### Create virtual environment with specific version

Make sure virtual environment support is installed for this specific version, in this case Python 3.11

```bash
sudo apt install python3.11-venv
```

Then create a virtual environment from that specific Python version

```bash
python3.11 -m venv My_venv
```

## Resources

Install older python versions in kali - GitHub Gist: <https://gist.github.com/whit3rabbit/43bd8d2fa26b415a0c8bad121bca2948>

Using EoL Python Versions on Kali: <https://www.kali.org/docs/general-use/using-eol-python-versions/>

venv module - Python: <https://docs.python.org/3/library/venv.html>
