← Back

Venv and Virualenv

Pythonvenvvirualenv
Published:


Extra Information

If you’re not using an interactive shell (e.g. cron job), you can use the environment by using the Python interpreter in the virtual environment. <path_to_env_folder>/bin/python <script.py>

Venv

venv is the standard tool for creating virtual environments in Python. It is included in the Python standard library since Python 3.3.

Uses the currently running Python interpreter to create the virtual environment.

Might need to install python3-venv package on Linux.

Create a virtual environment

python -m venv <your_env_name>

# Linux/macOS
source <your_env_name>/bin/activate

# Windows CMD
<your_env_name>\Scripts\activate.bat

# Windows PowerShell
# Need to set the execution policy to unrestricted.
# Set-ExecutionPolicy Unrestricted -Scope Process
<your_env_name>\Scripts\Activate.ps1

Virualenv

A more feature-rich library for creating virtual environments in Python than venv. E.g. it allows you to create virtual environments with different Python versions. (That is installed on your system)

Installing virtualenv

pip install virtualenv

Create a virtual environment

virtualenv <your_env_name>

# Linux/macOS
source <your_env_name>/bin/activate

# Windows CMD
<your_env_name>\Scripts\activate.bat

# Windows PowerShell
# Need to set the execution policy to unrestricted.
# Set-ExecutionPolicy Unrestricted -Scope Process
<your_env_name>\Scripts\Activate.ps1