Python venv (Virtual Environment) is a tool that comes bundled with Python 3.3 and later versions. It allows you to create isolated environments for Python projects.
To create a virtual environment you need to use the python venv
module
python3.9 -m venv myenv
We need to need to activate the environment variable so that when we run Python or install packages, it happens within the virtual environment.
- For windows run the below command
myenv\Scripts\activate
- For Linux/Mac run the below command
source myenv/bin/activate
- Python commands we run will use the Python interpreter and packages installed within that environment.
- We can install packages using
pip
, and they will be installed only within the virtual environment. - Does not affect your global Python installation.
When you're done working within the virtual environment, you can deactivate it with the following command:
deactivate