I am getting the error "ImportError: No module named Tkinter" but don't know why.
When I run my script I am getting this error but that is usually do to a library you are trying to use that you have not imported. I have searched and all of the posts I have found are directly related to someone ether messing up "Tkinter" with "tkinter" python 2 and 3, or have not imported the library the right way. I have ran this script from the command line on ubuntu 14.04, Spyder on the same Ubuntu box, and also on the Cloudera Data Science workbench and it worked fine. On all of these machines I have not imported the library and it works flawlessly.
When I try to run it on my rhel 7 box I am getting the following error:
root#rhel7_box:/home/user/4688_events_PC-Tags_last_7_days# python 4688_events_PC-Tags_last_7_days_NN_FromMergedHash.py
Traceback (most recent call last):
File "4688_events_PC-Tags_last_7_days_NN_FromMergedHash.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
from . import tkagg # Paint image to Tk photo blitter extension.
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
from six.moves import tkinter as Tk
File "/usr/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
ImportError: No module named Tkinter
It seems to be getting the error when it hits "import matplotlib.pyplot as plt".
These are the libraries that I am using that work fine everywhere else:
#Libraries
import pandas as pd
import numpy as np
import hashlib
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import confusion_matrix
tkinter is used as a UI processing backend for matplotlib. It should be shipped with python but might not be on your system for various reasons. Chances are you you have agg, (should ship with ubuntu) which you can use instead.
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
You can of course still use tkinter if you install it on your box. If you prefer that for some reason I can let you know but it requires sudo.
Related
When I was trying to import matplotlib, I wrote
import matplotlib.pyplot as plt
in my code.
and this error occured.
Traceback (most recent call last):
File "C:\aiProjects\opencv\test.py", line 2, in <module>
import matplotlib.pyplot as plt
File "C:\Users\blackhao\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\__init__.py", line 113, in <module>
from . import _api, _version, cbook, _docstring, rcsetup
File "C:\Users\blackhao\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\rcsetup.py", line 27, in <module>
from matplotlib.colors import Colormap, is_color_like
File "C:\Users\blackhao\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\colors.py", line 56, in <module>
from matplotlib import _api, _cm, cbook, scale
File "C:\Users\blackhao\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\scale.py", line 22, in <module>
from matplotlib.ticker import (
File "C:\Users\blackhao\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\ticker.py", line 138, in <module>
from matplotlib import transforms as mtransforms
File "C:\Users\blackhao\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\transforms.py", line 49, in <module>
from matplotlib._path import (
ImportError: DLL load failed while importing _path: The specified module could not be found.
Process finished with exit code 1
I tried several ways such as reinstalling matplotlib and pasting new "Msvcp71.dll" and "Msvcr71.dll" files in my system folders. Anyways to solve this problem?
... reinstall the matplotlib python package with this argument --ignore-installed:
pip3 install matplotlib --user --ignore-installed
I am attempting to build an exe file that utilizes statsmodels via PyInstaller, from the terminal. The exe file is built by PyInstaller without error--however when run I get the following error:
ModuleNotFoundError: No module named 'statsmodels.__init__._version'
I have tried installing the statsmodels hook in the PyInstaller "hooks" directory, as described here, however that does not work. I have also attempted to explicitly import statsmodels.init._version in my Python code, but that has also not worked. Below is the top of my code where I import the packages:
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.neighbors import NearestNeighbors, KNeighborsClassifier
import numpy as np
import matplotlib.pylab as plt
from sklearn.linear_model import LogisticRegression
import statsmodels.api as sm
from scipy import stats
from sklearn import datasets, neighbors
from mlxtend.plotting import plot_decision_regions
import seaborn as sns
from tqdm import tqdm
import matplotlib.ticker as mtick
from tkinter import *
from tkinter.filedialog import askopenfilename
import sklearn.utils._cython_blas
import sklearn.neighbors.typedefs
import statsmodels.tsa.statespace._filters
import statsmodels.tsa.statespace._filters._conventional
import statsmodels.tsa.statespace._filters._univariate
import statsmodels.tsa.statespace._filters._univariate_diffuse
import statsmodels.tsa.statespace._filters._inversions
import statsmodels.tsa.statespace._smoothers
import statsmodels.tsa.statespace._smoothers._conventional
import statsmodels.tsa.statespace._smoothers._univariate
import statsmodels.tsa.statespace._smoothers._univariate_diffuse
import statsmodels.tsa.statespace._smoothers._classical
import statsmodels.tsa.statespace._smoothers._alternative
import statsmodels.__init__._version
Here is the traceback of the error:
File "LR.py", line 9, in <module>
File "/opt/anaconda3/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "statsmodels/api.py", line 32, in <module>
File "/opt/anaconda3/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "statsmodels/__init__.py", line 2, in <module>
ModuleNotFoundError: No module named 'statsmodels.__init__._version'
What can I do to fix this so that the exe file includes the statsmodels package and can be run?
statsmodels version is accessed through the __version__ attribute.
import statsmodels
print(statsmodels.__version__)
which shows
v0.12.0rc0+20.g98dfc0073
on my system.
You should replace import statsmodels.__init__._version with from statsmodels.__init__ import __version__
I've seen so many questions about this error online, but even after reading through all of them, I have no idea why I am still getting this error. I have spicy installed, and I am completely lost as to what to do. All I'm trying to do is get an example working that I found online, but this ModuleError keeps appearing.
Here are the imports below:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV
Traceback (most recent call last):
File "/Users/.../PycharmProjects/SVM/venv/example1.py", line 11, in <module>
from sklearn.datasets import load_iris
File "/anaconda3/lib/python3.7/site-packages/sklearn/datasets/__init__.py", line 48, in <module>
from ._olivetti_faces import fetch_olivetti_faces
File "/anaconda3/lib/python3.7/site-packages/sklearn/datasets/_olivetti_faces.py", line 20, in <module>
from scipy.io.matlab import loadmat
File "/anaconda3/lib/python3.7/site-packages/scipy/io/__init__.py", line 97, in <module>
from .matlab import loadmat, savemat, whosmat, byteordercodes
ModuleNotFoundError: No module named 'scipy.io.matlab'
Any suggestions would be appreciated so much!! I am using Mac OS, and I have 3.7 python installed through Anaconda.
Try creating a fresh environment and install your packages with conda.
conda create -n testenv python=3.7 matplotlib numpy scikit-learn scipy
Then run your code within this environment.
I am using pycharm for python in windows 8. I have installed the package matplotlib.
I am using python 2.7
when i do import matplotlib there is no error.
However if i do matplotlib.pyplot as plt i am getting this following errors:
Traceback (most recent call last):
File "C:/Users/PiyushSudip/PycharmProjects/myFirst/test1.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 23, in <module>
from matplotlib.figure import Figure, figaspect
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 18, in <module>
from axes import Axes, SubplotBase, subplot_class_factory
File "C:\Python27\lib\site-packages\matplotlib\axes\__init__.py", line 4, in <module>
from ._subplots import *
File "C:\Python27\lib\site-packages\matplotlib\axes\_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 18, in <module>
from matplotlib.cbook import _string_to_bool, mplDeprecation
ImportError: cannot import name _string_to_bool
can anyone help figure out what i am doing wrong?
http://matplotlib.org/users/installing.html#required-dependencies
There is a package called six which matplotlib depends on. Check to make sure it's installed, or install it from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#six
This site has packaged versions of many python modules which are easy to setup on windows, just download and run.
Also check the other dependencies to make sure you have them all, otherwise you might get another error for a different module
I have NumPy and SciPy installed and working. I'm using Python 2.5 (MIT Intro to Programming course) so I've been installing the packages with .exe files instead of Pip or Anaconda. With MatPlotLib, I get the following errors during installation:
could not create: matplotlib-py2.5
could not create: python 2.5 matplotlib-1.0.0
could not set key value: "C:\Python25\Removematplotlib.exe" -u "C:\Python25\matplotlib-wininst.log"
I have a MatPlotLib folder in Python's Lib/site-packages directory. Importing matplotlib into IDLE works fine. But when I try to import pylab:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pylab
File "C:\Python25\Lib\site-packages\pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python25\Lib\site-packages\matplotlib\pylab.py", line 216, in <module>
from matplotlib import mpl # pulls in most modules
File "C:\Python25\Lib\site-packages\matplotlib\mpl.py", line 1, in <module>
from matplotlib import artist
File "C:\Python25\Lib\site-packages\matplotlib\artist.py", line 6, in <module>
from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
File "C:\Python25\Lib\site-packages\matplotlib\transforms.py", line 34, in <module>
from matplotlib._path import affine_transform
ImportError: DLL load failed: The specified module could not be found.
I've looked around on different message boards but can't find anyone else with this issue.
Any ideas?