How to manage your Python projects with Pipenv (2024)

We are well known for our work with Ruby and Rails here at thoughtbot, butgenerally we always try to use the most appropriate language or framework forthe problem at hand. With that, I’ve recently beenexploring machine learning techniques so I’ve been working a lot morein Python.

One of the big differences between working on Ruby projects and Python projectsis the way that dependencies are typically managed. There currently isn’tanything similar to Bundler or Gemfiles in the Pythonuniverse so usually a Python developer will create a virtual environmentusing Virtualenv, and then annotate a requirements.txt text file witha list of dependent packages, which they can then install using Pip.

This approach works fine but sometimes it can be a juggling act, as you have tomanually install or remove packages with particular versions, and remember toregularly update the requirements.txt file, in order to keep the projectenvironment consistent. Especially when there are Python packages you wantinstalled in your virtual environment, but not necessarily associated with theproject itself. Moreover, some projects sometimes maintain two versions of therequirements.txt file – one for the development environment and one for theproduction environment – which can lead to further complications.

Fortunately Kenneth Reitz’s latest tool, Pipenv, serves tosimplify the management of dependencies in Python-based projects. It bringstogether Pip, Pipfile and Virtualenv to provide astraightforward and powerful command line tool.

Getting started

Begin by using pip to install Pipenv and its dependencies,

pip install pipenv

Then change directory to the folder containing your Python project andinitiate Pipenv,

cd my_projectpipenv install

This will create two new files, Pipfile and Pipfile.lock, in your projectdirectory, and a new virtual environment for your project if it doesn’t existalready. If you add the --two or --three flags to that last command above,it will initialise your project to use Python 2 or 3, respectively. Otherwisethe default version of Python will be used.

Managing Python dependencies

Pipfiles contain information about the dependencies of your project,and supercede the requirements.txt file that is typically used in Pythonprojects. If you’ve initiated Pipenv in a project with an existingrequirements.txt file, you should install all the packages listed in that fileusing Pipenv, before removing it from the project.

To install a Python package for your project use the install keyword. Forexample,

pipenv install beautifulsoup4

will install the current version of the Beautiful Soup package. A packagecan be removed in a similar way with the uninstall keyword,

pipenv uninstall beautifulsoup4

The package name, together with its version and a list of its own dependencies,can be frozen by updating the Pipfile.lock. This is done using the lockkeyword,

pipenv lock

It’s worth adding the Pipfiles to your Git repository, so that ifanother user were to clone the repository, all they would have to do isinstall Pipenv on their system and then type,

Then Pipenv would automagically locate the Pipfiles, createa new virtual environment and install the necessary packages.

Managing your development environment

There are usually some Python packages that are only required in yourdevelopment environment and not in your production environment, suchas unit testing packages. Pipenv will let you keep the twoenvironments separate using the --dev flag. For example,

pipenv install --dev nose2

will install nose2, but will also associate it as a package that is onlyrequired in your development environment. This is useful because now, if youwere to install your project in your production environment with,

pipenv install

the nose2 package won’t be installed by default. However, if another developerwere to clone your project into their own development environment, they coulduse the --dev flag,

pipenv install --dev

and install all the dependencies, including the development packages.

Running your code

In order to activate the virtual environment associated with your Python projectyou can simply use the shell keyword,

pipenv shell

You can also invoke shell commands in your virtual environment, withoutexplicitly activating it first, by using the run keyword. For example,

pipenv run which python

will run the which python command in your virtual environment, and display thepath where the python executable, that is associated with your virtualenvironment, is located. This feature is a neat way of running your own Pythoncode in the virtual environment,

pipenv run python my_project.py

If you’re like me and shudder at having to type so much every time you want torun Python, you can always set up an alias in your shell, such as,

alias prp="pipenv run python"

Keeping it simple

