当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《ValueError:使用conda命令后无法解析CPythonsys.version》经验,为你挑选了2个好方法。

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

我正在远程连接到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

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

$ conda --version
conda 3.18.3

按照测试驱动说明,我更新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 
    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帖子.这个建议从头开始重新安装python和pycharm(但我刚安装了Anaconda,我不使用pycharm).另一个建议重新安装遮篷,但我不是在这里使用它.最后,第三个人认为它实际上是一个bug,并提出了一个修复方案.不幸的是,重命名sys.version无法解决错误.这甚至不是我的电脑,所以我不想深入研究代码并冒险搞砸了.

我会很感激一些想法或建议.



1> Lav..:

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

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

    备份Anaconda修改的sys.version.

    将其替换为合法的版本字符串.

    调用任何platform解析sys.version字符串的模块函数.

    为Anaconda修改的sys.version复制它的缓存.

    从备份恢复sys.version.

哈克本身:

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.py,conda/api.py或者conda/connection.py.



2> 小智..:

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

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有