Skip to content

Instantly share code, notes, and snippets.

@ranabhat
Created November 6, 2020 10:24
Show Gist options
  • Save ranabhat/b41fca15755c6d0d1f5a9097200d80cd to your computer and use it in GitHub Desktop.
Save ranabhat/b41fca15755c6d0d1f5a9097200d80cd to your computer and use it in GitHub Desktop.
Allow for deterministic builds for your Python project without gaining the responsibility of updating versions of sub-dependencies.

Basic Usage

  1. Install pipenv pip install pipenv
  2. Create a directory say hello where you are going to create your python project.
  3. Go inside the directory cd hello
  4. Create a virtual env using command pipenv --python 3.7 (Specify which version of python you need)
  5. Alternative to 4 enter command pipenv shell. This case pipenv will use whatever default virtualenv finds.

This will create Pipfile and Pipfile.lock

  1. Install packages you need, say numpy pipenv install numpy

After every required pakage installed and the python project functions as required and you’re ready to push it to production. Then, you need to lock your environment so you can ensure you have the same one in production

  1. Enter pipenv lock

After you et your code and Pipfile.lock in your production environment, you should install the last successful environment recorded

  1. Enter pipenv install --ignore-pipfile
  2. Note if dependency is only for development (not for production) then install with the --dev argument, say pytest pipenv install pytest --dev
  3. Pipfile looks like
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[dev-packages]
pytest = "*"

[packages]
numpy = "*"


[requires]
python_version = "3.7"
  1. Pipfile.lock
{
    "_meta": {
        ...
    },
    "default": {
        "numpy": {
            "hashes": [
                "sha256:6c3130c8927109a08225993e4e503de4ac4f2678678ae211b33b519c622a7242",
                "sha256:9dce4b6bfbb5b062181d3f7da8f727ff70c1156cbb4024351eafd426deb5fb88"
            ],
            "version": "==0.12.1"
        },
        
        ...
    },
    "develop": {
        "pytest": {
            "hashes": [
                "sha256:8970e25181e15ab14ae895599a0a0e0ade7d1f1c4c8ca1072ce16f25526a184d",
                "sha256:9ddcb879c8cc859d2540204b5399011f842e5e8823674bf429f70ada281b3cc6"
            ],
            "version": "==3.4.1"
        },
        ...
    }
}
  1. Uninstall a package pipenv uninstall numpy
  2. Find your virtual environment pipenv --venv
  3. Find more pipenv commands pipenv --help
  4. More detail info go pipenv-introduction
  5. Check Pipenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment