我設法在我的Windows上安裝最新版本。但是這種方法可能會導致安全問題。 在嘗試這個之前,請注意你在做什麼。
我只在Windows上的Python 3.5.2上測試過它。如果您使用Python2.x,最好不要嘗試它。
首先,要弄清楚你的venv
使用什麼版本。用途:
>>> import inspect
>>> import venv
>>> print(inspect.getsource(venv))
[the source of venv goes here]
而且你會發現,在功能_setup_pip
的認定中:
def _setup_pip(self, context):
"""Installs or upgrades pip in a virtual environment"""
# We run ensurepip in isolated mode to avoid side effects from
# environment vars, the current directory and anything else
# intended for the global Python environment
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
'--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
此功能告訴我們,PIP是叫ensurepip
另一個Python包安裝。所以我們會去的ensurepip
源使用:
>>> import ensurepip
>>> print(inspect.getsource(ensurepip))
[the source of ensure pip goes here]
在源的前幾行,你將獲得:
_SETUPTOOLS_VERSION = "20.10.1"
_PIP_VERSION = "8.1.1"
這是版本信息的ensurepip
捆綁。但只有更改這兩行纔會導致失敗,因爲您沒有更改本地.whl軟件包。那麼在哪裏。whl包位於?這,源裏面的功能bootstrap
:
def bootstrap(*, root=None, upgrade=False, user=False,
altinstall=False, default_pip=False,
verbosity=0):
...
with tempfile.TemporaryDirectory() as tmpdir:
# Put our bundled wheels into a temporary directory and construct the
# additional paths that need added to sys.path
additional_paths = []
for project, version in _PROJECTS: # *pay attention to these lines*
wheel_name = "{}-{}-py2.py3-none-any.whl".format(project, version)
whl = pkgutil.get_data(
"ensurepip",
"_bundled/{}".format(wheel_name),
)
with open(os.path.join(tmpdir, wheel_name), "wb") as fp:
fp.write(whl)
additional_paths.append(os.path.join(tmpdir, wheel_name))
...
注意什麼我評論就行了。因此,這告訴我們,位於ensure/_bundled/
目錄中的.whl文件,您也可以使用inspect.getsourcefile(ensurepip)
。我得到的是'c:\\python35\\lib\\ensurepip\\__init__.py'
。
轉到此目錄,您有兩個文件:pip-8.1.1-py2.py3-none-any.whl
,setuptools-20.10.1-py2.py3-none-any.whl
。
我上面寫的只是讓你瞭解我們在做什麼,所以以下是你需要做什麼:
- 轉到
/your/python/lib/dir/ensurepip/_bundled/
。
下載使用命令PIP輪文件,我得到了pip-9.0.1-py2.py3-none-any.whl
:
$ pip download pip
編輯的ensurepip
源。在ensurepip/__init__.py
,編輯這樣並將其保存:
# _PIP_VERSION = "8.1.1" # comment this line
_PIP_VERSION = "9.0.1" # add this line
現在你做到這一點。要檢查PIP版本:
$ python -m venv testpip
$ cd testpip
$ .\Scripts\activate.bat (in Linux, use '$ source ./bin/activate' instead.)
(testpip) $ pip --version
pip 9.0.1 from the \your\venv\testpip\lib\site-packages (python 3.5)
注意不要做同樣的setuptools
因爲它有一些其他的依賴,這使得問題更加複雜。
快樂黑客:)