Python Virtual Environments.

Usman abdulsalam olanrewaju
2 min readMar 25, 2023

Smart way of isolating your project environments

image copyright @ www.https://dev.to/

Virtual environments are used in software development to create isolated environments with their own dependencies and configurations. This helps to avoid conflicts between different projects that may be using different versions of the same dependencies, and also makes it easier to replicate the development environment across different machines.

for example, a developer working on two different projects at the same time, and both project requires a particular library. let’s say Django, but one is using django version 2.2 while the other is using version 3.2. Conflicts will surely arise if both projects are being managed from the global environment created by pip (python default package manager).

How to setup a Virtual Enviroment

There quite number of python libraries that can be used in setting up your project Environment. My favorite is virtualenv because of its ease of use. so, you need to install the library.

pip install virtualenv

After installing virtualenv, navigate into your project folder and run the following command to create a virual environment.

virtualenv <env_name>

Activating the virtual Environment by typing the following command into your terminal.

<env_name>/Scripts/activate.bat

you’ve just succesfully activated the virtual environment, to confirm if it has been created check in to the terminal. your <env_name> should show like this

image showing that a virtual environment named ‘venv’ has been activated

So you can continue to install your project dependencies in to the virtual environment with it not going to affect another project.

To deactivate a virtual environment , just change the ‘activate.bat’ in the activating command to ‘deactivate.bat’

<env_name>/Scripts/deactivate.bat

for confirmation, the <env_name> at the beginning of the should not be there again

image showing that the virtual environment has been deactivated

so, you should start maintaing your python projects with seperate virtual environments.

Thanks for reading!! 😁😁😁

--

--