Getting started with pipenv

Start working with pipenv, a packaging and dependency management tool for Python

How to install

It’s a normal one command job if you have pip module installed

pip install pipenv

create environment for your project

Run below command in your project directory to create environment using pipenv

pipenv install

This will create two files in current directory. Pipfile and Pipfile.lock. It also create environment folder in your user directory but let’s not bother about that for now.

In case, this project already been created with pipenv earlier, it will install all the dependencies lock with existing Pipfile.

💡 Learn how to use the zip method to convert lists into dictionaries in Python:

How to Use the Zip Method to Convert Lists into Dictionaries

👉 To read more such acrticles, sign up for free on Differ.

Adding module/packages to your environment

Once the environment is created, you are good to start adding your project dependency to it. For example, you are using boto3 for AWS related development, below statement will add boto3 to created environment.

pipenv install boto3

This will create entry in both pipenv and pipenv.lock file. This is how the pipfile looks like.

[[source]]
name = "pypi"
url = "[https://pypi.org/simple](https://pypi.org/simple)"
verify_ssl = true[dev-packages][packages]
boto3 = "*"**[requires]
python_version = "3.7"

if there are some packages you need just for your dev environment , like unit testing (using pytest), you can add them just for development by below command.

pipenv install pytest --dev

Start using added dependencies in project

Now that we have added dependencies, we can start writing python code by importing those dependencies in code. There are two way to run commands in environment

Run commands in environment by running shell

pipenv shell

This will launch shell and prompt you indicating environment name. you can start running command on that prompt. One can exit the shell by ‘exit’ command.

Run commands in environment without running shell

In order to run command in environment without running shell, you need to start all your commands with ‘pipenv run’

pipenv run python example.py

Above command will run example.py in created environment

Remove dependency from environment

You may want to remove dependency which is no longer needed

pipenv uninstall boto3

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics