This guide will walk you through creating and using Python's built-in virtual environment (venv) for isolating project dependencies.
venv is a tool that allows you to create isolated environments for your Python projects. Each virtual environment has its own Python executable and can have its own set of libraries, independent of the system-wide Python installation. This helps prevent version conflicts between different projects.
- Python 3.3+:
venvis included in Python 3.3 and later. - pip: The Python package installer should be installed alongside Python.
Make sure you have Python 3 installed on your system. You can check by running:
python3 --version # macOS/Linux
python --version # WindowsInstall virtualenv
pip3 install virtualenv # macOS/Linux
pip install virtualenv # WindowsNavigate to your project directory and create a new virtual environment with the following command:
On MacOS
python3 -m venv venvOn Windows
python -m venv venvThis command creates a folder named venv in your project directory that contains a fresh Python environment.
Once your environment is created, you need to activate it:
On macOS/Linux:
source venv/bin/activateOn Windows:
venv\Scripts\activateWhen the virtual environment is activated, your shell prompt will change to show the name of the virtual environment, like so:
(venv) user@machine:path/to/project$Once the virtual environment is activated, you can install Python packages using pip:
On MacOS
pip3 install <package-name>On Windows
pip install <package-name>deactivate