RHEL服务器pip2故障,咨询pip3兼容性及跨Python版本安装模块可行性
问题背景
在部分RHEL Linux服务器上,此前一直使用pip2安装所有Python模块。近期发现pip2无法正常工作,执行pip2 list或pip2 install等命令时会出现以下错误:
示例(来自pip2 list,安装Python模块时也会出现相同错误):
Traceback (most recent call last): File "/usr/bin/pip", line 11, in <module> load_entry_point('pip==20.1.1', 'console_scripts', 'pip')() File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_internal/cli/main.py", line 73, in main command = create_command(cmd_name, isolated=("--isolated" in cmd_args)) File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_internal/commands/__init__.py", line 104, in create_command module = importlib.import_module(module_path) File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_internal/commands/install.py", line 24, in <module> from pip._internal.cli.req_command import RequirementCommand, with_cleanup File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_internal/cli/req_command.py", line 16, in <module> from pip._internal.index.package_finder import PackageFinder File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_internal/index/package_finder.py", line 21, in <module> from pip._internal.index.collector import parse_links File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_internal/index/collector.py", line 14, in <module> from pip._vendor import html5lib, requests File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_vendor/requests/__init__.py", line 97, in <module> from pip._vendor.urllib3.contrib import pyopenssl File "/usr/lib/python2.7/site-packages/pip-20.1.1-py2.7.egg/pip/_vendor/urllib3/contrib/pyopenssl.py", line 48, in <module> from cryptography.hazmat.backends.openssl import backend as openssl_backend File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/__init__.py", line 7, in <module> from cryptography.hazmat.backends.openssl.backend import backend File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 37, in <module> from cryptography.hazmat.backends.openssl.x509 import _Certificate File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/x509.py", line 24, in <module> class _Certificate(object): File "/usr/lib64/python2.7/site-packages/cryptography/utils.py", line 23, in register_decorator verify_interface(iface, klass) File "/usr/lib64/python2.7/site-packages/cryptography/utils.py", line 43, in verify_interface actual = inspect.getargspec(getattr(klass, method)) File "/usr/lib64/python2.7/inspect.py", line 815, in getargspec raise TypeError('{!r} is not a Python function'.format(func)) TypeError: <method-wrapper '__ne__' of type object at 0x35dad00> is not a Python function
随后安装了pip3,pip3可正常运行,能执行pip3 list及模块安装操作,示例如下:
pip3 list Package Version ---------- ------- pip 19.3.1 setuptools 41.6.0
疑问
能否使用pip3为Python2和Python3安装所有模块?pip3是否与pip2向后兼容?
回答
不能用pip3为Python2安装模块
pip3是绑定Python3的包管理器,它安装的模块只会被Python3环境识别和使用,无法被Python2加载。这是因为Python2和Python3的模块存储路径、字节码格式完全不兼容,pip3不会将模块安装到Python2的site-packages目录下。pip3与pip2不存在向后兼容
pip2和pip3是针对不同Python版本开发的工具,两者的命令逻辑、依赖处理、包索引适配都有差异。比如,很多Python2专属的包已不在pip3的索引中维护,而pip3支持的现代包格式(如PEP 517)也无法被Python2识别。此外,pip2的最后一个稳定版本是20.3.4,早已停止维护,而pip3仍在持续更新,两者没有官方兼容承诺。修复pip2的可行方案
你遇到的错误根源是pip20.1.1与系统中安装的cryptography版本不兼容,可尝试以下步骤修复:- 降级cryptography到Python2.7支持的最高兼容版本:
python2 -m pip install cryptography==2.8 - 如果上述命令无法执行,手动卸载现有cryptography包后重新安装兼容版本:
rm -rf /usr/lib64/python2.7/site-packages/cryptography* curl https://files.pythonhosted.org/packages/source/c/cryptography/cryptography-2.8.tar.gz | tar xzf - cd cryptography-2.8 python2 setup.py install - 若仍有问题,降级pip2到更稳定的版本:
python2 -m pip install pip==20.3.4
- 降级cryptography到Python2.7支持的最高兼容版本:
内容的提问来源于stack exchange,提问作者Judy
相关产品推荐
相关产品推荐

