导入matplotlib.pyplot时出错(在Windows 10 Home 64位PC的Anaconda3上)

我最近在Windows 10 Home(64位)机器上安装了“Anaconda3 for Windows v2.4.0”。

(我从https://www.continuum.io/downloads下载了Windows 64位graphics安装程序“Anaconda3-2.4.0-Windows-x86_64.exe”(392 MB)。)

在一个命令提示符窗口中,我做了conda“testing驱动”,包括“conda update conda”等。最后,我看到以下内容:

C:\Users\Anshul\Downloads\Python>conda update conda Fetching package metadata: .... # All requested packages already installed. # packages in environment at C:\Anaconda3: # conda 3.18.6 py35_0 defaults C:\Users\Anshul\Downloads\Python>conda list matplotlib # packages in environment at C:\Anaconda3: # matplotlib 1.5.0 np110py35_0 defaults 

安装似乎已经成功 – 例如:

 C:\Users\Anshul\Downloads\Python>python Python 3.5.0 |Anaconda 2.4.0 (64-bit)| (default, Nov 7 2015, 13:15:24) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World") Hello World >>> import os >>> os.getcwd() 'C:\\Users\\Anshul\\Downloads\\Python' >>> import matplotlib as mpl >>> print(mpl.__version__) 1.5.0 >>> 

请注意,matplotlib导入罚款以上。 但是,当我尝试导入“matplotlib.pyplot”时出现错误消息,如下所示:

 >>> import matplotlib.pyplot as pp Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 1412, in <module> fontManager = pickle_load(_fmcache) File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 963, in pickle_load with open(filename, 'rb') as fh: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Anshul\\.matplotlib\\fontList.py3k.cache' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 29, in <module> import matplotlib.colorbar File "C:\Anaconda3\lib\site-packages\matplotlib\colorbar.py", line 34, in <module> import matplotlib.collections as collections File "C:\Anaconda3\lib\site-packages\matplotlib\collections.py", line 27, in <module> import matplotlib.backend_bases as backend_bases File "C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 62, in <module> import matplotlib.textpath as textpath File "C:\Anaconda3\lib\site-packages\matplotlib\textpath.py", line 15, in <module> import matplotlib.font_manager as font_manager File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 1420, in <module> _rebuild() File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 1405, in _rebuild fontManager = FontManager() File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 1043, in __init__ self.ttffiles = findSystemFonts(paths) + findSystemFonts() File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 312, in findSystemFonts for f in win32InstalledFonts(fontdir): File "C:\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 231, in win32InstalledFonts direc = os.path.abspath(direc).lower() File "C:\Anaconda3\lib\ntpath.py", line 535, in abspath path = _getfullpathname(path) ValueError: _getfullpathname: embedded null character >>> 

我在文本编辑器中打开“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ font_manager.py”,并试图寻找错误的来源。 我认为这是事情出错的地方:

 >>> mpl.get_cachedir() 'C:\\Users\\Anshul\\.matplotlib' >>> mpl.get_configdir() 'C:\\Users\\Anshul\\.matplotlib' >>> 

在Windows资源pipe理器中,我看到“C:\ Users \ Anshul.matplotlib”文件夹是空的,因此“fontList.py3k.cache”文件的FileNotFoundError (我在“C:\ Anaconda3” “目录)。 这似乎是安装程序的问题(我认为),但我不知道如何解决它。 我会很感激任何帮助或指针。

(顺便说一句,我已经试过用googlesearch这个问题了,最近的那个是在2013年的时候报告过的: 无法导入matplotlib.pyplot#2320 ,它涉及WinPython-64bit-3.3.2.2在Windows 7 64位这个线程被closures了,注释:“closures,已经被修复了”,但是问题已经出现了,希望有一个简单的解决方法或者解决方法。

谢谢,
Anshul

这是Python中的一个错误,而不是matplotlib。

问题在于winreg.EnumValue并没有因为某种原因正确地剪切字符串值,字符串将包含os.path.abspath无法处理的空字符。

发生这种情况的注册表项是在SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts 尽管这不是matplotlib的错,但我们仍然可以暂时修补它,以便在'\0'处结束字符串。 font_manager.pywin32InstalledFonts()函数中的补丁行310用于:

 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] 

我正在使用Python 3.5.2。 当我尝试@simonzack解决方案时,我仍然有一个错误。 我缩小了<python>/Lib/ntpath.py文件的例外。 请在530行周围查找abspath()函数的定义。

我将@ simonzack的解决方案添加到ValueError异常处理程序。 在行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) 

为我解决了这个错误。

关于这个问题的原因,请看一下: Python Issue 25778 。 这有点长,但最后的结论是,修复没有把它变成2.7.14,3.5.3或3.6.0。 所以看来,这个黑客将成为我们老版本Python的唯一解决方案。