Post

Getting Started with Pipenv Managing Python Dependencies Efficiently

Coursera

Pipenv

  • For the first time, I’m using pipenv as I’m accustomed to using virtualenv and pip

    So, I was curious about the differences between them and the benefits of using pipenv

  • requirements.txt and (Pipfile, Pipfile.lock) are all used to manage the dependencies of a Python project

Requirements.txt

  • pip install -r requirements.txt

    First, install the packages, and then compare the hash value specificed in requirements.txt with the actual hash value of the package

    If the hash does not match, the package installation is stopped and error message is displayed

    SHA256 hash algorithm, generated when the package is installed

    Key point : Not abstract, specify the exact versions of dependencies

Pipfile

  • Pipfile & Pipfile.lock

    On the other hand, you can make abstract dependency declaration using Pipfile. you can delcare dependencies without specifying a certain version

    The abstract dependency refers to the way of requesting packages without specifying a specfic version. For example, when you need the requests package, but you don't need to specify which version you need.

    Pipfile.lock interprets these abstract dependencies into specific versions and includes the hash of each package

    Therefore, using Pipfile and Pipfile.lock allows you to manage dependencies more conveniently and securely

  • Team Project

    Usually, One person runs the pipnev lock command to create the Pipfile.lock and shares this file with all team members.

How to use pipfile

  1. Install pipenv
    1
    
     pip install pipenv
    
  2. Install package (for me, I installed django)
    1
    
     pipenv install django
    

    This command creates Pipfile in your directory

Or, add package directly to the Pipfile

  1. Create text file Pipfile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
     [[source]]
     url = "https://pypi.org/simple"
     verify_ssl = true
     name = "pypi"
    
     [packages]
     django = "*"
     djangorestframework = "*"
     djangorestframework-xml = "*"
     djoser = "*"
    
     [dev-packages]
    
     [requires]
     python_version = "3.11"
    

    In this case, I added django, DRF, djoser packages under the [packages] section.

  2. Run Install
    1
    
     pipenv install
    
  • Once the installation is complete (create Pipfile or using pipenv install), Update the Pipfile.lock

    1
    
      pipenv lock
    
This post is licensed under CC BY 4.0 by the author.