2021 年如何在 Python 2.7 上安装 pip

How to install pip on Python 2.7 in 2021

我有仍然是 运行 Python 2.7.6 的遗留生产服务器。我们有一个从 docker 图像构建的本地环境 ubuntu 14.04 旨在复制该环境(一旦安装了所有东西,事情仍然在那里工作。)创建这个环境的加壳构建脚本最近停止工作显然是由于到 PyPi 放弃非 SNI 支持。

我尝试使用文档中的 get-pip.py 下载 pip:

wget -c https://bootstrap.pypa.io/pip/2.7/get-pip.py
python2 get-pip.py

这给了我以下警告:

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
/tmp/tmpBb3LJu/pip.zip/pip/_vendor/urllib3/util/ssl_.py:424: SNIMissingWarning: An HTTPS request has been made, but the SNI (Server Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
/tmp/tmpBb3LJu/pip.zip/pip/_vendor/urllib3/util/ssl_.py:164: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
ERROR: Could not find a version that satisfies the requirement pip<21.0 (from versions: none)
ERROR: No matching distribution found for pip<21.0

建议的解决方案是使用 pip 升级 urllib3 https://serverfault.com/questions/866062/easy-install-and-pip-fail-with-ssl-warnings

我没有 pip,所以我使用

安装了一个旧版本
apt-get install python-pip

这将安装 pip 1.5.4

当我尝试 pip install "urllib3[secure]" 时,我得到以下信息:

Requirement already satisfied (use --upgrade to upgrade): urllib3[secure] in /usr/lib/python2.7/dist-packages
  Installing extra requirements: 'secure'
Cleaning up...

如果我尝试 pip install "urllib3[secure]" --upgradepip install --index-url https://pypi.python.org/simple/ --upgrade pip 我得到:

Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement urllib3[secure] in /usr/lib/python2.7/dist-packages
Downloading/unpacking urllib3[secure]
Cleaning up...
No distributions at all found for urllib3[secure] in /usr/lib/python2.7/dist-packages
Storing debug log for failure in /root/.pip/pip.log

(pip 消息反映的是 pip,而不是 urllib3[secure])

当我尝试使用 pip 1.5 安装 uWSGI 时

pip install uWSGI

我得到以下信息:

Downloading/unpacking uWSGI
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement uWSGI
Cleaning up...
No distributions at all found for uWSGI
Storing debug log for failure in /root/.pip/pip.log

升级 pip 在这里也不起作用

Downloading/unpacking uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Cleaning up...
No distributions at all found for uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Storing debug log for failure in /root/.pip/pip.log

重新安装 pip 不起作用:

python -m pip install -U --force-reinstall pip

给我:

Downloading/unpacking pip
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement pip
Cleaning up...
No distributions at all found for pip
Storing debug log for failure in /root/.pip/pip.log

如果我打开 /root/.pip/pip.log 我会看到以下内容:

Downloading/unpacking pip
  Getting page https://pypi.python.org/simple/pip/
  Could not fetch URL https://pypi.python.org/simple/pip/: 403 Client Error: [[[!!! BREAKING CHANGE !!!]]] Support for clients that do not support Server Name Indication is temporarily disabled and will be permanently deprecated soon. See https://status.python.org/incidents/hzmjhqsdjqgb and https://github.com/pypa/pypi-support/issues/978 [[[!!! END BREAKING CHANGE !!!]]]

The link 表示 SNI 支持已被删除:

For users of Python 2.7.{0...8}
Upgrading to the last Python 2.7 release is an option.

However, note that Python 2.7 series itself is now End of Life and support in pip was dropped with version 21.0.

For users of Python 2.6.x and lower:
Neither the Python core developers, or pip maintainers support Python 2.6 and below.

If someone is aware of a work around for this issue (SNI support specifically) they are welcome to share it here for others.

There is no recommended solution from the PyPI team.

如何为新开发人员设置本地环境来处理我们的遗留应用程序?我已经创建了一个新的 Python 3 开发服务器和本地环境,但是我还需要一段时间才能推出暂存和实时环境,将所有内容都移走并进行测试。

正如消息所说,PyPi 有 discontinued support for Python <2.7.9 as of May 6th 2021. If you're running a version < 2.7.9 and you cannot upgrade to a newer version of Python then your only option is to manually download the wheels from PyPi.

这些是我需要对我的构建脚本进行修改以使其工作:

我需要安装 software-properties-commongcc

apt-get install -y software-properties-common gcc

然后我下载了(setuptools](https://pypi.org/project/setuptools/44.1.1/#files)解压安装:

python ./setuptools-44.1.1/setup.py install

接下来,我下载了 pip 并将其添加到名为 wheels 的文件夹中。然后我可以使用 whl 文件到 运行 pip 得到 pip

python ./wheels/pip-20.3.4-py2.py3-none-any.whl/pip install --no-index --find-links ./wheels/ pip --ignore-installed

建议使用 Ubuntu 16.04 和 Python 2.7.17 构建一个 Docker 容器,并使用它来下载包。

pip download -r requirements.txt

但是包的版本是错误的,所以我最终通过 requirements.txt 并从 PyPi 手动下载每个包并将其添加到 wheels 文件夹中。 运行ning 实例很有用,因此您可以 运行 pip freeze 或查看 requirements.txt 文件来获取您需要的所有软件包的版本号。

现在我可以使用 pip,我可以安装我的其他包了:

python pip install --no-index --find-links ./wheels/ -r /root/requirements.txt

这发现了一些我还没有下载包的依赖项,所以我必须仔细检查并下载它们并将它们添加到 wheels 文件夹中。我发现还有其他一些东西需要与我最初下载的版本不同的版本,一些软件包依赖于 pbr and many more wanted wheel:

pip install --no-index --find-links ./wheels/ pbr==5.5.1 wheel==0.36.2

我还需要下载 cMake 并将其添加到 wheels 文件夹

之后我可以安装我的 requirements.txt:

pip install --no-index --find-links ./wheels/ -r /root/requirements.txt --ignore-installed

聚会可能迟到了,但我在尝试使用 Python 2.7.6(缺少 SNI 支持)发出 HTTPS 请求时发生了类似的事情。这在我工作的远程 Web 服务器上引起了很多问题。

寻找答案我尝试安装 urllib3[secure] 并进入了一个漏洞,因为 pip 抱怨缺乏 SNI 支持来安装这个和其他包。

我发现了这个 Whosebug answer 它帮助我安装了使 Python 2.7.6 和 pip 本身支持 SNI 以及安装 urllib[secure].[=16= 所需的依赖项]

您需要创建一个包含所需轮子的文件夹(例如使用 wget 从 PyPi 下载它们):

pip, asn1crypto, enum34, idna, six, ipaddress, pyOpenSSL, cffi, cryptography wheels; and also pycparser (a non-wheel, it will be a tar.gz)

确保你下载的 wheels 支持 Python 2.7 并且你在安装其他 wheel 之前安装 pip。

在原始答案中,它声明您可以使用 python -m OpenSSL.debug 来验证一切是否正常(ModuleNotFoundError 表示未安装 pyOpenSSL 包)。您还可以使用 pip -V 检查新的 pip 版本是否也已正确安装。

更新 pip 并安装这些依赖项后,我能够安装 urllib3[secure] 并从 python 和 pip 获得 SNI 支持。

祝你好运!