pip version mismatch in venv

2020-04-19 05:13发布

问题:

I am confused about how virtual Python environments work (Python 3.6, using venv). I set up my venv, and activated it in the command line. I would expect that everything I do from then on out uses the python and pip commands from the virtual environment (as these directories are added to PATH upon activation).

The thing is, that when I run pip --version in my venv, it returns pip 10.0.1. But when I try to upgrade it with python -m pip install --upgrade pip, I get Requirement already up-to-date

(venv) PS C:\Python\files\myproj> pip --version
> pip 10.0.1 from c:\python\files\myproj\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip (python 3.6)
(venv) PS C:\Python\files\myproj> python -m pip install --upgrade pip
> Requirement already up-to-date: pip in c:\python\files\myproj\venv\lib\site-packages (18.0)

So what's going on? Why isn't the command line command targeting the same pip as python is? And how to remedy this?

回答1:

pip still points to the global one, even when in an venv. python, however, does not and uses the venv.

This means that when you run pip --version, you get your global pip version. when you run python -m pip install --upgrade pip, you're using the venv version of python, which apparently already has the newest pip. To confirm this, you can run python -m pip --version and you should get the latest version as an output.