Search code examples
pythonpippython-venv

Why is pip installed in virtual environments by default?


Python's venv module is installing pip in created virtual environments by default, unless instructed not to using --without-pip, as described in docs.python.org. Why is that so?

If pip is "just" a package manager, I don't see why it should in any way be coupled to the used Python interpreter or installed packages. I wonder why the system's pip is not used instead to manage all environments. Indeed, pip's documentation (pip.pypa.io) only describes how this can be done and does not discourage it.

$ python -m venv .venv --without-pip
$ python -m pip --python .venv install SomePackage

Solution

  • Here is the reasoning/justification as given in PEP-453:

    Python 3.3 included a standard library approach to virtual Python environments through the venv module. Since its release it has become clear that very few users have been willing to use this feature directly, in part due to the lack of an installer present by default inside of the virtual environment. They have instead opted to continue using the virtualenv package which does include pip installed by default.

    To make the venv more useful to users it will be modified to issue the pip bootstrap by default inside of the new environment while creating it. This will allow people the same convenience inside of the virtual environment as this PEP provides outside of it as well as bringing the venv module closer to feature parity with the external virtualenv package, making it a more suitable replacement.

    To handle cases where a user does not wish to have pip bootstrapped into their virtual environment a --without-pip option will be added.

    The venv.EnvBuilder and venv.create APIs will be updated to accept one new parameter: with_pip (defaulting to False).

    The new default for the module API is chosen for backwards compatibility with the current behaviour (as it is assumed that most invocation of the venv module happens through third part tools that likely will not want pip installed without explicitly requesting it), while the default for the command line interface is chosen to try to ensure pip is available in most virtual environments without additional action on the part of the end user.