Pipenv: Practical Guide to the New Python Packaging Tool

Introduction

Pipenv is a tool that creates and manages a virtual environment for your projects and adds or removes packages from your Pipfile as you install or uninstall them. It also generates the Pipfile.lock, which we use to produce deterministic builds and create a snapshot of our working environment. Pipenv provides us with an easy method to set up a working environment. You no longer need to use pip and virtualenv separately. They work together.

Pipenv: Practical Guide to the New Python Packaging Tool

Install Pipenv

Before we start using Pipenv, we will need to install it. I assume you have installed Python and pip on your machine. So, open the terminal and run . And pip will install Pipenv within your home directory. You might need to replace pip with pip3 depending on the Python version you have installed on your computer.

Configure PATH Environment Variable

You might want to add the user base’s binary directory to your PATH environmental variable for your convenience. If you don’t, you will receive “command not found” by running the pipenv command directly.

On Windows:

To do that, on Windows, open the Command Prompt and run , and this will return the path to the user site-packages directory.

Replace “site-packages” with “Scripts” in this path to receive a string for adding to the PATH variable. And then run

On macOS or Linux:

On macOS or Linux, open the terminal and run . Add "bin" to this path to receive a string for adding to your ~/.bashrc file.

Edit ~/.bashrc file and add

And finally, run this command to make the changes take effect. .

Create a New Virtual Environment

To create a new virtual environment, using a specific Python version, use the — python flag. Navigate to the directory containing your Python project and run , which is the installed version of Python on my computer.

Alternatively, you can run to initialise a Python 3 virtual environment or for Python 2.

By running this, Pipenv will create a new virtual environment for your project with the Python version you specified as the required Python version for this project. And it will tell me where I can find the newly created virtual environment.

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[requires]
python_version = "3.8"

Activate the Newly Created Virtual Environment

You might want to verify the Python version installed in your virtual environment. To do so, you first need to activate that virtual environment by running .

We will then run , and this will tell me the installed version of Python. You can also deactivate this shell using the exit command.

Installing Your Project Dependencies

Now you can install the third-party packages you need. To install a Python package for your project, use the install keyword. For example, Here, we are going to install requests version 2.24.

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "==2.24"
[requires]
python_version = "3.8"

To install the latest version of a package, not a specific version, you could omit the version from this command.

Note: Install command is fully compatible with pip install syntax, for which you can find the full documentation here.

You can similarly remove this package by running .

Installing Your Project Development Dependencies

Let’s say you also want to lint your code, and you want to use pylint for doing this. You don’t need pylint in production, so you can specify that this dependency is only for development using the — dev flag.

And Pipenv will add the pylint under [dev-packages] within Pipfile. This section separates dependencies needed only for development from ones needed for the code to work.

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "==2.24"
[dev-packages]
pylint = "*"
[requires]
python_version = "3.8"

Update Outdated Dependencies

To update a specific package or all the packages in your project’s dependencies, you first need to find out whether there is any new release for the packages in your dependencies. Run , and Pipenv will return a list of outdated packages.

Now you can update all the packages by running or to update the requests package only.

Push Your Project to Production

Let’s say you’re ready to push your project to production. To do that, you need to lock your current working environment to ensure you will have the same production environment. Run .

Now you are in the production environment and want to replicate the exact environment you had in development. Let me remove the current virtual environment to simulate this situation by running .

I’m then going to install the last successful environment. Run . And this tells Pipenv to ignore the Pipfile for installation and use what is in the Pipfile.lock. And it will create the same environment as you had when you ran .

Originally published on my blog at https://rapidevelop.org/python/pipenv-practical-guide.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store