Virtual Environments in Python Applications
Tags: env, venv, python, pyenv
Published: 2024-04-11 19:19:05

Virtual environments are a vital part of modern application development, regardless of the stack you are using. Virtual environments avoid problems related to dependency and version incompatibility. In this guide, I will show you three popular ways to use virtual environments in Python.

---

## Venv
Python Venv is a built-in tool to create and manage lightweight virtual environments. 

> For more information visit: [venv tutorial](https://docs.python.org/3/tutorial/venv.html).

### Installation:
Already installed on MacOS and Windows platforms, but needs to be installed on some Linux distros, here is an installation guide for different package managers:

```bash
sudo apt install python3-env # using apt
sudo dnf install python3-env # using dnf
sudo pacman -S python3-env # using pacman
```

### Creating a Virtual Environment:
```bash
python -m venv <env_name> # Unix-like systems
python -m venv <env_name> # windows
```

### Activating the Virtual Environment:

- On Unix-like systems (Unix, Linux, or MacOS):
```bash
source <env_name>/bin/activate
```

- On Windows:
```bash
<env_name>\Scripts\activate
```

### Deactivating the Virtual Environment:
```bash
deactivate
```

---

## Virtualenv

virtualenv is a simple to use tool to create isolated Python environments.

For more information visit: [virtualenv documentation](https://virtualenv.pypa.io/).

### Installation:
```bash
pip install virtualenv
```

### Creating a Virtual Environment:
```bash
virtualenv <env_name>
```

### Activating the Virtual Environment:
- On Unix-like systems (Unix, Linux, or MacOS):
```bash
source <env_name>/bin/activate
```

- On Windows:
```bash
<env_name>\Scripts\activate
```

### Deactivating the Virtual Environment:
```bash
deactivate
```

---

## Pyenv (Unix, Linux or MacOS only)

Pyenv is a popular option in Python applications. Pyenv allows you to manage different versions of Python in your application and also on your system.

Pyenv does not officially support Windows but you can use WSL (Windows Subsystem for Linux).

> For more information visit: [pyenv repository](https://github.com/pyenv/pyenv)

### Installation:
You can install using Homebrew or make a manual installation:

- **Using Homebrew**:
    ```bash     
    brew install pyenv
    ```

- **Manual Installation**:
    - Download pyenv:    
    ```bash
    curl https://pyenv.run | bash    
    ```

    - Let's edit your bash profile:    
    ```bash
    nano .bashrc
    ```

    - Add these lines in the end of the document:    
    ```bash
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"
    ```

### Creating a Virtual Environment:
```bash
pyenv virtualenv <python_version> <env_name>
```

### Activating the Virtual Environment:
```bash
pyenv activate <env_name>
```

### Deactivating the Virtual Environment:
```bash
pyenv deactivate
```

### Managing Python Versions:

Pyenv allows you to manage python versions, here is a simple tutorial that you can use in your application or in your input system:

    
- List available versions: 
```bash
pyenv install -list
```

- Install a version:
```bash
pyenv install <version>
```

- Defining a version: 
```bash
pyenv global <version> # set a global version
pyenv local <version> # set a local version
```

## Conclusion:

The tools are very similar, the biggest difference is in the installation process and in some specific features, such as python version management (pyenv), you can choose according to your taste or personal preference, I recommend that you test each one of them and see that best suits your project.