PipFile

Pipenv is a tool that combines the features of pip and virtual environments to simplify package management in Python projects.

Installation

pip install pipenv

Creating a Virtual Environment - Pipfile

Creates a Pipfile to describe the project's environment; it's similar to a requirements.txt file. If there is a Pipfile then it installs all the packages present in this file.

pipenv install

Install packages

Installing requests package

Note: It creates a virtual environment for the project if it doesn't already exist.

pipenv install requests

Uninstall Packages

pipenv uninstall requests

Activate Virtual Environment

pipenv shell

Deactivate Virtual Environment

exit;

Remove Virtual Environment

pipenv --rm

Common Commands

  • To Run Python in a virtual environment

      pipenv run python
    
  • To change the Python to a different version

      pipenv --python 3.6
    
  • Path to your virtual environment

      pipenv --venv
    
  • Security issues within the packages

      pipenv check
    
  • Dependency graph or tree

      pipenv graph
    
  • Update the pipfile.lock with the latest versions present in pipfile - production

      pipenv lock
    
  • Create a virtual environment with versions and packages present in the Pipfile.lock i.e ignoring Pipfile

      pipenv install --ignore-pipfile
    
  • Adds a development package that is not required for production.

      pipenv install pytest --dev
    
  • Install packages from requirements.txt

      pipenv install -r requirements.txt
    
  • Update back requirements.txt file with latest installations and packages

      pipenv lock -r
    

Environment Variables

.env file: Stores key-value pairs for environment variables.

About Pipfile

  • Describe the project's environment, including dependencies and settings.

  • Uses the TOML format (Key, value pairs).

  • source: It is a URL from which the dependencies get downloaded. This is very useful if the repositories are hosted in a private corporate environment.

  • packages: Lists all packages, and if '*' is used, it means the latest versions will be installed.

  • requires: Specifies the current Python version.

About Pipfile.lock

  • Automatically generated and should not be modified.

  • Locks the versions of packages and includes hash values.

References

Corey Schafer - link