Your First Guide to Getting Started with “pipenv”
Tags: python, pipenv, package
Published: 2019-04-17 06:29:55

---
title: Your First Guide to Getting Started with “pipenv”
published: true
description: Introduction to using "pipenv", Python package manager
tags: Python, pipenv, package
---

# What is “pipenv”?
There are many package manager tools in other programming languages such as:

* Ruby
	* bundler
* PHP
	* composer
* NodeJS
	* npm
	* yarn
* Rust 
	* cargo

If you know any of the above tools, it might be easy to understand what it is.
`pipenv` is just a package management tool for Python as same as those tools.


# Why “pipenv”?
The official documentation [Pipenv: Python Dev Workflow for Humans](https://pipenv.readthedocs.io/en/latest/) and this blog post  [Why Python devs should use Pipenv](https://opensource.com/article/18/2/why-python-devs-should-use-pipenv) would tell us why we should use pipenv.

Two main problems of “before” pipenv workflow are following:

* Using `pip` and `virtualenv` separately is not necessary any more!
* `requirements.txt` is problematic: see [A Better Pip Workflow™](https://www.kennethreitz.org/essays/a-better-pip-workflow)
	* without version specified -> different versions every time for dev/prod environments!
	* with version specified -> `--upgrade` is hard!

If we look back on the history of Python package management tools, there are many tools before, and this is terrible.

* Package manager
	* Easy Install
	* pip
* Many many virtual environment tools
	* virtualenv
	* venv
	* pyenv
	* pyenv-virtualenv
	* etc

Finally, `pipenv` comes to our rescue!


## Setup pipenv
Check Python version (should be 3.x)

```bash
$ python --version
Python 3.6.0
```

Check pip version

```bash
$ pip --version
pip 18.1
```

Install pipenv

```bash
brew install pipenv
```

Check pipenv version

```bash
$ pipenv --version
pipenv, version 2018.11.26
```

Success: pipenv is installed!

## pipenv workflow

Install dependencies (pipenv automatically detect `requirements.txt`and solve the dependencies)

```bash
$ cd [YOUR PROJECT]
$ pipenv install
```

`Pipfile` and `Pipfile.lock` should be created

* Pipfile: list of all installed packages

(Pipfile example)
```toml
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
jupyter = "*"
seaborn = "*"
numpy = "*"
pandas = "*"
plotly = "*"
scipy = "*"
six = "*"
sklearn = "*"
pyspark = "*"
flake8 = "*"
autopep8 = "*"
Flask = "*"

[requires]
python_version = "3.7"
```

* Pipfile.lock: maintain a proper dependencies and sub packages with version

(Pipfile.lock  example)
```json
{
    "_meta": {
        "hash": {
            "sha256": "xxxxxxxxxxxxxxxxxxxxxxxx"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "appnope": {
            "hashes": [
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx",
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx"
            ],
            "markers": "sys_platform == 'darwin'",
            "version": "==0.1.0"
        },
        "attrs": {
            "hashes": [
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx",
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx"
            ],
            "version": "==19.1.0"
        },
        "autopep8": {
            "hashes": [
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx"
            ],
            "index": "pypi",
            "version": "==1.4.4"
...
```


Now, run your python script (Installed packages are available when you use `pipenv run`)

```bash
$ pipenv run python [YOUR PYTHON MAIN SCRIPT]
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (980232)!
Installing dependencies from Pipfile.lock (980232)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 73/73 — 00:01:22
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
```

Thanks for reading!
 If you enjoyed this article, feel free to hit that clap button 👏.

# References
* [Pipenv: Python Dev Workflow for Humans](https://pipenv.readthedocs.io/en/latest/): Official documentation
* [Why Python devs should use Pipenv](https://opensource.com/article/18/2/why-python-devs-should-use-pipenv): Why pipenv?
* [The Hitchhikers guide to pipenv](https://medium.com/@kurianbenoy1998/the-hitchhikers-guide-to-pipenv-bb0e42673bf5): pipenv guide
* [Python Dependency Management with pipenv](https://medium.com/@ryanbrookepayne/python-dependency-management-with-pipenv-e9f11e8f5386): pipenv guide
* [Five Myths About Pipenv](https://medium.com/@grassfedcode/five-myths-about-pipenv-698c5f198e4b)