2021 年如何在 Python 2.7 上安装 pip

Jon*_*Rys 7 pip pypi python-2.7 ubuntu-14.04

我有仍在运行 Python 2.7.6 的旧生产服务器。我们有一个从 ubuntu 14.04 的 docker 镜像构建的本地环境,旨在复制该环境(安装完所有内容后,一切仍然可以正常工作。)创建此环境的打包器构建脚本最近显然停止工作,因为 PyPi 放弃了非 SNI 支持。

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

wget -c https://bootstrap.pypa.io/pip/2.7/get-pip.py
python2 get-pip.py
Run Code Online (Sandbox Code Playgroud)

这给了我以下警告:

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
Run Code Online (Sandbox Code Playgroud)

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

我没有 pip,所以我使用安装了旧版本

apt-get install python-pip
Run Code Online (Sandbox Code Playgroud)

这将安装 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...
Run Code Online (Sandbox Code Playgroud)

如果我尝试pip install "urllib3[secure]" --upgrade或者pip 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
Run Code Online (Sandbox Code Playgroud)

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

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

pip install uWSGI
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

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
Run Code Online (Sandbox Code Playgroud)

升级 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
Run Code Online (Sandbox Code Playgroud)

重新安装 pip 不起作用:

python -m pip install -U --force-reinstall pip
Run Code Online (Sandbox Code Playgroud)

给我:

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
Run Code Online (Sandbox Code Playgroud)

如果我打开,/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 !!!]]]
Run Code Online (Sandbox Code Playgroud)

该链接显示 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.
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*Rys 4

正如消息所述,从 2021 年 5 月 6 日起, PyPi 已停止对 Python <2.7.9 的支持。如果您运行的版本 <2.7.9 并且无法升级到较新版本的 Python,那么您唯一的选择是手动下载来自PyPi的轮子。

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

我需要安装software-properties-commongcc

apt-get install -y software-properties-common gcc
Run Code Online (Sandbox Code Playgroud)

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

python ./setuptools-44.1.1/setup.py install
Run Code Online (Sandbox Code Playgroud)

接下来,我下载了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
Run Code Online (Sandbox Code Playgroud)

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

pip download -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

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

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

python pip install --no-index --find-links ./wheels/ -r /root/requirements.txt
Run Code Online (Sandbox Code Playgroud)

这发现了一些我尚未下载软件包的依赖项,因此我必须仔细检查并下载它们并将它们添加到文件夹中wheels。我发现还有一些其他东西需要与我最初下载的版本不同的版本,并且一些软件包依赖于pbr和更多想要的Wheel

pip install --no-index --find-links ./wheels/ pbr==5.5.1 wheel==0.36.2
Run Code Online (Sandbox Code Playgroud)

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

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

pip install --no-index --find-links ./wheels/ -r /root/requirements.txt --ignore-installed
Run Code Online (Sandbox Code Playgroud)