Python Virtual Environment (Python3) an Introduction
A Python virtual environment allows us to create separate environments for our projects. Each of our environments will only have the packages that it needs. This makes it easy to share our environment with others, have multiple versions of the same package installed on our machine for different projects without interfering with each other, and avoid unexpected side effects from installing packages that update or have dependencies on others.
Verify if virtual environment is already installed
There are chances that virtual environment is already installed on your system. To check this –
Open you terminal and enter the following command.
if you see message like “virtualenv 20.0.10 from …..” then you already have it installed.
Installing virtual environment
There are many ways to install Python Virtual environment, however, this article will list only one method to install it using pip3. To install, open your terminal and enter below command-
pip3 install virtualenv
Creating a new virtual environment
After installation of virtual environment you can enter below command to create a new virtual environment-
python3 -m venv helloEnv
After running this command, a directory named helloEnv will be created. This is the directory which contains all the necessary executables to use the packages that a Python project would need.
Activating virtual Environment
After you have created the environment, you can activate it by issuing below command-
Once the virtual environment is activated, the name of your virtual environment will appear on left side of terminal. This will let you know that the virtual environment is currently active. You can now install dependencies related to the project in this virtual environment. Any packages that are installed in the environment don’t exist outside the environment.
Make sure you activate your relevant environment every-time before working.
Deactivating virtual Environment
Once you are done with your work in a particular environment, you can deactivate the virtual environment by issuing following command: