How to manage Python projects with Pipenv (2024)

Python Hands-on

Have your Python projects become a rat’s nest? Pipenv provides a clean and easy way to manage virtual environments and packages together.

By Serdar Yegulalp

Senior Writer, InfoWorld |

How to manage Python projects with Pipenv (2)
Table of Contents
  • How Pipenv works
  • Get started with Pipenv
  • Set up a new project with Pipenv
  • Package installs with Pipenv
  • Pipenv and lockfiles
  • Use a Pipenv project

Show More

Python’s package ecosystem lets you leverage the work of millions of other Python developers with a simple pip install command. And Python’s virtual environments let you isolate projects and their packages from one another.

But juggling environments and packages separately can be unwieldy. Doubly so if your projects have specific package requirements, and you want to focus on development instead of maintenance. What we need is a way to manage environments and packages together.

Pipenv rolls the management of Python virtual environments and Python packages into a single tool. Pipenv ensures that each project uses the correct version of each package it needs, and that each of those packages has the correct dependencies as well.

Further, Pipenv generates a list of your project’s dependencies that can travel with it, allowing other users or developers to set up the same project in the same way. Other users will also need to install Pipenvto properly set up a Pipenv-managed project, but fortunately, installing and using Pipenv is a breeze.

How Pipenv works

Typically when you create a Python project and use a virtual environment for its packages, you’re tasked with creating the virtual environment yourself (using the commandpy -m venv), installing dependencies into it, and tracking the dependencies manually.

Pipenv provides a way to do all of this semi-automatically. The virtual environment for your project is created and managed for you when you install packages via Pipenv’s command-line interface. Dependencies are tracked and locked, and you you can manage development and runtime dependencies separately. You can also migrate from existing old-school requirements.txt files, so you don’t need to tear your project apart and start it over from scratch to use Pipenv well.

Note that unlike other Python project management tools (such as Poetry), Pipenv does not manage the “scaffolding” of your project. That is, Pipenv does not create the internal structure of the project directory with mock tests, documentation stubs, etc., but focuses chiefly on package and environment management. This makes Pipenv a good choice if you just want a tool to focus on virtual environments and packages, and not an all-in-one solution.

How to manage Python projects with Pipenv (3) IDG

Get started with Pipenv

Pipenv installs in the same manner as most any other Python package: pip install --user pipenv. The --user option is recommended to keep Pipenv from conflicting with other system-wide packages. You should alsoadd the path to the user base binary directory to the system path, so that Pipenv commands get routed to the right place.

If you plan to make Pipenv a consistent part of your workflow, it’s also a good idea to keep yourunderlying Python installation as minimal as possible. That advice applies for most any Python installation that makes use of virtual environments.

Set up a new project with Pipenv

To begin a completely new project with Pipenv, just create a directory and populate it with the files you’d normally create for a project. If you tend to scaffold a project as you go, you can start with an empty directory.

Installing packages for a project isn’t appreciably different with Pipenv than with Pip; in fact, the syntax is much the same. Open a console in your project directory and type pipenv install <package_name> to install a package for the project. To specify that the package is for development, use the -d flag. You can use pipsyntax to denote a specific version of a package (e.g., black==13.0b1).

Most projects need only the main and development (-d) sets of dependencies. If you want more, such as development vs. testing vs. bleeding edge, you can use the--categories flag to set categories for the dependencies you’re installing.

Package installs with Pipenv

When you install a package with Pipenv, two things happen.

First, Pipenvwill check if a virtual environment has already been created for this project directory. If yes, Pipenv will install the package into the existing virtual environment. If no, Pipenv will create a virtual environment that uses the same edition of Python used to run Pipenv. Note that the virtual environment is not created in the project directory itself; it’s created in a directory managed by Pipenvin your user profile.

Second, Pipenvwill install the requested packages to the virtual environment. When the installation is done, Pipenv will report back on all that it did, including a path to the virtual environment if it had to create one.

You generally don’t need to know the path to the virtual environment Pipenvcreates. To activate the environment, just navigate to your project directory and usepipenv shell to launch a new shell session or usepipenv run <command> to run a command directly. For example, usepipenv run mypy to run the command-line tool version of mypy (assuming the mypy tool was installed in the virtual environment), or pipenv run python -m <module> to run a Python module available in the virtual environment.

Pipenv and lockfiles

Peek inside the project directory after you’ve installed packages with Pipenv,and you’ll see two files, Pipfile and Pipfile.lock. Both are auto-generated by Pipenv,and should not be edited directly, as they describe the state of the packages in the project.

Pipfile is the simpler of the two. It just lists the packages needed for the project, where they’re installed from (the default is PyPI), and which version of Python is needed to run everything. In other words, Pipfile is akin to requirements.txt.

Pipfile.lock is more complex. It lists each package along with version details and SHA-256 hashes generated from the package. The hashes are used to ensure that the installed packages match exactly what’s specified — not just the version number, but the obtained contents as well.

When you work on a project that uses Pipenvfor package management, you’ll want to add the Pipfile and Pipfile.lock files to the version control repository for the project. Any changes made to the packages for your project will in turn alter those files, so those changes should be tracked and versioned.

Use a Pipenv project

If you download a source repository for a project that uses Pipenv for package management, all you need to do is unpack the contents of the repository into a directory and run pipenv install (no package names needed). Pipenv will read the Pipfile and Pipfile.lock files for the project, create the virtual environment, and install all of the dependencies as needed.

