Using virtualenv and pipenv tools
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).
$ pip install virtualenv
We create a sample project file as our working directory.
$ mkdir demoproject
$ cd demoproject
We are now inside the demoproject directory. We will create a virtual environment using the following command.
$ virtualenv venv_demo
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.
$ source venv_demo/bin/activate
Once the virtual environment is activated, its name is displayed in the terminal as below:
We can now install packages.
$ python -m pip install pandas
We now have pandas installed in our virtual environment. The freeze command shows the list of installed packages.
$ python -m pip freeze
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.
$ python -m pip install pandas==1.0.5
If you just want to check the installed version of a particular package, the freeze command is used with grep:
$ pip freeze | grep pandas
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.
$ python -m pip install -r requirements.txt
In order to exit the virtual environment, we use the deactivate command.