Python 3.x Python错误(ValueError:\u getfullpathname:嵌入的空字符)

Python 3.x Python错误(ValueError:\u getfullpathname:嵌入的空字符),python-3.x,matplotlib,Python 3.x,Matplotlib,我不知道如何修复它请帮忙,我已经尝试了帖子中提到的一切,但没有运气。我是python的新手,正在自学具体细节,非常感谢 控制台: Traceback (most recent call last): from matplotlib import pyplot File "C:\Users\...\lib\site-packages\matplotlib\pyplot.py", line 29, in <module> import matplotlib.c

我不知道如何修复它请帮忙,我已经尝试了帖子中提到的一切,但没有运气。我是python的新手,正在自学具体细节,非常感谢

控制台:

 Traceback (most recent call last):
     from matplotlib import pyplot
   File "C:\Users\...\lib\site-packages\matplotlib\pyplot.py", line 29, in <module>
     import matplotlib.colorbar
   File "C:\Users\...\lib\site-packages\matplotlib\colorbar.py", line 34, in <module>
     import matplotlib.collections as collections
   File "C:\Users\...\lib\site-packages\matplotlib\collections.py", line 27, in <module>
     import matplotlib.backend_bases as backend_bases
   File "C:\Users\...\lib\site-packages\matplotlib\backend_bases.py", line 62, in <module>
     import matplotlib.textpath as textpath
   File "C:\Users\...\lib\site-packages\matplotlib\textpath.py", line 15, in <module>
     import matplotlib.font_manager as font_manager
   File "C:\Users\...\lib\site-packages\matplotlib\font_manager.py", line 1421, in <module>
     _rebuild()
   File "C:\Users\...\lib\site-packages\matplotlib\font_manager.py", line 1406, in _rebuild
     fontManager = FontManager()
   File "C:\Users\...\lib\site-packages\matplotlib\font_manager.py", line 1044, in __init__
     self.ttffiles = findSystemFonts(paths) + findSystemFonts()
   File "C:\Users\...\lib\site-packages\matplotlib\font_manager.py", line 313, in findSystemFonts
     for f in win32InstalledFonts(fontdir):
   File "C:\Users\...\lib\site-packages\matplotlib\font_manager.py", line 231, in win32InstalledFonts
     direc = os.path.abspath(direc).lower()
   File "C:\Users\...\lib\ntpath.py", line 535, in abspath
     path = _getfullpathname(path)
 ValueError: _getfullpathname: embedded null character

我认为您没有正确地应用修补程序:如果应用了,则不应该在错误堆栈中提到
direc=os.path.abspath(direc.lower()
,因为修补程序已将其删除

为了清楚起见,以下是应用修补程序后,matplotlib 2.0.0版本中
C:\Anaconda\envs\py35\Lib\site packages\matplotlib\font\u manager.py(或安装了Anaconda的任何位置)中的整个
win32InstalledFonts()
方法:

def win32InstalledFonts(directory=None, fontext='ttf'):
    """
    Search for fonts in the specified font directory, or use the
    system directories if none given.  A list of TrueType font
    filenames are returned by default, or AFM fonts if *fontext* ==
    'afm'.
    """

    from six.moves import winreg
    if directory is None:
        directory = win32FontDirectory()

    fontext = get_fontext_synonyms(fontext)

    key, items = None, {}
    for fontdir in MSFontDirectories:
        try:
            local = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, fontdir)
        except OSError:
            continue

        if not local:
            return list_fonts(directory, fontext)
        try:
            for j in range(winreg.QueryInfoKey(local)[1]):
                try:
                    ''' Patch fixing [Error on import matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC)](https://stackoverflow.com/a/34007642/395857)
                    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 = os.path.abspath(direc).lower()
                    '''
                    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]


                    if os.path.splitext(direc)[1][1:] in fontext:
                        items[direc] = 1
                except EnvironmentError:
                    continue
                except WindowsError:
                    continue
                except MemoryError:
                    continue
            return list(six.iterkeys(items))
        finally:
            winreg.CloseKey(local)
    return None

要在font_manager py中找到此选项,请执行以下操作:

direc = os.path.abspath(direc).lower()
将其更改为:

direc = direc.split('\0', 1)[0]

并保存以应用到您的文件中

问题的实际原因似乎在
os.path.abspath()
中。更好的解决方案可能是编辑
\Lib\ntpath.py
,如中所述


基本上,将
ValueError:
异常处理程序添加到abspath()函数的Windows版本中。这在调用堆栈的下方,可以避免您在其他地方遇到此问题。

请注意,它适用于Python 2。我在您的帖子中添加了一个完整的回溯,从
import matplotlib.pyplot as plt
您已经链接()到了这个问题的解决方案;你对上面提到的修复有完全相同的错误吗?@KeyWeeUsr我删除了赏金。如果你愿意的话,可以在另一篇文章中随意添加。
direc = direc.split('\0', 1)[0]