DEV Community

Cover image for Python Virtual Environments
Brandon Brown
Brandon Brown

Posted on

Python Virtual Environments

One constant struggle with development on multiple platforms or with multiple people is managing packages and dependencies. In order to have a stable product or application, it is extremely important to define and use the same dependencies and correct package versions when developing as part of a team or deploying an application. For example, let's say Bill and Todd are creating the next big web application. Bill has Django 2 installed on his computer while Todd uses Django 3. If Todd wrote some Django 3 specific code in the application, Bill's version of the application would break. Often when versioning is to blame, it can be very hard to troubleshoot the issue. Therefore, Bill could be scratching his head for hours before realizing that everything is correct and the only issue is his version of Django. Pipenv is a package managing package that helps solve this problem. Pipenv makes it extremely easy to add, remove, and manage packages while maintaining a virtual environment (more on this later) for your project. Now that we know what Pipenv is made for, let's get into how to use it.

Installing Pipenv

Just like any Python package, we can install Pipenv by using a simple Pip command:

pip3 install pipenv
Enter fullscreen mode Exit fullscreen mode

Check to make sure everything worked by running the following:

pipenv --version
Enter fullscreen mode Exit fullscreen mode

If you see a version printed out on the screen, you are good to start using Pipenv.

Pipenv Virtual Environment

Pipenv creates and manages a virtual environment for your packages. What this means is that Pipenv will run your code under an environment containing packages that you specify. For example, if you want to run Django version 2.0.6 for your project, however, you do not want to change your Django version on your computer, Pipenv can can help you do this. To break this down, let me show you through images.

Django not in Pipenv

As you can see above, I have Django installed on my computer (version 3.1.2).

Now let's say that we are working on an application as part of a team, and that application requires us to use Django 2.0.6. With Pipenv, we can create a virtual environment that allows us to using Django 2.0.6 without changing the Django installation on our actual computer.

Django in Pipenv

Isn't that cool? As shown above, we are in a virtual environment that I created called 'delete'. In that virtual environment, we are using Django 2.0.6.

Now that I hope you have an idea of the power of Pipenv, we can move on to how to use it.

Pipenv Virtual Environment Setup

To start a Pipenv virtual environment, navigate to the the directory you wish to house the virtual environment.

Important: Your Pipenv virtual environment must be in the same folder as your application or above it. Installing the virtual environment within your project is not typically a good idea.

When ready, run the following to create a virtual environment:

pipenv install
Enter fullscreen mode Exit fullscreen mode

This will create two files: Pipfile and Pipfile.lock. Pipfile shows the dependences in plain text. Pipfile.lock is an auto-generated file that is not intended to be edited. Pipfile.lock contains all the information your virtual environment needs to manage its dependencies.

Adding Packages to Our Virtual Environment

Fantastic. We now have a virtual environment to work with. Next, we want to install packages within the environment. For consistency, let's install Django 2.0.6. To do this, run the following in the same folder as your Pipfile and Pipfile.lock files:

pipenv install django==2.0.6
Enter fullscreen mode Exit fullscreen mode

You should receive something similar to the following:

Django 2.0.6 install

Now let's check to make sure it worked. First we must enter our virtual environment by typing:

pipenv shell
Enter fullscreen mode Exit fullscreen mode

This will add some parenthesis before your terminal command to let you know you are in your newly created virtual environment. We can then check the version of Django to verify we are using 2.0.6.

Pipenv Shell

If you got this far, congratulations. That is really it to managing and installing packages with Pipenv. Let's touch on one more thing that could make our lives a little easier when working as part of a team.

Requirements file (Easier Setup)

If you're working on an application as part of a team, you will likely have a list of dependencies needed for your project. If someone new comes on, this could be annoying to sit through and manually install all packages. For this reason, Pipenv allows you to create and use a simple txt file to install all your dependencies.

First, let's create the requirements.txt file. To do this, you must be in your virtual environment (run pipenv shell to enter virtual environment). Then run:

pipenv lock -r > requirements.txt
Enter fullscreen mode Exit fullscreen mode

This will create a new file called requirements.txt inside the same directory as your Pipfile. This houses all of the package requirements for your virtual environment.

Create requirements.txt

Now that we have that file created, we can use it to automatically install the all packages in another virtual environment. To do this, verify that the requirements.txt file is in your current directory and run:

pipenv install
Enter fullscreen mode Exit fullscreen mode

This will automatically install the package requirements as long as the requirements.txt file is found.

Installing with requiements.txt

As you can see, there were 9/9 packages installed. Now isn't that so much easier than manually installing each package?

Alternatively, if you would rather specify where your requirements file is, you can run the the same pipenv install command but by specifying the requirements file with the -r flag:

 pipenv install -r ./dev-requirements.txt
Enter fullscreen mode Exit fullscreen mode

Specifying requirements file

Again we have those 9 packages installed into our new virtual environment.

Conclusion

Pipenv is a great tool that can almost guarantee that an application will work across computers. With Pipenv, you no longer have to worry about who has what version of software or if anything is out of date. I hope I have convinced you of the power of Pipenv, and I hope you can find it useful. It may seem a little daunting at first to use, but you can quickly get the hang of it. Happy coding!

Top comments (0)