A virtual environment is a lightweight Python installation with its own package directories and a Python binary copied (or linked) from the binary used to create the environment.
In Python 2.7 we need a module called
virtualenv which is installed using PIP to get started with Python virtual environments. You can install it with
pip install virtualenv
In Python 3 the
venv module comes pre-shipped as a part of the standard library.
Create Virtual environments
Create a directory for your virtual environment and cd into it. python_envs is my directory name.
python3 -m venv python_envs/your_env
Here is what each folder in the structure contains:
- Include: This folder contains C headers to compile the Python packages.
- Lib: This folder contains a copy of the Python version and all the other third-party modules
- Scripts: Files to interact with the virtual environment.
Now after creating a virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command:
After your work, to deactivate the virtual environment, you need to execute the following command: