我最近安装了#34; Anaconda3 for Windows v2.4.0"在我的Windows 10 Home(64位)机器上。
(我从https://www.continuum.io/downloads下载了Windows 64位图形安装程序" Anaconda3-2.4.0-Windows-x86_64.exe"(392 MB)。)
在命令提示符窗口中,我执行了conda" Test Drive",包括" 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
>>>
我打开了&#34; C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ font_manager.py&#34;在文本编辑器中,并试图寻找错误的来源。我认为这是出错的地方:
>>> mpl.get_cachedir()
'C:\\Users\\Anshul\\.matplotlib'
>>> mpl.get_configdir()
'C:\\Users\\Anshul\\.matplotlib'
>>>
在Windows资源管理器中,我看到&#34; C:\ Users \ Anshul.matplotlib&#34;文件夹为空,因此&#34; fontList.py3k.cache&#34;的FileNotFoundError
文件(我不会在&#34; C:\ Anaconda3&#34;目录中的任何地方看到)。这似乎是安装程序的一个问题(我认为),但我不知道如何解决它。我很感激任何帮助或指示。
(顺便说一下,我已经尝试过谷歌搜索这个问题了。2013年报道了最接近的问题:fail to import matplotlib.pyplot #2320。它涉及在Windows 7 64上安装WinPython-64bit-3.3.2.2这个帖子已关闭了评论:&#34; Closing。已经在master中修复了。&#34;,但似乎问题已经重新出现。我希望有一个简单的修复或解决方法。)
谢谢,
Anshul
答案 0 :(得分:21)
这是python中的一个错误,而不是matplotlib。
问题是winreg.EnumValue
由于某种原因没有正确剪切字符串值,字符串将包含os.path.abspath
无法处理的空字符。
发生这种情况的注册表项位于SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
。尽管这不是matplotlib的错,但我们仍然可以暂时修补它,以便它在'\0'
结束字符串。在font_manager.py
中,win32InstalledFonts()
函数中的补丁行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]
答案 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。所以看来这个hack将成为旧版Python的唯一解决方案。