Pipenv, the new Python packaging tool
Tags: pip, pipenv, python3, virtualenv
Published: 2018-04-23 16:40:45

---
title: Pipenv, the new Python packaging tool
published: true
description: 
tags: Pip, Pipenv,python3, virtualenv
---

![](https://thepracticaldev.s3.amazonaws.com/i/xu3vw4t3rwhvlvp4h12l.jpg)

Clearly,I have been living under a rock but not anymore! I shared an article <a href="https://achiengcindy.com/how-set-django-environment-linux-beginners/1"  target=_'blank'>about setting django environment using pip and virtualenv</a> and  <a href="https://dev.to/hangtwenty/comment/2m3e"  target=_'blank'>  M. Floering</a> suggested I check <code>pipenv</code>. You guessed right , yes I did and I am loving it!
<code>Pipenv</code> is the official recommended python packaging tool  acccording to <a href="https://docs.pipenv.org/"  target=_'blank'>  pipenv.org</a>.
It is a great tool for managing your project's dependencies as well as install and uninstall packages.

Seems like something pip and virtualenv did just well, **but why pipenv?**

1. Think of <code>pipenv </code> as a powerful  combination of pip and virtualenv.
2. Goodbye to  manually creating and managing you virtual environments <code>pipenv </code>does it for you!.
3. Great at managing project's dependencies.Instead of pip's <code>requirements.txt</code>,  <code>pipenv </code> creates two files the <code>pipfile</code> for the packages you installed directly and <code>pipfile.lock</code> for the dependencies and the specific version of the packages based on your pipfile.

#### pipenv workfow
1. Create virtualenv if doesn't exist already. 
2. Create <code>pipfile</code> to manage the packages installed by you
3. Finally, creates <code>pipfile.lock</code> to manage dependencies for the packages in pipfile

#### Let's get started!
We use <code> pip</code> to install <code>pipenv</code>,so ensure you have  both python and pip installed.
To check if pip is  installed run:
	
	$ pip --version
.. and python:

	$ python --version

**Installing pipenv**

	$ pip install --user pipenv
><strong>Note</strong>: I have used the user installation otherwise just run  $ pip install pipenv.

**Using pipenv**

Now that we have successfully installed <code>pipenv</code>, let's use it. Create the project directory and name it(I will call mine laughing-blog) and then change directory into the project's folder.
Let's  Initiate  <code>pipenv</code> by using the <code> install</code> command.

	$ mkdir laughing-blog //projectname
	$ cd laughing-blog
	$ pipenv --three install
><strong>Note</strong>:Specify  your python version by using <code>$ pipenv --three</code> for python 3 and <code>$ pipenv --two </code> for python 2.To use an  abitrary python version do:
<code>pipenv --python 3.6.4</code> or just <code>$ pipenv install</code> to use the default python  version.

The above code creates a new virtual environment for your project if it doesn’t exist already and the two magic files, `Pipfile` and `Pipfile.lock` in your project directory.

Below is a breakdown of the  **pipenv install** output.

**virtualenv**

    Creating a virtualenv for this project…
	Using /usr/bin/python3 (3.5.2) to create virtualenv
	Virtualenv location: /home/cindy/.local/share/virtualenvs/laughing-blog-FpALE3CM

**pipfile**

    Creating a Pipfile for this project…
 
Finally, **pipfile.lock**

    Pipfile.lock not found, creating…
    Locking [dev-packages] dependencies…
    Locking [packages] dependencies…
    Updated Pipfile.lock (711973)!
    Installing dependencies from Pipfile.lock (711973)…
    🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00

**Activating Virtualenv**

To activate virtualenv,simply run:

	$ pipenv shell
To exit virtulalenv,run:

	$ exit

####Managing Packages
To install a package simply run:

    $ pipenv  install <packagename>
... and to remove the package in the pipfile run:

    $ pipenv  uninstall <packagename> 
    
#### understanding pipfile and pipfile.lock
**Pipfile**

<code>Pipfile</code> manages the packages that you install,think of it as an  upgrade of <code>requirements.txt</code>.

 What makes pipfile  superior to requirements.txt

Lets look at a sample of  <code>requirements.txt</code> file

><strong>Note</strong>:simply run <code>pip freeze > requirements.txt</code> to generate the above file

    ...
    certifi==2018.4.16
    chardet==3.0.4
    defusedxml==0.5.0
    Django==2.0.4
    django-allauth==0.35.0
    idna==2.6
    oauthlib==2.0.7
    python3-openid==3.1.0
    ...

You will notice that <code>requirements.txt</code> list all the package we installed , version and the  dependencies as well.
 <code>pipfile</code> holds the  packages we installed and spare the rest to  <code>pipfile.lock</code>

**pipfile.lock**

This is the file that contains the dependencies based on the packages present in  <code>Pipfile</code>, the specific version of the packages to be used avoiding the risks of automatically upgrading packages that depend upon each other and breaking your project dependency tree.

If pipfile.lock wasn't created for some reason, you can lock the currently installed packages by running:

    $ pipenv lock

**pipfile sample**
 
     [packages]
     django = "*"
     django-allauth = "*"

     [requires]
     python_version = "3.5"

**pipfile.lock sample**

      "django-allauth": {
            "hashes": [
                "sha256:7b31526cccd1c46f9f09acf0703068e8a9669337d29eb065f7e8143c2d897339"
            ],
            "index": "pypi",
            "version": "==0.35.0"
        },
        "idna": {
            "hashes": [
                "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f",
                "sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4"
            ],
            "version": "==2.6"
        },
        "oauthlib": {
            "hashes": [
                "sha256:09d438bcac8f004ae348e721e9d8a7792a9e23cd574634e973173344046287f5",
                "sha256:909665297635fa11fe9914c146d875f2ed41c8c2d78e21a529dd71c0ba756508"
            ],
            "version": "==2.0.7"
        },
        "python3-openid": {
            "hashes": [
                "sha256:0086da6b6ef3161cfe50fb1ee5cceaf2cda1700019fda03c2c5c440ca6abe4fa",
                "sha256:628d365d687e12da12d02c6691170f4451db28d6d68d050007e4a40065868502"
            ],
            "version": "==3.1.0"
        },
##### Conclusion
Now,you know the definition,advantages and how pipenv functions.And the difference between the two magic files pipfile and pipfile.lock.

This Post was originally posted on [achiengcindy.com](https://achiengcindy.com/pipenv-new-python-packaging-tool/5/)