An experimental pip subcommand for the Python Launcher for Unix
There are a couple of things I always want to be true when I install Python packages for a project:
- I have a virtual environment
- Pip is up-to-date
For virtual environments, you would like them to be created as fast as possible and (usually) with the newest version of Python. For keeping pip up-to-date, it would be nice to not have to do that for every single virtual environment you have.
To help make all of this true for myself, I created an experimental Python Launcher for Unix "subcommand": py-pip. The CLI app does the following:
- Makes sure there is a globally cached copy of pip, and updates it if necessary
- Uses the Python Launcher for Unix to create a virtual environment where it finds a
pyproject.toml
file - Runs pip using the virtual environment's interpreter
This is all done via a py-pip.pyz
file (which you can rename to just py-pip
if you want). The py-pip.pyz
file available from a release of py-pip can be made executable (e.g. chmod a+x py-pip.pyz
). The shebang of the file is already set to #!/usr/bin/env py
so it's ready to use the newest version of Python you have installed. Stick that on your PATH
and you can then use that instead of py -m pip
to run pip itself.
To keep pip up-to-date, the easiest way to do that is to have only a single copy of pip to worry about. Thanks to the pip team releasing a self-contained pip.pyz
along with pip always working with supported, it means if we just cache a copy of pip.pyz
and keep that up-to-date then we can have that one copy to worry about.
Having a single copy of pip also means we don't need to install pip for each virtual environment. That lets us use microvenv and skip the overhead of installing pip in each virtual environment.
Now, this is an experiment. Much like the Python Launcher for Unix, py-pip is somewhat optimized for my own workflow. I am also keeping an eye on PEP 723 and PEP 735 as a way to only install packages that have been written down somewhere instead of ever installing a package à la carte as I think that's a better practice to follow and might actually trump all of this. But since I have seen others have frustration from both forgetting the virtual environment and having to keep pip up-to-date, I decided to open source the code.