Getting Started with Python package and Virtual Environment Managment System PipEnv

neotam Avatar

Getting Started with Python package and Virtual Environment Managment System PipEnv
Posted on :

Pipenv is the package management, dependency management and environment management system for the python. It was introduced to solve the common problems associated with work flows in using virtualenv and pip. PipEnv brings best of many package mamangement systems such as npm, yarn, pip, composer into the world of python. It simplifies and automates the tedious task of maintaining the requirements.txt while managing virtual environments without needing to use of virtualenv separately. Pipenv uses hashes to check integrity and exposes the any security vulnerabilities to help stay on the edge.

Pipenv is like fusion of both pip and virutalenv. It servers as the package management system while handling the virtual environments for development, production etc

Best part of using pipenv is it reinforces the use of latest dependencies such that you will not be a left alone fish in the lack using lagacy packages as dependencies.

Pipenv create Pipfile and adds or removes packages automatically. Pipfile.loc is used to produce the deterministic builds

Benefits of using pipenv

  • No need to use pip and virtualenv where Pipenv combines power of both tools
  • Works as both dependency manager and virtual environment management system
  • No need to manage requirements.txt where Pipenv manages Pipfile and Pipfile.lock
  • Encourages the usage of latest dependencies
  • Provides the dependency graph using command “pipenv graph”
  • Pipfile.lock is used to provide the deterministic builds and maintains the hashes for locked dependencies
  • Automatically maintains the Pipfile by adding and removing dependencies
  • Virtual environment is created automatically if not exists

Install the pipenv

pip install pipenv 

Once installed you forget about the good old requirements.txt, pipenv creates a new file name Pipfile and helper Pipfile.lock is also created

To get the quick help type the pipenv command

pipenv –help

Usage: pipenv [OPTIONS] COMMAND [ARGS]...

  --where                         Output project home information.
  --venv                          Output virtualenv information.
  --py                            Output Python interpreter information.
  --envs                          Output Environment Variable options.
  --rm                            Remove the virtualenv.
  --bare                          Minimal output.
  --man                           Display manpage.
  --support                       Output diagnostic information for use in
                                  GitHub issues.
  --site-packages / --no-site-packages
                                  Enable site-packages for the virtualenv.
                                  [env var: PIPENV_SITE_PACKAGES]
  --python TEXT                   Specify which version of Python virtualenv
                                  should use.
  --clear                         Clears caches (pipenv, pip).  [env var:
  -q, --quiet                     Quiet mode.
  -v, --verbose                   Verbose mode.
  --pypi-mirror TEXT              Specify a PyPI mirror.
  --version                       Show the version and exit.
  -h, --help                      Show this message and exit.

Usage Examples: 
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

  check         Checks for PyUp Safety security vulnerabilities and against
                PEP 508 markers provided in Pipfile.
  clean         Uninstalls all packages not specified in Pipfile.lock.
  graph         Displays currently-installed dependency graph information.
  install       Installs provided packages and adds them to Pipfile, or (if no
                packages are given), installs all packages from Pipfile.
  lock          Generates Pipfile.lock.
  open          View a given module in your editor.
  requirements  Generate a requirements.txt from Pipfile.lock.
  run           Spawns a command installed into the virtualenv.
  scripts       Lists scripts in current environment config.
  shell         Spawns a shell within the virtualenv.
  sync          Installs all packages specified in Pipfile.lock.
  uninstall     Uninstalls a provided package and removes it from Pipfile.
  update        Runs lock, then sync.
  verify        Verify the hash in Pipfile.lock is up-to-date.

Features of Pipenv

  • Both Pipfile and Pipfile.lock are generated automatically
  • Packages are automatically added or removed from Pipfile
  • Uses the .env file if exists
  • It finds Pipfile the recursively in the home directory
  • Using Pipfile.lock produces the deterministic builds
  • Virtualenv will be created automatically if it doesn’t exist

Using PipEnv

To get started with Pipenv simply install first package using pipenv

For example install Django of specific version

pipenv install Django==3.2.9

If Pipfile and Pipfile.lcok are already not present in the folder they will be auto created during installation of first package.

Good thing with pipenv is you don’t have to do the “pip freeze” to save requirements to the good old requirements.txt file. Pipenv auto adds installed packages as dependencies to the Pipfile

To install all dependencies from Pipfile, simply run pipenv install

pipenv install 

To install dependencies from particular group

pipenv install --dev

To initialise the project by creating virutal environment with specific python version

pipenv --python 3.11 

To uninstall dependencies

pipenv uninstall --all

You can combine the power of Conda with pipenv as follows

To create conda environment with your desired python version run command as follows

conda create --name py3_11 python=3.11.2

By running above command, creating the conda python environment with python 3.11.2

To, activate the conda environment

conda activate py3_11

You may find, pipenv is available in newly switched conda environment. In such a case, feel free to install it using “pip install pipenv”

No switch to newly created conda environment as follows

Now start using pipenv by installing any package. Make sure you are project home directory since that is where you need Pipfile and Pipfile.lock

pipenv install django 

Other helpful commands

# Locate project home directory
pipenv --where

# Locate the virutal envrionment
pipenv --venv

# What python interpreter 
pipenv --py

Leave a Reply

Your email address will not be published. Required fields are marked *