Error on import matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC)

后端 未结 2 2120
时光说笑
时光说笑 2020-12-08 15:57

I recently installed \"Anaconda3 for Windows v2.4.0\" on my Windows 10 Home (64 bit) machine.

(I downloaded the Windows 64-bit Graphical Installer \"Anaconda3-2.4.0-

相关标签:
2条回答
  • 2020-12-08 16:25

    This is a bug in python, not matplotlib.

    The issue is that winreg.EnumValue is not cutting string values at their length properly for some reason, and strings will include null characters which os.path.abspath is not able to process.

    The registry entry where this happens is at SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts. Despite this not being matplotlib's fault we can still temporarily patch it so that it ends the string at '\0'. In font_manager.py, patch line 310 in the win32InstalledFonts() function to:

    key, direc, any = winreg.EnumValue( local, j)
    if not is_string_like(direc):
        continue
    if not os.path.dirname(direc):
        direc = os.path.join(directory, direc)
    direc = direc.split('\0', 1)[0]
    
    0 讨论(0)
  • 2020-12-08 16:31

    I'm using Python 3.5.2. When I tried @simonzack solution, I still got an error. I narrowed down the exception to the <python>/Lib/ntpath.py file. Look for the definition of the abspath() function around line 530.

    I added @simonzack's solution to a ValueError exception handler. Insert the following code after line 537:

        530:    def abspath(path):
        531:    """Return the absolute version of a path."""
    
        533:    if path: # Empty path must return current working directory.
        534:        try:
        535:            path = _getfullpathname(path)
        536:        except OSError:
        537:            pass # Bad path - return unchanged.
        NEW:        except ValueError:
        NEW:            path = path.split('\0', 1)[0]
        NEW:            path = _getfullpathname(path)
        538:    elif isinstance(path, bytes):
        539:        path = os.getcwdb()
        540:    else:
        541:        path = os.getcwd()
        542:    return normpath(path)
    

    That fixed the error for me.

    For a little history on the cause of this issue take a look at: Python Issue 25778. It's a bit long but the final conclusion is that the fix didn't make it into 2.7.14, 3.5.3 or 3.6.0. So it appears this hack is going to be our only solution for older versions of Python.

    0 讨论(0)
提交回复
热议问题