I recently activated an old virtualenv that was laying around to determine whether or not I should keep it. Unfortunately, when I attempted to run pip freeze
, this error message popped up instead:
$ pip freeze
Traceback (most recent call last):
File ".../bin/pip", line 5, in <module>
from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'
No pip? No problem.
See, here's the thing. Typically, pip
is the tool for installing tools in Python, but that's a problem if pip
isn't already installed. The developers of Python anticipated this tricky scenario and preemptively resolved it by including a Python package in the standard library to handle the bootstrapping! Just run:
python -m ensurepip
The output will look something like this:
$ python3 -m ensurepip
Looking in links: /tmp/tmp3buv8enu
Processing /tmp/tmp3buv8enu/setuptools-65.5.0-py3-none-any.whl
Processing /tmp/tmp3buv8enu/pip-22.3.1-py3-none-any.whl
Installing collected packages: setuptools, pip
Successfully installed pip-22.3.1 setuptools-65.5.0
And voilà, pip freeze
will work again!