Unable to import matplotlib.pyplot - python

I have installed python 2.7.10 in CentOS 6.5. Then I installed matplotlib using pip. However, I am getting the following error when trying to import matplotlib.pyplot.
import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/local/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
tkinter seems to be installed.
"Package tkinter-2.6.6-66.el6_8.x86_64 already installed and latest version"
I have _tkinter.so module in python2.6/lib-dynload, but nothing related to tkinter under python2.7/lib-dynload.
/usr/lib64/python2.6/lib-dynload/_tkinter.so
/usr/local/lib/python2.7/lib-tk/test/test_tkinter
Also, I verified sys.path in python to get the following:
Python 2.7.10 (default, Jun 1 2017, 18:51:46)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages

This may be a problem with your python library path. Check to see where tkinter is installed, and make sure that you have that available to your Python interpreter. Can you import anything else from tkinter?

Try to install tkinter as they said ”no module named _tkinter“.
You can do so with that command:
sudo apt-get install python-tk

Related

Cannot import pywinauto on Windows 10

I installed pywinauto using pip install pywinauto.
OS: Windows 10
Python: 3.6.2
When I run python and try to import pywinauto, I get the error:
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pywinauto.application import Application
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\__init__.py", line 89, in <module>
from . import findwindows
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\findwindows.py", line 42, in <module>
from . import controls
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\controls\uiawrapper.py", line 47, in <module>
from ..uia_defines import IUIA
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 181, in <module>
pattern_ids = _build_pattern_ids_dic()
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 169, in _build_pattern_ids_dic
if hasattr(IUIA().ui_automation_client, cls_name):
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 50, in __call__
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 60, in __init__
self.UIA_dll = comtypes.client.GetModule('UIAutomationCore.dll')
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\client\_generate.py", line 118, in GetModule
mod = _CreateWrapper(tlib, pathname)
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\client\_generate.py", line 183, in _CreateWrapper
generate_module(tlib, ofi, pathname)
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\tools\tlbparser.py", line 750, in generate_module
gen.generate_code(list(items.values()), filename=pathname)
File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\tools\codegenerator.py", line 261, in generate_code
tlib_mtime = os.stat(self.filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'UIAutomationCore.dll'
Any ideas how to fix this?
I have the same issue today and fixed it by pip install comtypes==1.1.7.
It caused by comtypes library which release a new version 1.1.8 at Dec.26. Downgrade to previous version, it works well now.
It should be fixed in comtypes==1.1.9. Please check.
The problem was introduced in comtypes==1.1.8. My fault.
Try using a different version of python (e.g. 3.7.5 or 3.8):
https://github.com/pywinauto/pywinauto/issues/887
"Had the same issue with Python 3.7.6 and 3.8.1. Issue does not occur in 3.7.5 or 3.8.
I think this will be fixed in 3.8.2 ( but not sure yet)"
Try this, for me works fine
Install pywin32 from https://github.com/mhammond/pywin32/releases
pip install -U comtypes
pip install six
pip install Pillow
cd c:\windows\syswow64\
regsvr32 UIAutomationCore.dll
python
import pywinauto
#and the problem should be solved
I have Win10 and python 3.9..... i don't like Win10
Another thing could be your numpy library, uninstall it and install it again.
pip uninstall numpy
pip install numpy
Credits -> https://www.dll-files.com/download/65996a0afad353ec43ffe1ca69a2be88/uiautomationcore.dll.html?c=TTM2MGJJaHVRaEJkM1JyNDB3TDNwZz09

Python3 ImportError: No module named '_tkinter' on Ubuntu

The current version of Python3 is 3.5.2 when I import matplotlib it retuned the following error
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python3.5/site-packages/six.py", line 92, in __get__
result = self._resolve()
File "/usr/local/lib/python3.5/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python3.5/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
And import tkinter
Python 3.5.2 (default, Jan 19 2017, 11:29:22)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
>>>
It seems that tkinter has already been intalled.
I have installed tk and tcl by
sudo apt-get install tk-dev
sudo apt-get install tk8.6-dev
And OS is Ubuntu 14.04.
I think it is the reason that TK was not configured on Python3, but I'm not sure. Many people said I should rebuild and reinstall Python3 with tk, however I don't think it is an elegant way to solve this problem.
How can I fix this problem?
If you are having trouble with a matplotlib backend try selecting a different one.
Matplotlib caters for many different scenarios and uses.
On Linux, I use the following code to select whichever backend is available and works first.
import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
try:
matplotlib.use(gui,warn=False, force=True)
from matplotlib import pyplot as plt
break
except:
continue
or if you are going to be creating an image file rather than displaying it
Use:
matplotlib.use('agg')
from matplotlib import pyplot as plt
Edit:
Based on your comments try this and see if you get a result that works.
import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
print ("I will test for", gui_env)
for gui in gui_env:
print ("testing", gui)
try:
matplotlib.use(gui,warn=False, force=True)
from matplotlib import pyplot as plt
print (" ",gui, "Is Available")
plt.plot([1.5,2.0,2.5])
fig = plt.gcf()
fig.suptitle(gui)
plt.show()
print ("Using ..... ",matplotlib.get_backend())
except:
print (" ",gui, "Not found")

Managed to break my venv; is it possible to fix?

I managed to break one of my virtual environments. I think I did it by installing pycrypto with pip and uninstalling it with easy_install.
This is what I get when I run python in the venv.
filip#dratmac:~/PycharmProjects/Project-Marketplace2/backend/odb/helper_objects$ python
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 62, in <module>
import os
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 49, in <module>
import posixpath as path
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 17, in <module>
import warnings
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py", line 8, in <module>
import types
File "types.py", line 1, in <module>
import json
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 108, in <module>
from .decoder import JSONDecoder
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 3, in <module>
import re
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 280, in <module>
import copy_reg
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 7, in <module>
from types import ClassType as _ClassType
ImportError: cannot import name ClassType
filip#dratmac:~/PycharmProjects/Project-Marketplace2/backend/odb/helper_objects$
And outside the venv everything works as expected:
Last login: Tue Apr 7 22:30:08 on ttys005
filip#dratmac:~$ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
How can I fix this?
You should not name your file types.py as there is a standard library module with the same name (import types).
As such, Python tries to import it as part of some standard library code but it now actually imports your local types.py file, not the module it expects.
So the problem should be fixed when you rename types.py to something else.

keep getting 'ImportError: No module named pygtk' when I try to run Gaphor on windows

I have installed pygtk-all-in-one-2.24.0.win32-py2.7.msi on windows, and installed gaphor by easy_install. When I try to run gaphor, I receive an import error.
PS C:\Python27\Scripts> .\gaphor.exe
Traceback (most recent call last):
File "C:\Python27\Scripts\gaphor-script.py", line 9, in <module>
load_entry_point('gaphor==0.17.2', 'console_scripts', 'gaphor')()
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 521, in load_entry_point
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 2632, in load_entry_point
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 2312, in load
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 2318, in resolve
File "C:\Python27\lib\site-packages\gaphor-0.17.2-py2.7.egg\gaphor\__init__.py", line 13, in <module>
import pygtk
ImportError: No module named pygtk
Any suggestions would be appreciated!
Did you actually install pygtk? I suspect easy-install should have pulled it in automatically. Did you see any errors during the install? pygtk also requieres installing gtk+.
Do a simple test - start Python in a console (ipython?), and type:
$ python
Python 2.7.6 (default, May 6 2014, 10:57:58)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygtk
>>> import gtk
>>>
There should be no error messages.
You can also search the disk for pygtk.py (with the file manager) and for a directory gtk-2.0 or gtk-3.0.

scipy with enthought canopy

I am evaluating the Enthought package. I installed the 32bit canopy (downloaded from https://www.enthought.com/downloads/) in Ubuntu.
$ sudo bash canopy-1.0.1-rh5-32.sh
Upon testing I don't see scipy in /usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages:
$ /usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/bin/python
Enthought Canopy Python 2.7.3 | 32-bit | (default, Mar 25 2013, 15:45:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named scipy
>>> from pylab import*
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages/matplotlib/pylab.py", line 265, in <module>
from matplotlib.pyplot import *
File "/usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages/matplotlib/backends/backend_wxagg.py", line 23, in <module>
import backend_wx # already uses wxversion.ensureMinimal('2.8')
File "/usr/local/Canopy/appdata/canopy-1.0.0.1160.rh5-x86/lib/python2.7/site-packages/matplotlib/backends/backend_wx.py", line 52, in <module>
raise ImportError(missingwxversion)
ImportError: Matplotlib backend_wx and backend_wxagg require wxversion, which was not found.
Please help me: how can install the Python Canopy (including scipy, matplotlib etc.)? Do I have to manually install them? I thought the installer shell script installed all these automatically. Am I missing something?
$ uname -a
Linux sree 3.0.0-32-generic-pae #51-Ubuntu SMP Thu Mar 21 16:09:48 UTC 2013 i686 i686 i386 GNU/Linux
Scipy is installed into a different virtual environment (~/Enthought/Canopy_64bit/System/). In general, you should be running the Python available at ~/Enthought/Canopy_64bit/User/bin/python as described here. Also look at this article for information on how to make Canopy your default Python

Categories

Resources