解决Anaconda执行pip等命令报alueError: failed to parse CPython sys.version错误的问题

问题简述: 环境:deepin15+Anaconda3,在执行pip时报错如下:

Traceback (most recent call last): File "/usr/share/Anaconda3/bin/pip", line 7, in from pip._internal import main File "/usr/share/Anaconda3/lib/python3.6/site-packages/pip/_internal/__init__.py", line 42, in from pip._internal import cmdoptions File "/usr/share/Anaconda3/lib/python3.6/site-packages/pip/_internal/cmdoptions.py", line 16, in from pip._internal.index import ( File "/usr/share/Anaconda3/lib/python3.6/site-packages/pip/_internal/index.py", line 15, in from pip._vendor import html5lib, requests, six File "/usr/share/Anaconda3/lib/python3.6/site-packages/pip/_vendor/requests/__init__.py", line 86, in from pip._vendor.urllib3.contrib import pyopenssl File "/usr/share/Anaconda3/lib/python3.6/site-packages/pip/_vendor/urllib3/contrib/pyopenssl.py", line 46, in import OpenSSL.SSL File "/usr/share/Anaconda3/lib/python3.6/site-packages/OpenSSL/__init__.py", line 8, in from OpenSSL import crypto, SSL File "/usr/share/Anaconda3/lib/python3.6/site-packages/OpenSSL/crypto.py", line 12, in from cryptography import x509 File "/usr/share/Anaconda3/lib/python3.6/site-packages/cryptography/x509/__init__.py", line 8, in from cryptography.x509.base import ( File "/usr/share/Anaconda3/lib/python3.6/site-packages/cryptography/x509/base.py", line 16, in from cryptography.x509.extensions import Extension, ExtensionType File "/usr/share/Anaconda3/lib/python3.6/site-packages/cryptography/x509/extensions.py", line 13, in from asn1crypto.keys import PublicKeyInfo File "/usr/share/Anaconda3/lib/python3.6/site-packages/asn1crypto/keys.py", line 22, in from ._elliptic_curve import ( File "/usr/share/Anaconda3/lib/python3.6/site-packages/asn1crypto/_elliptic_curve.py", line 51, in from ._int import inverse_mod File "/usr/share/Anaconda3/lib/python3.6/site-packages/asn1crypto/_int.py", line 52, in if platform.python_implementation() == 'PyPy': File "/usr/share/Anaconda3/lib/python3.6/platform.py", line 1257, in python_implementation return _sys_version()[0] File "/usr/share/Anaconda3/lib/python3.6/platform.py", line 1215, in _sys_version repr(sys_version)) ValueError: failed to parse CPython sys.version: '3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) \n[GCC 7.2.0]'

问题定位: /usr/share/Anaconda3/lib/python3.6/platform.py:1215:

# CPython match = _sys_version_parser.match(sys_version) if match is None : raise ValueError ( 'failed to parse CPython sys.version: %s ' % repr (sys_version)) version, buildno, builddate, buildtime, compiler = \ match.groups() _sys_version_parser:

_sys_version_parser = re.compile( r ' ([ \w.+ ] + ) \s * ' # "version" r ' \( # ? ([^ , ] + ) ' # "(#buildno" r ' (?: ,\s * ([ \w ] * ) ' # ", builddate" r ' (?: ,\s * ([ \w : ] * )) ? ) ?\) \s * ' # ", buildtime)" r ' \[ ([^ \] ] + ) \]? ' , re.ASCII) # "[compiler]" 发现此正则表达式未考虑Anaconda增加了"|Anaconda, Inc.|"的问题,需要过滤掉.
解决方法:
在_sys_version_parser中增加一行过滤掉"|Anaconda xxx|"等信息即可:

_sys_version_parser = re.compile( r ' ([ \w.+ ] + ) \s * ' # "version" r ' (?: \| [^ | ] *\| ) * \s * ' #去掉'|Anaconda xxx|'等,避免出现failed to parse CPython sys.version r ' \( # ? ([^ , ] + ) ' # "(#buildno" r ' (?: ,\s * ([ \w ] * ) ' # ", builddate" r ' (?: ,\s * ([ \w : ] * )) ? ) ?\) \s * ' # ", buildtime)" r ' \[ ([^ \] ] + ) \]? ' , re.ASCII) # "[compiler]"
【解决Anaconda执行pip等命令报alueError: failed to parse CPython sys.version错误的问题】

    推荐阅读