python - 使用conda命令后ValueError : failed to parse CPython sys.版本

标签 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

太好了,所以现在我想使用 conda,它预装了 Anaconda。看起来 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重新安装 canopy,但我没有在这里使用它。最后,a third suggests它实际上是一个错误,并提出修复建议。遗憾的是,重命名 sys.version 无法解决错误。这甚至不是我的电脑,所以我不想深入研究代码并冒着搞砸的风险。

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

最佳答案

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

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

  1. 备份 Anaconda 修改后的 sys.version。
  2. 将其替换为合法的版本字符串。
  3. 调用任何解析 sys.version 字符串的 platform 模块函数。
  4. 也为 Anaconda 修改的 sys.version 复制它的缓存。
  5. 从备份中恢复 sys.version。

self 破解:

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.

关于python - 使用conda命令后ValueError : failed to parse CPython sys.版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34145861/

相关文章:

python - 使用scrapy从网站返回值

installation - conda : "specifications were found to be in conflict";包安装失败

python-3.x - 使用 anaconda 在 mac 中为 python3 安装 pygame

python - 我无法在 Anaconda Windows 中安装 kivy

python - 确保所有 Django View 返回 200?

python - docopt 的选项多样性

python - 如何为 Python 程序分配更多内存?它在 4GB RAM 上消耗的内存不超过 64MB

python - C(P)ython 或 D 中的多平台 gui 应用程序

python - 参数指向 PySys_SetArgv() 的字符串是否应该保留在内存中直到 Py_Finalize()?

python - 为什么 Python 代码在函数中运行得更快?