6

I don't know How to fix it please help, I have tried everything mentioned in the post Error on import matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC) but no luck. I'm a newbie to the python and am self learning specific details would greatly be appreciated.

Console:

 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

Python:

importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#importing dataset
dataset = pd.read_csv('Position_Salaries.csv')
x = dataset.iloc[:,1:2].values
y = dataset.iloc[:,2].values

#Linear Regression
from sklearn.linear_model import LinearRegression
reg_lin = LinearRegression()
reg_lin = reg_lin.fit(x,y)

#ploynomial Linear Regression
from sklearn.preprocessing import PolynomialFeatures
reg_poly = PolynomialFeatures(degree = 3)
x_poly = reg_poly.fit_transform(x)
reg_poly.fit(x_poly,y)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly,y)

#Visualizing Linear Regression results
plt.scatter(x,y,color = 'red')
plt.plot(x,reg_lin.predict(x), color = 'blue')
plt.title('Truth vs. Bluff (Linear Reg)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

#Visualizing Polynomial Regression results
plt.scatter(x,y,color = 'red')
plt.plot(x,lin_reg_2.predict(reg_poly.fit_transform(x)), color = 'blue')
plt.title('Truth vs. Bluff (Linear Reg)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
Community
  • 1
  • 1
  • 1
    Just a note, it works on Python 2. I added a full traceback to your post from `import matplotlib.pyplot as plt` – Peter Badida Nov 05 '16 at 11:21
  • 1
    You already linked (http://stackoverflow.com/q/34004063/3581217) to what seems to be a solution for this problem; do you have the exact same error with the fix mentioned there? – Bart Nov 06 '16 at 16:23
  • 1
    @KeyWeeUsr I removed the bounty. Feel free to add it on the other post, if you would like. – elixenide Nov 12 '16 at 02:58

3 Answers3

2

To find this in font_manager py:

direc = os.path.abspath(direc).lower()

change it into:

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

and save to apply in your file.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
beango
  • 63
  • 1
  • 1
  • 6
0

I don't think you applied the patch in Error on import matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC) correctly: if you had, there shouldn't be a mention of direc = os.path.abspath(direc).lower() in your error stack, since the patch removed it.

To be clear, here is the entire win32InstalledFonts() method in C:\Anaconda\envs\py35\Lib\site-packages\matplotlib\font_manager.py (or wherever Anaconda is installed) after the patch is applied, with matplotlib 2.0.0:

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
Community
  • 1
  • 1
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
0

The actual cause of the issue appears to be in os.path.abspath(). A better solution might be to edit <python dir>\Lib\ntpaths.py as detailed in Error on import matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC)

Basically, add a ValueError: exception handler to the Windows version of the abspath() function. This is lower down on the call stack and could save you from encountering this issue in other places.

Quantium
  • 1,779
  • 1
  • 14
  • 14