ValueError:使用conda命令后无法解析CPython sys.version

时间:2015-12-08 00:19:27

标签: python anaconda cpython conda

我遇到了一个我无法解决的错误,尽管其他人报告了同样的错误。

我正在远程连接到Linux机器。我安装了最新版本的anaconda:

$ bash Anaconda2-2.4.0-Linux-x86_64.sh

// A lot of python libraries get installed

installing: _cache-0.0-py27_x0 ...
Python 2.7.10 :: Continuum Analytics, Inc.
creating default environment...
installation finished. 

我更新了相应的路径,看起来似乎有效:

$ python
Python 2.7.10 |Anaconda 2.4.0 (64-bit)| (default, Oct 19 2015, 18:04:42) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

很好,所以现在我想使用预先安装了Anaconda的conda。看起来Anaconda给了我3.18.3:

$ conda --version
conda 3.18.3

test drive instructions之后,我更新了conda:

$ conda update conda
Fetching package metadata: An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

    https://github.com/conda/conda/issues

Include the output of the command 'conda info' in your report.


Traceback (most recent call last):
  File "/code/anaconda2-4-0/bin/conda", line 5, in <module>
    sys.exit(main())
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/cli/main.py", line 195, in main
    args_func(args, p)
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/cli/main.py", line 202, in args_func
    args.func(args, p)
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/cli/main_update.py", line 48, in execute
    install.install(args, parser, 'update')
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/cli/install.py", line 239, in install
    offline=args.offline)
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/cli/common.py", line 598, in get_index_trap
    return get_index(*args, **kwargs)
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/api.py", line 42, in get_index
    unknown=unknown)
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/utils.py", line 119, in __call__
    value = self.func(*args, **kw)
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/fetch.py", line 237, in fetch_index
    session = CondaSession()
  File "/code/anaconda2-4-0/lib/python2.7/site-packages/conda/connection.py", line 61, in __init__
    super(CondaSession, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 272, in __init__
    self.headers = default_headers()
  File "/usr/lib/python2.7/dist-packages/requests/utils.py", line 555, in default_headers
    'User-Agent': default_user_agent(),
  File "/usr/lib/python2.7/dist-packages/requests/utils.py", line 524, in default_user_agent
    _implementation = platform.python_implementation()
  File "/usr/lib/python2.7/platform.py", line 1521, in python_implementation
    return _sys_version()[0]
  File "/usr/lib/python2.7/platform.py", line 1486, in _sys_version
    repr(sys_version))
ValueError: failed to parse CPython sys.version: '2.7.10 |Anaconda 2.4.0 (64-bit)| (default, Oct 19 2015, 18:04:42) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'

不幸的是,我无法弄清楚如何避免这个错误。

我发现了一些其他StackOverflow帖子。 This one建议从头开始重新安装python和pycharm(但我刚刚安装了Anaconda而且我没有使用pycharm)。 Another suggests重新安装天篷,但我不是在这里使用它。最后,a third suggests它实际上是一个bug,并提出修复。不幸的是,重命名sys.version无法解决错误。这甚至都不是我的电脑,所以我不想深入研究代码并冒险搞砸了。

我很感激一些想法或建议。

6 个答案:

答案 0 :(得分:10)

第三种解决方案是正确的。这确实是一个错误,因为修改后的sys.version字符串会破坏许多依赖于具有特定格式的字符串的platform模块函数。

但是,如果您无法正确修复它,您可以尝试一种hacky解决方法。模块platform实际上有一个用于解析sys.version字符串的缓存:所以你需要做的是:

  1. 备份Anaconda-modified sys.version。
  2. 将其替换为合法版本字符串。
  3. 调用任何解析sys.version字符串的platform模块函数。
  4. 也为Anaconda-modified sys.version复制了它的缓存。
  5. 从备份恢复sys.version。
  6. 哈克本身:

    try:
        import sys # Just in case
        start = sys.version.index('|') # Do we have a modified sys.version?
        end = sys.version.index('|', start + 1)
        version_bak = sys.version # Backup modified sys.version
        sys.version = sys.version.replace(sys.version[start:end+1], '') # Make it legible for platform module
        import platform
        platform.python_implementation() # Ignore result, we just need cache populated
        platform._sys_version_cache[version_bak] = platform._sys_version_cache[sys.version] # Duplicate cache
        sys.version = version_bak # Restore modified version string
    except ValueError: # Catch .index() method not finding a pipe
        pass
    

    您需要将此代码放在将要执行的位置之前 conda有可能因异常而失败。不确定最佳位置,但您可以使用conda/cli/main.pyconda/api.pyconda/connection.py进行尝试。

答案 1 :(得分:6)

在Windows上遇到同样的问题,通过将PythonPath更改为Anaconda安装(我之前安装了Python)修复了它。

答案 2 :(得分:1)

我找到了解决此问题的另一种解决方案。我在MAC上打开Spyder时遇到了同样的问题。这是适合我的解决方案。

  • 首先从系统中删除现有的Spyder conda remove spyder

克隆目录并运行设置

这将打开spyder实例。

答案 3 :(得分:0)

系统升级后,我在Linux下遇到了同样的问题。我设法通过删除〜/ .local目录来获取此消息(“无法解析CPython sys.version”)。也许有用......

答案 4 :(得分:0)

只需安装正确版本的python即可。 对我来说,它安装了Python 3.6.5。 使用此命令

  • conda install Python = 3.6.5

完成安装后再次运行spyder。

答案 5 :(得分:0)

使用following line from the FiPy website安装FiPy后尝试打开Spyder时出现此错误(ValueError: failed to parse CPython sys.version

conda create --name <MYFIPYENV> --channel guyer --channel conda-forge fipy nomkl 

该错误源自名为zmq的软件包:

File "/Users/user/anaconda3/lib/python3.6/site-packages/spyder/utils/introspection/plugin_client.py", line 18, in
import zmq
File "/Users/user/anaconda3/lib/python3.6/site-packages/zmq/init.py", line 34, in
from zmq import backend
File "/Users/user/anaconda3/lib/python3.6/site-packages/zmq/backend/init.py", line 21, in
if platform.python_implementation() == 'PyPy':
File "/Users/user/anaconda3/lib/python3.6/platform.py", line 1234, in python_implementation
return _sys_version()[0]
File "/Users/user/anaconda3/lib/python3.6/platform.py", line 1192, in _sys_version
repr(sys_version))

在终端中运行以下代码行即可解决(from a Spyder maintainer on GitHub):

conda update --name <MYFIPYENV> python pyzmq python.app