Finally, if you want to use Pipenvto manage a project that currently uses a requirements.txt file, just navigate to the project’s directory and run pipenv install. Pipenv will detect therequirements.txt (or you can use the -r flag to point to it) and migrate all of the requirements into a Pipfile.

Next read this:

  • Why companies are leaving the cloud
  • 5 easy ways to run an LLM locally
  • Coding with AI: Tips and best practices from developers
  • Meet Zig: The modern alternative to C
  • What is generative AI? Artificial intelligence that creates
  • The best open source software of 2023

Related:

  • Python
  • Software Development

Serdar Yegulalp is a senior writer at InfoWorld, focused on machine learning, containerization, devops, the Python ecosystem, and periodic reviews.

Follow

Copyright © 2023 IDG Communications, Inc.

How to install Python the smart way

Virtualenv and venv: Python virtual environments explained

Python virtualenv and venv dos and don’ts

Currently reading

How to manage Python projects with Pipenv

How to use Python dataclasses

How to use structural pattern matching in Python

How to use PyInstaller to create Python executables

Intro to PyScript: Run Python in your web browser

4 keys to writing modern Python in 2022

How to manage Python projects with Pipenv (2024)

FAQs

How do I create a Python project with 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.

How do I run Pipfile? ›

Click Create a pipenv environment using Pipfile if you want PyCharm to configure pipenv for you automatically. Alternatively, you can click Configure Python interpreter to follow the standard workflow. If PyCharm cannot autodetect the pipenv executable, specify the path to it and click OK.

Does pipenv manage Python versions? ›

Pipenv Features

Automatically install required Python version when pyenv is available. Automatically finds your project home, recursively, by looking for a Pipfile . Automatically generates a Pipfile , if one doesn't exist. Automatically creates a virtualenv in a standard customizable location.

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.

What is the difference between pip and Pipenv? ›

Pip is the default package management tool for Python, allowing users to easily download, install, upgrade, and uninstall Python packages. Pyenv is used to manage Python versions, while Pipenv is used to manage packages, and Anaconda is a distribution of Python (think of it as a lazy package of Python).

How do I start a project with 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

What is the difference between pipenv and virtualenv? ›

Consider Pipenv if you want a tool that simplifies the process of managing virtual environments and dependencies for your Python projects. Consider virtualenv if you want a more lightweight tool for creating isolated Python environments, or if you prefer to manually manage dependencies using pip.

How do you activate a virtual environment in pipenv? ›

You generally don't need to know the path to the virtual environment Pipenv creates. 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.

What is the difference between Pyenv and pipenv? ›

venv , pyvenv , and pyenv are all tools that can be used to create isolated Python environments. virtualenv and virtualenvwrapper are similar tools that can also create isolated Python environments. pipenv is a tool that combines virtualenv with pip , the Python package manager.

Should pipenv be installed globally? ›

Pipenv is a good tool for managing python environments. But some things need to be global. For instance most command line tools should be always available and jupyter for use with non python kernels.

Is it OK to install multiple Python versions? ›

I could just have different python version on my system, and use their fully specified path just to start a local environment. This is completely standard and should work. Even with a management tool like pyenv, ultimately there are just multiple installed versions of Python.

How do I specify Python version in pipenv? ›

Specifying Versions of Python
  1. $ pipenv --python 3.
  2. $ pipenv --python 3.11.
  3. [[source]] url = "https://pypi.python.org/simple" verify_ssl = true name = "pypi" [dev-packages] [packages] [requires] python_version = "3.11"
Nov 16, 2023

How do I package with Pipenv? ›

Clone / create project repository:
  1. $ cd myproject. Install from Pipfile, if there is one:
  2. $ pipenv install. Or, add a package to your new project:
  3. $ pipenv install <package> This will create a Pipfile if one doesn't exist. ...
  4. $ pipenv shell $ python --version.

Where does pipenv store packages? ›

Pipenv installs packages in ~/. local/share/virtualenvs/<your-virtualenv> by default. I prefer to place the folder containing the virtual environment in the project itself. This way you always delete the virtual environment along with your project when you're done.

What is the command not found in Pipenv? ›

The 'pipenv' command not found error often occurs when the directory where Pipenv is installed is not included in the system's PATH variable. The PATH variable is a list of directories that the operating system searches when executing commands.

How do I create a simple Python project? ›

  1. Step 1: Download Python and your IDE. Python is free, open-source software that works on Linux, Mac, and Windows. ...
  2. Step 2: Create your first program. ...
  3. Step 3: Write the first line of Python code. ...
  4. Step 4: Explore some math with Python. ...
  5. Step 5: Explore an if-statement with Python. ...
  6. Step 6: Create a function in Python.
Aug 21, 2020

How do I set up a Python project? ›

  1. Install a Python version manager (pyenv) ...
  2. Choose an environment manager (poetry) ...
  3. Otherwise, use Docker to isolate dependencies. ...
  4. Add Some Code. ...
  5. Write Some Tests. ...
  6. Linting our code. ...
  7. Automate checks on local with pre-commit. ...
  8. Automate checks on remote with GitHub Actions.
Jun 5, 2023

How to create virtual environment in Python using Pipenv? ›

You need to create virtual environments. pipenv allows you to spawn a virtual environment with pipenv shell I believe. Then you can install dependencies to that virtual environment and have the other modules installed with pip. For that matter you don't even need to differentiate between pipenv or pip.

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5732

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.