I hope this post has shown you how to manage your Python projectswith Pipenv. It has been around for less than a month now, so I, forone, will be interested to see how it develops over time. I certainly don’twant, or expect, it to become exactly like Bundler for Ruby, but I’lldefinitely champion it for simplifying the management of dependencies in Pythonprojects. I hope you do too!

How to manage your Python projects with Pipenv (2024)

FAQs

How to use pipenv on an existing project? ›

Migrating an Existing Project to Pipenv

Make sure your project folder has a requirements. txt file in it, and then simply initiate a project as you would do with pipenv install . This will create the Pipfile and Pipfile. lock and you can delete your old requirements.

How do you manage a project in Python? ›

9 Best practices for structuring Python projects
  1. Organize your code. Properly organizing your code is crucial when working on a Python project. ...
  2. Use consistent naming. ...
  3. Use version control. ...
  4. Use a package manager. ...
  5. Create virtual environments. ...
  6. Comment your code. ...
  7. Test, test, test. ...
  8. Lint and Style.
Mar 21, 2023

Is poetry faster than pipenv? ›

dependencies locking

poetry is faster and seems more robust. pipenv fails to install dependencies on machine learning pocs that used pytorch or yolo. Locking dependencies is a plus to guarantee the deployment on a PaaS hosting that deploys source code instead of docker image like heroku , ....

What is the use of pipenv in Python? ›

Pipenv is a tool that provides all necessary means to create a virtual environment for your Python project. It automatically manages project packages through the Pipfile file as you install or uninstall packages. Pipenv also generates the Pipfile.

How do I set up a Python project in pipenv? ›

Getting started
  1. pip install pipenv. Then change directory to the folder containing your Python project and initiate Pipenv,
  2. cd my_project pipenv install. ...
  3. pipenv install beautifulsoup4. ...
  4. pipenv install. ...
  5. pipenv install --dev nose2. ...
  6. pipenv install. ...
  7. pipenv install --dev. ...
  8. alias prp="pipenv run python"
Oct 16, 2017

How do I create a Python project using pipenv? ›

Create & Manage Python Projects

Now to create python projects, create a folder of your project name and open the terminal inside it. Just use pipenv install and then the name of the package you want to install. For eg. Pipenv instantiates automatically and will create two files in the folder – Pipfile and Pipfile.

Why use poetry instead of pip? ›

To manage these dependencies in your code, Poetry is a great tool for those working with more complex and diverse projects with a higher number of dependencies. While Pip and Conda are still viable options, they are more suited for smaller environments that are less complex.

Why use poetry over venv? ›

Just like virtualenv and venv , Poetry creates isolated environments for projects. However, it also ensures reproducibility by locking dependencies to specific versions, guaranteeing that your project works the same way across different environments.

Should I use Pipenv or poetry? ›

I always use poetry which is amazing when you are collaborating. It has a little overhead for the first time but it totally worths the effort. One of the main benefits is that when I install a package and commit the dependency update, the next person using the repo can have the same exact package.

How do I add venv to an existing project? ›

In the Settings window, go to the Project: [project name] > Project Interpreter section. Click the gear icon next to the interpreter drop-down menu and select "Add". In the Add Python Interpreter window, select "Existing environment" and click the "..." button to browse for the location of the virtual environment.

How do I activate the existing environment in pipenv? ›

To activate the environment, just navigate to your project directory and use pipenv shell to launch a new shell session or use pipenv run <command> to run a command directly.

How do I activate pipenv? ›

To work on an existing project with PIPenv shell, you need to navigate to the directory where the project files are located, and then run `pipenv shell`. This will activate the virtual environment and load the packages from the Pipfile or Pipfile.

How to install using pipenv? ›

If you must move or rename a directory managed by pipenv, run 'pipenv –rm' before renaming or moving your project directory. Then, after renaming or moving the directory run 'pipenv install' to recreate the virtualenv. Customize this behavior with PIPENV_CUSTOM_VENV_NAME environment variable.

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6325

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.