Practical Guide for Virtual Environments in Python
Using virtualenv and pipenv tools
![Photo by Markus Spiske on Unsplash [2].](https://cdn.statically.io/img/assets.insightmediagroup.io/media/wp-content/uploads/2021/01/1ZZ2xQ8qo64nGyj05Wl6cCg-scaled.jpeg)
The projects we work on are very much likely to have many dependencies that need to be installed. These dependencies facilitate many tasks in projects. However, we need to be careful about them especially when working on multiple projects.
Just like any other technology, the software packages or programming languages are constantly being improved. Thus, new versions are being introduced.
Different projects might require different versions of a package or software. For instance, we might have one project that requires Python 2.7 and another one with Python 3.6. As the number of projects and dependencies increase, it becomes hard to follow up and handle such differences.
One way to overcome this issue is to use virtual environments. They can be considered as bounding boxes for software packages. We can develop a project in a virtual environment and install all the dependencies specific to that project. What we have in the virtual environment is not affected by the changes in the global scope of our machine.
There are many virtual environment tools for Python such as pipenv, virtualenv, venv, and so on. In this article, we will go over some examples using virtualenv and pipenv to get familiar with the idea of virtual environments and how they work.
Let's start with the virtualenv. We first install it from the terminal using python package installer (pip).
We create a sample project file as our working directory.
We are now inside the demoproject directory. We will create a virtual environment using the following command.
It's been created. We can run the ls command to see the files in the current working directory.
The next step is to activate the virtual environment.
Once the virtual environment is activated, its name is displayed in the terminal as below:

We can now install packages.
We now have pandas installed in our virtual environment. The freeze command shows the list of installed packages.
NumPy has also been installed because it is a dependency for Pandas. The installed version of Pandas is 1.1.5. We can specify the version we need while installing a package.
If you just want to check the installed version of a particular package, the freeze command is used with grep:
We can also install several packages saved in a text file. It is better than installing dependencies one-by-one especially when there are several of them.
In order to exit the virtual environment, we use the deactivate command.
The next tool we will discover is pipenv which can be installed using the pip:
Let's create a new virtual environment using pipenv.
Pipenv allows for installing a dependency while creating the virtual environment. For instance, I could have added pandas at the end of the command above so that the virtual environment is created with pandas installed.
We run the shell command to activate the virtual environment.

We are now in the virtual environment. Let's also install pandas to this one.
The graph command shows a detailed overview of the installed packages.
We can uninstall a specific package or all the packages in the virtual environment using the uninstall command.
The following command uninstalls all the packages.
We type the "exit" command to exit the virtual environment.
Conclusion
Virtual environments are great tools to manage multiple projects at the same time. There are numerous packages and libraries which are updated in no time. Thus, it is exhaustive and not efficient to try to manually follow up.
What we have covered in this article can be considered as a practical introduction to Python virtual environments. There is, of course, much more to learn both theoretically and practically.
The official documentation of virtualenv and pipenv provide more detailed overview about these tools.
Thank you for reading. Please let me know if you have any feedback.








