Working with Virtual Environment in Python

Learning to use virtual environments in Python is one of the most productive things I’ve learned which has a near-zero learning curve. In fact, learning a few commands will let us install multiple versions of the same library in different projects without any hassle. Virtual environment for Python can be created and managed using the module venv or virtualenv. We will be looking into how to venv as it is a standard library.

Creating a new virtual environment

python3 -m venv desired_env_name

Run this command on your project folder, replace desired_env_name with any desired name, all packages for the current project is going to be installed into that directory.

Activating the virtual environment

source desired_env_name/bin/activate

Once inside the virtualenv, use pip install normally to install packages inside the virtual environment. Packages installed while this virtual environment is activated stays inside desired_env_name and running our project(python3 your_project_name) while it is activated will ensure that the project utilizes libraries in the current virtual environment.

Exiting virtual environment

exit


programminggnu/linux
pythonbash