How to create virtual environment with pipenv

Pipenv is a Python tool that aims to bring the best features of multiple other tools into one. It provides an easy way to manage virtual environments and manage package dependencies consistently. Its key goal is to simplify the workflow of managing a Python environment for your project.

Installing pipenv

$ python -m pip install pipenv

Checking pipenv version

$ pipenv --version

pipenv, version 2022.7.4

Checking pipenv version

$ pipenv -h
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --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.
  --three                         Use Python 3 when creating virtualenv.
  --clear                         Clears caches (pipenv, pip).  [env var:
                                  PIPENV_CLEAR]
  -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 setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

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

Commands:
  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.

Custom virtual environment location

Pipenv automatically honors the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want, e.g.:

$ echo 'export WORKON_HOME=~/.venvs' >> ~/.zshrc

In addition, you can also have Pipenv stick the virtualenv in project/.venv by setting the PIPENV_VENV_IN_PROJECT environment variable.

$ echo 'export PIPENV_VENV_IN_PROJECT=true' >> ~/.zshrc

Creating virtual environment

$ mkdir python_demo
$ cd python_demo
$ pyenv local 3.10.5
$ export PIPENV_VENV_IN_PROJECT=true
$ pipenv --python $(pyenv which python)
$ pipenv --venv

/Users/kenanhancer/.local/share/virtualenvs/python_demo-7aY5i6lG
$ cd python_demo
$ ls -lat

-rw-r--r--   1 kenanhancer  staff  10765 19 Jun 20:24 Pipfile.lock
-rw-r--r--   1 kenanhancer  staff    167 19 Jun 20:24 Pipfile
-rw-r--r--   1 kenanhancer  staff      7 19 Jun 20:05 .python-version
$ tree -L 4 $(pipenv --venv)

├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.nu
│   ├── activate.ps1
│   ├── activate_this.py
│   ├── normalizer
│   ├── pip
│   ├── pip-3.10
│   ├── pip3
│   ├── pip3.10
│   ├── py.test
│   ├── pytest
│   ├── python -> /Users/kenanhancer/.pyenv/versions/3.10.5/bin/python
│   ├── python3 -> python
│   ├── python3.10 -> python
│   ├── wheel
│   ├── wheel-3.10
│   ├── wheel3
│   └── wheel3.10
├── lib
│   └── python3.10
│       └── site-packages
│           ├── __pycache__
│           ├── _distutils_hack
│           ├── _pytest
│           ├── _virtualenv.pth
│           ├── _virtualenv.py
│           ├── certifi
│           ├── certifi-2023.5.7.dist-info
│           ├── charset_normalizer
│           ├── charset_normalizer-3.1.0.dist-info
│           ├── distutils-precedence.pth
│           ├── exceptiongroup
│           ├── exceptiongroup-1.1.1.dist-info
│           ├── idna
│           ├── idna-3.4.dist-info
│           ├── iniconfig
│           ├── iniconfig-2.0.0.dist-info
│           ├── packaging
│           ├── packaging-23.1.dist-info
│           ├── pip
│           ├── pip-23.1.2.dist-info
│           ├── pip-23.1.2.virtualenv
│           ├── pkg_resources
│           ├── pluggy
│           ├── pluggy-1.0.0.dist-info
│           ├── py.py
│           ├── pytest
│           ├── pytest-7.3.2.dist-info
│           ├── requests
│           ├── requests-2.31.0.dist-info
│           ├── setuptools
│           ├── setuptools-67.8.0.dist-info
│           ├── setuptools-67.8.0.virtualenv
│           ├── tomli
│           ├── tomli-2.0.1.dist-info
│           ├── urllib3
│           ├── urllib3-2.0.3.dist-info
│           ├── wheel
│           ├── wheel-0.40.0.dist-info
│           └── wheel-0.40.0.virtualenv
├── pyvenv.cfg
└── src

Listing virtual environments

$ ls -lat ~/.local/share/virtualenvs/

Installing dependencies

$ pipenv install requests
$ pipenv install --dev pytest

Listing dependencies

$ pipenv graph
$ pipenv run pip freeze | cut -d'=' -f1

Uninstalling dependencies

$ pipenv uninstall requests

Activating virtual environment

$ pipenv shell

Deactivating virtual environment

$ exit

Deleting virtual environment

$ cd python_demo
$ pipenv --rm