How to use the command venv (with examples)
The venv
command is used to create lightweight virtual environments in Python. Virtual environments allow you to isolate your Python project dependencies from your system, making it easier to manage and distribute your code. You can create a virtual environment using the python -m venv
command and then activate and deactivate it as needed.
Use case 1: Create a python virtual environment
Code:
python -m venv path/to/virtual_environment
Motivation: Creating a virtual environment is useful when you want to work on a Python project without interference from system-level packages. It ensures that your project dependencies are installed locally and prevents conflicts between different projects.
Explanation:
python
: The Python interpreter executable.-m venv
: The-m
flag tells Python to run a library module as a script.venv
is the library module responsible for creating virtual environments.path/to/virtual_environment
: The path where you want to create the virtual environment. Replace with the desired path on your system.
Example output:
path/to/virtual_environment/
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── python -> /usr/bin/python
│ └── python3 -> python
├── include
├── lib
│ └── python3.9
│ └── site-packages
└── pyvenv.cfg
Use case 2: Activate the virtual environment (Linux and Mac OS)
Code:
source path/to/virtual_environment/bin/activate
Motivation: Activating the virtual environment is necessary to set up the correct Python interpreter and environment variables for your project. It ensures that when you run Python, it uses the packages installed within the virtual environment.
Explanation:
source
: Thesource
command is used to execute the script in the current shell session instead of spawning a subshell. This ensures that the virtual environment settings are applied to the current shell session.path/to/virtual_environment/bin/activate
: The path to theactivate
script within the virtual environment. Replace with the actual path on your system.
Example output:
(virtual_environment) $
Use case 3: Activate the virtual environment (Windows)
Code:
path\to\virtual_environment\Scripts\activate.bat
Motivation: Windows uses a different file extension for scripts compared to Linux and Mac OS. Hence, the activation command is different for Windows systems.
Explanation:
path\to\virtual_environment\Scripts\activate.bat
: The path to theactivate.bat
script within the virtual environment. Replace with the actual path on your system.
Example output:
(virtual_environment) C:\>
Use case 4: Deactivate the virtual environment
Code:
deactivate
Motivation: Once you have finished working in the virtual environment, it is essential to deactivate it. This restores your system’s Python environment and removes any changes made within the virtual environment.
Explanation:
deactivate
: The command to deactivate the current virtual environment.
Example output: (no output)
Conclusion:
In this article, we covered the various use cases of the venv
command. We learned how to create a Python virtual environment, activate it on different operating systems, and deactivate it when finished. Using virtual environments can greatly simplify the management of project dependencies and ensure that your Python code runs consistently across different systems.