Pipenv is a python tool to make creating virtual environments easier. Why do you want virtual environments? Let me google that for you. But seriously, to understand why it’s useful, it’s worth considering the alternative, namely to install every python package you need for any project globally in your shared python environment. Honestly, if you’re a beginner, that will get you pretty far. But it’s bad for (at least) the following reasons:

  1. If you ever want to share your project with others, it’s not clear what they need to install for it to work.
  2. As your list of globally installed packages grows, things can start to get slow.
  3. This is the real killer: Different projects may need different versions of packages. For example, let’s say you start a project and you realize that the currently installed version of package X is old and doesn’t have a feature you need. Easy enough, you upgrade package X (globally, remember) to the latest version. Now, you go and work on an old project that relied on package X and, oh no, it’s now broken because it relied on the older verison of package X. Ugh.

Anyways, there are many (much better) explanations of what virtual environments are and why they’re useful available elsewhere; that’s not the point of this post. The point of this post is to provide an extremely minimal example of how to use Pipenv to serve as cliff-notes for future me. So…

---------------------------------
~/code/python/pipenv-experiments:
$ ls

---------------------------------
~/code/python/pipenv-experiments:
$ pipenv shell
Creating a virtualenv for this project...
Pipfile: /Users/mrussell/code/python/pipenv-experiments/Pipfile
Using /usr/local/bin/python3 (3.8.5) to create virtualenv...
⠦ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 347ms
  creator CPython3Posix(dest=/Users/mrussell/.local/share/virtualenvs/pipenv-experiments-utw_IVs0, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/mrussell/Library/Application Support/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /Users/mrussell/.local/share/virtualenvs/pipenv-experiments-utw_IVs0
Creating a Pipfile for this project...
Launching subshell in virtual environment...

~/code/python/pipenv-experiments:
$  . /Users/mrussell/.local/share/virtualenvs/pipenv-experiments-utw_IVs0/bin/activate

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ ls
Pipfile

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.8"

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ which pip
/Users/mrussell/.local/share/virtualenvs/pipenv-experiments-utw_IVs0/bin/pip

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ pipenv install osxphotos
Installing osxphotos...
... it worked ...

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ pip freeze  | rg osxphotos
osxphotos==0.36.8

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ exit
exit

---------------------------------
~/code/python/pipenv-experiments:
$ pip freeze | rg osxphotos # notice it's not there outside of the pipenv
EXIT STATUS: 1

---------------------------------
~/code/python/pipenv-experiments:
$ pipenv shell
Launching subshell in virtual environment...
~/code/python/pipenv-experiments:

$  . /Users/mrussell/.local/share/virtualenvs/pipenv-experiments-utw_IVs0/bin/activate

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ pipenv install jupyter
Installing jupyter...
... it worked ...

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ pipenv run jupyter notebook
... it worked ....

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
osxphotos = "*"
jupyter = "*"

[dev-packages]

[requires]
python_version = "3.8"

---------------------------------
(pipenv-experiments) ~/code/python/pipenv-experiments:
$ exit
exit