A Python virtual environment ends up with an older

2019-06-03 10:20发布

My question is similar to another that was asked about Python3 so perhaps the answer is the same one - if so, I´d appreciate it if somebody can clarify this and go the step further of answering the additional questions posted here since there is, really, not a good answer there as to WHY it happens and HOW to avoid it without unintended consequences. Perhaps with 2.7 there is a better one?

I don´t understand the following sequence where a virtual environment in my MAC OS ends up with a version of PIP that´s older than the version it created it:

dhcp--41:VO$ virtualenv -p 
/usr/local/Cellar/python@2/2.7.15/bin/python env
Running virtualenv with interpreter 
/usr/local/Cellar/python@2/2.7.15/bin/python
New python executable in /Users/jbs/PycharmProjects/VOSW- VWN/env/bin/python2.7
Also creating executable in /Users/jbs/PycharmProjects/VOSW-VWN/env/bin/python
Installing setuptools, pip, wheel...done.

We´ve made sure the interpreter is 2.7.15

dhcp--41:VO$ source env/bin/activate
(env) dhcp--41:VO jbs$ python
Python 2.7.15 (default, May  1 2018, 16:44:14) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

This is just a check that the interpreter is 2.7.15 and now we generate the requirements output which is small as expected but which gives this warning (which is what I DON´T understand):

(env) dhcp--41:VO$ pip freeze
wheel==0.26.0
You are using pip version 8.0.2, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

We´ll now leave the environment and do freeze outside it:

(env) dhcp--41:VO$ deactivate
dhcp--41:VO$ pip freeze
absl-py==0.2.0

No suggestion for upgrade is given here despite the fact that we have the SAME version of python (if I understand correctly) as we check next:

dhcp-18--41:VO$ python
Python 2.7.15 (default, May  1 2018, 16:44:14) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Why did it install a different version of pip? Or perhaps, why does it default to a different version inside the virtual environment? It seems very odd to me. How can I make sure this does not happen every time I create a new virtual environment? Any input would be most helpful!

2条回答
Fickle 薄情
2楼-- · 2019-06-03 10:40

Your pip outside virtual environment is /usr/local/bin/pip which most probably means it uses /usr/local/bin/python or /usr/bin/python. But you've created the virtual environment using a different python — /usr/local/Cellar/python@2/2.7.15/bin/python. You can check its pip version with

/usr/local/Cellar/python@2/2.7.15/bin/pip --version

or

/usr/local/Cellar/python@2/2.7.15/bin/python -m pip --version

To upgrade that pip you need to run

/usr/local/Cellar/python@2/2.7.15/bin/python -m pip install -U pip

And to upgrade pip inside the virtual env

python -m pip install -U pip

after activating the virtual env.

查看更多
神经病院院长
3楼-- · 2019-06-03 11:01

Newer

If you want to "hotpatch" your installed python, just modify the versions listed in ensurepip/__init__.py and replace the two files in ensurepip/_bundled. You can find this location by running find * | grep ensurepip from the directory where python is installed. On macOS with Homebrew, this is the location: /usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ensurepip

You will also want to delete the ensurepip/__pycache__ directory that contains the .pyc files.

My older, build-time fix:

You are able to update the bundled versions of pip and setuptools by patching Python before building it from source. The following patch will update the bundled versions of pip and setuptools to the current available today. You will want to invoke configure with the following option: --with-ensurepip=upgrade

Those whl files are downloaded from PYPI here:

https://pypi.org/project/pip/#files

https://pypi.org/project/setuptools/#files

diff -ru Python-3.7.1/Lib/ensurepip/__init__.py Python-3.7.1.new/Lib/ensurepip/__init__.py
--- Python-3.7.1/Lib/ensurepip/__init__.py  2018-10-20 06:04:19.000000000 +0000
+++ Python-3.7.1.new/Lib/ensurepip/__init__.py  2018-11-27 02:36:19.301655008 +0000
@@ -8,9 +8,9 @@
 __all__ = ["version", "bootstrap"]


-_SETUPTOOLS_VERSION = "39.0.1"
+_SETUPTOOLS_VERSION = "40.6.2"

-_PIP_VERSION = "10.0.1"
+_PIP_VERSION = "18.1"

 _PROJECTS = [
     ("setuptools", _SETUPTOOLS_VERSION),
Only in Python-3.7.1/Lib/ensurepip/_bundled: pip-10.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: pip-18.1-py2.py3-none-any.whl
Only in Python-3.7.1/Lib/ensurepip/_bundled: setuptools-39.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: setuptools-40.6.2-py2.py3-none-any.whl
查看更多
登录 后发表回答