Goodbye Virtual Environments?

  • Learning curve: explaining “virtual environments” to people who just want to jump in and code is not always easy
  • Terminal isolation: Virtual Environments are activated and deactivated on a per-terminal basis
  • Cognitive overhead: Setting up, remembering installation location, activating/deactivating

Enter PEP 582 — Python local packages directory

A Python Enhancement Proposal (PEP) was introduced back in May of 2018 to modify Python itself to solve many of the problems Virtual Environments solve, but in a totally different and much simpler way. Note that it’s still in draft state so the proposal may still change, or it may not even be adopted at all.

Try it Today

It even comes with a reference CPython implementation.

> piploc install requests
Successfully installed certifi-2018.11.29 chardet-3.0.4 idna-2.8 requests-2.21.0 urllib3-1.24.1
> tree -L 4
.
└── __pypackages__
└── 3.6
└── lib
├── certifi
├── certifi-2018.11.29.dist-info
├── chardet
├── chardet-3.0.4.dist-info
├── idna
├── idna-2.8.dist-info
├── requests
├── requests-2.21.0.dist-info
├── urllib3
└── urllib3-1.24.1.dist-info
> python -c "import requests; print(requests)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
> pythonloc -c "import requests; print(requests)"
<module 'requests' from '/tmp/demo/__pypackages__/3.6/lib/requests/__init__.py'>
pip install --user pythonloc

Installing Multiple Dependencies or Lockfiles

If you have the source code available and it has a setup.py file, you can run

piploc install .
piploc install -r requirements.txt
pipenv lock --requirements
poetry run pip freeze > requirements.txt

Freezing Dependencies

Okay so we can install from various sources, but what if we’re developing and want to generate a list of dependencies.

pipfreezeloc
requests==2.21.0
pip freeze > requirements.txt
pipfreezeloc > requirements.txt

Update (2/19/19): What about …?

Changes to packaging and workflows should be scrutinized heavily since they have an outsized impact on the language’s adoption and community momentum. I wanted to take a moment to respond to some of the concerns I have seen raised. (Note: I, like you, am just an observer to the whole PEP process. I have no say in whether it is adopted or not.)

BINARY [ARGS] # i.e. pytest --help
pipx run BINARY [ARGS] # i.e. pipx run pytest --help

Conclusion

PEP 582 is a draft proposal that introduces a new way to install and isolate packages without Virtual Environments. It also eliminates indirection between project location and environment location, since the installation location is always in the same directory as the project — at __pypackages__.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store