15

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

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

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

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.

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.

I would appreciate some thoughts or advice.

Community
  • 1
  • 1
ComputerScientist
  • 936
  • 4
  • 12
  • 20

6 Answers6

11

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.

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. 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.

Hack itself:

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

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.

Lav
  • 2,204
  • 12
  • 23
  • OK, looks like if I assign `sys.version` and get rid of text within (and including) the vertical bars, then `platform.python_implementation()` works. How can I fix this long-term? I assume `sys.version` is assigned somewhere where sys is implemented? I'll search for that in the anaconda directory. Is there anything I should be aware of in particular as I do this? – ComputerScientist Dec 13 '15 at 00:32
  • OK, I think your hack seemed to work, I put it in `conda/cli/main.py`. Unfortunately, it looks like I'm now getting `AttributeError: 'module' object has no attribute 'sslwrap'`. Looks like the problem is my python version (2.7.10) is higher than 2.7.8. Seriously, how are people supposed to use anaconda and conda on Linux if this happens? – ComputerScientist Dec 13 '15 at 01:16
  • @ComputerScientist `sys.version` is defined by Python executable. A long-term solution would be to make Anaconda developers aware of this issue and to wait for them to fix it. If you wish to develop a fix yourself, try searching Anaconda code for places where it uses `sys.version` string. Maybe they only modified it for visuals sake and you can remove the patch without any issues. Worst case, Anaconda actually uses the modified value of `sys.version` for something, and then you'll have to patch in an alternative solution with same functionality that doesn't affect `sys.version`. – Lav Dec 13 '15 at 14:40
  • 1
    I resolved this by switching over to Python 3.5. Sorry if that's a lame solution, but I didn't want to get deep into the code when switching Python versions would work (and the application I'm using this for can use Python 2.7.10 or 3.5). But the point is that your hack *does* work and would be the first step if I wanted to continue with Python 2.7.10. – ComputerScientist Dec 13 '15 at 18:16
  • I was having this issue when importing pandas. I fixed it by copying the answer's snippet into my python script before the import pandas line. – Roger Oba Sep 04 '18 at 15:49
9

Just had the same issue on Windows, fixed it by changing the PythonPath to the Anaconda installation (I had a previous installation of Python).

TheSwiftHippo
  • 91
  • 1
  • 3
  • 1
    I initially installed Python 2.7 in C:\Python27 and later I installed Anaconda. I got the same error message as the original poster. I had to edit the environment variables and remove the python paths pointing to C:\Python27 and I also added the path where Anaconda is installed. – Nick_F Feb 27 '16 at 10:54
  • In Windows 10, I installed 2.7 after installing Anaconda. The installer automatically sets your Python path variable. – Jesuisme Apr 04 '16 at 17:21
2

I found another solution for this issue. I was getting the same issue while opening the Spyder on MAC. here is the solution which worked for me.

  • First remove the existing Spyder from the system conda remove spyder

Clone the Directory and run the setup

This will open the spyder instance.

Vaibhav K
  • 2,762
  • 3
  • 21
  • 22
1

I had the same issue under Linux after a system upgrade. I managed to get ride of this message ("failed to parse CPython sys.version") by deleting the ~/.local directory. Can be useful maybe...

dputhier
  • 734
  • 1
  • 7
  • 23
1

Just install proper version of python. For me it worked when i installed Python 3.6.5. Use this command

  • conda install Python=3.6.5

after finishing the installation Run spyder again.

Vaibhav K
  • 2,762
  • 3
  • 21
  • 22
1

I had this error (ValueError: failed to parse CPython sys.version) when trying to open Spyder after installing FiPy using the following line from the FiPy website:

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

The error originates from a package called 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))

Running the following line of code in Terminal resolved it (from a Spyder maintainer on GitHub):

conda update --name <MYFIPYENV> python pyzmq python.app
drpm
  • 489
  • 5
  • 11