Pirobits
  

Python pipenv tutorial: manage dependencies like a pro

alberto avatar Alberto Sola · 3/19/2024

When you're working with Python, you need to install packages and manage dependencies. If you have several projects, dependencies can clash and cause problems. I'll tell you how pipenv solves this issue.

Introduction

When you're working on a Python project and need to install a package, you typically run python3 -m pip install <package>. What happens if two projects depend on different versions of the same package? Problems can arise.

This is where "virtual envs" or "venv" were created, allowing you to have different versions of Python with their respective packages on the same computer.

In this case, pipenv is a quite popular tool that allows you to manage Python virtual environments on your computer, similar to npm in the Node ecosystem.

I'll explain the basic concepts so you can use it too.

Tutorial: how to use pipenv

First, install pipenv with the following command:

python3 -m pip install pipenv

With this, you can use the pipenv command to manage environments. Keep reading!

Now, the basic commands you need to know:

Install dependencies

This command creates a virtual environment if one doesn't exist, so you can launch it directly.

pipenv install <package>

Activate a virtual environment

pipenv shell

Once inside, you can run:

python file.py

Run a file

pipenv run python file.py

Create a virtual environment

pipenv --python <version>

Delete a virtual environment

pipenv --rm

Virtualenv

There's another lower-level tool called virtualenv that allows you to create directories containing all the dependencies to run the project with a specific python version.

Here's a link with all the documentation for the tool.

Summary

In my case, I use pipenv when needed since it's the most convenient. This reminds me of when I started working where we had different projects, all with Python, virtualenv, and docker.

Did you find this article useful?Join the community to receive exclusive content!

Learn about product creation and software development

Exclusive Content

Transformative Knowledge

I write in-depth articles about software development and product creation.

Real Experience

I share what I learn in my day-to-day work as a software engineer and tech lead.

Straight to the Point

I value your time and send you, at most, one email a week with a summary of the most important news.

Would you like to join?

I value your privacy, I only send quality content.

We are already more than 50 professionals

The most recent posts I wrote in the blog