Python error (ValueError: _getfullpathname: embedded null character)

I dont know. How to fix this, please help, I tried everything that was mentioned in the message Error importing matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC) , but no luck. I am new to python and myself studying specific details that will be greatly 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() 
+5
source share
3 answers

To find this in py font_manager:

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

change it to:

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

and save for use in your file.

+2
source

I don’t think that you applied the correction in Error when importing matplotlib.pyplot (on Anaconda3 for Windows 10 Home 64-bit PC) correctly: if you should not mention direc = os.path.abspath(direc).lower() in your error stack direc = os.path.abspath(direc).lower() since the patch removed it.

To be clear, here is the whole win32InstalledFonts() method in C:\Anaconda\envs\py35\Lib\site-packages\matplotlib\font_manager.py (or wherever Anaconda is installed) after applying the patch, 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 
0
source

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

Basically, add a ValueError: exception handler to the version of the abspath () function of Windows. This is lower in the call stack and may save you from running into this problem elsewhere.

0
source

Source: https://habr.com/ru/post/1258494/


All Articles