ValueError:使用conda命令后无法解析CPython sys.version [英] ValueError: failed to parse CPython sys.version after using conda command

查看:165
本文介绍了ValueError:使用conda命令后无法解析CPython sys.version的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running into an error that I can't solve despite others having reported the same error.

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

I am connecting remotely to a Linux machine. I have installed the latest version of 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. 

我更新了相应的路径,并且看起来可行:

I updated the corresponding paths and it seems like it works:

$ 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:

Great, so now I want to use conda, which is pre-installed with Anaconda. It looks like Anaconda gave me 3.18.3:

$ conda --version
conda 3.18.3

按照测试驱动器说明,我更新conda:

Following the test drive instructions, I update 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)]'

不幸的是,我不知道如何避免此错误.

Unfortunately, I can't figure out how to avoid this error.

我发现了其他一些StackOverflow帖子. 建议从头开始重新安装python和pycharm(但我刚安装了Anaconda,而我不使用pycharm). 另一个建议重新安装顶篷,但是我我在这里不使用它.最后,三分之一建议它实际上是一个错误,并提出了解决方案.不幸的是,重命名sys.version无法解决该错误.这甚至都不是我的电脑,所以我不想深入了解代码并冒着弄乱某些东西的风险.

I found a few other StackOverflow posts. This one suggests reinstalling python and pycharm from scratch (but I just installed Anaconda and I don't use pycharm). Another suggests reinstalling canopy, but I'm not using that here. Finally, a third suggests that it's actually a bug, and proposes a fix. Unfortunately, re-naming sys.version fails to resolve the error. This isn't even my computer so I don't want to get deep into the code and risk messing something up.

我将不胜感激.

推荐答案

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

Third solution is correct. This is indeed a bug, since modified sys.version string breaks a number of platform module functions that rely on that string having certain format.

但是,如果无法正确修复它,则可以尝试采用变通办法.模块platform实际上具有用于解析sys.version字符串的缓存:因此,您要做的是:

However, if you cannot fix it properly, you can attempt a hacky workaround. Module platform actually has a cache for parsing sys.version string: so what you have to do is:

  1. 备份Anaconda修改的sys.version.
  2. 用合法的版本字符串替换它.
  3. 调用任何无法解析sys.version字符串的platform模块函数.
  4. 也为Anaconda修改的sys.version复制它的缓存.
  5. 从备份还原sys.version.
  1. Backup Anaconda-modified sys.version.
  2. Replace it with a legal version string.
  3. Make a call to any platform module function that unparses sys.version string.
  4. Duplicate it's cache for Anaconda-modified sys.version as well.
  5. Restore sys.version from backup.

黑客本身:

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.

You need to put this code somewhere where it will be executed before conda has a chance to fail with exception. Not sure what the best place would be, but you could try it with conda/cli/main.py, conda/api.py or conda/connection.py.

这篇关于ValueError:使用conda命令后无法解析CPython sys.version的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