"ImportError: No module named tkinter" when using Pmw

43,792

Solution 1

Maybe I can help you on how to remove the error.

here are two thoughts:

1) you use python 2.xx and have installed the python 3 pwm module (Tkinter was renamed to tkinter from Python 2 to 3)

2) you do the following before the import and hope it helps:

#import tkinter
#Traceback (most recent call last):
#  File "<pyshell#11>", line 1, in <module>
#    import tkinter
#ImportError: No module named tkinter

import sys, Tkinter
sys.modules['tkinter'] = Tkinter # put the module where python looks first for modules
#import tkinter # now works!

Solution 2

Another workaround would be the following:

try:
    import tkinter
except:
    import Tkinter as tkinter

This way you would always have the module tkinter available and depending on the Python version your program loads tkinter or Tkinter.

Solution 3

I was facing the same problem with matplotlib.pyplot (python 2.7+) in my CentOs. I solved the problem by just installing the tkinter. sudo yum install tkinter. Hope this can help you.

Share:
43,792
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Here's my problem: I'm running the code in this example. I have Python 2.7 and 3 installed on my RaspberryPi but I have checked and double-checked, and I am running the code in 2.7. I've installed Pmw 2.0.0 under 2.7, not 3, but when I try to run, I get the "ImportError: No module named tkinter" error. I use Tkinter all the time, so it usually works fine, and I've done a search to verify that I'm definitely calling "Tkinter", not "tkinter", so I think it has to be a problem with Pmw, which also seems to be indicated by the traceback (posted in full at the bottom of my question). I can't for the life of me find a specific place where Pmw is looking for lower-case "tkinter", and I'm at a loss for how to work around this. I'm not eager to switch platforms--this is for work, so unless this is unfixable, I need to stick with Tkinter. Oh, and I am pretty new to Python, so I would love to find out that it's a simple problem that someone on here can spot easily.

    import sys; print sys.path gives me:

    ['/home/pi/Desktop', '/home/pi', '/usr/bin', '/usr/local/lib/python2.7/dist-packages/distribute-0.6.28-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7']
    

    The full traceback is:

    Traceback (most recent call last):
      File "/home/pi/Desktop/LinkedMenusSample.py", line 151, in <module>
        Pmw.initialise(root)
      File "/usr/local/lib/python2.7/dist-packages/Pmw/Pmw_2_0_0/lib/PmwLoader.py", line 131, in __getattr__
        self._initialise()
      File "/usr/local/lib/python2.7/dist-packages/Pmw/Pmw_2_0_0/lib/PmwLoader.py", line 89, in _initialise
        raise ImportError(msg)
    ImportError: No module named tkinter
    
  • Admin
    Admin over 11 years
    Thank you! This solved my problem. I'd previously tried importing Tkinter as tkinter and all kinds of things like that, but I hadn't tried assigning it like this. EDIT: I'd upvote you, but I guess I don't have enough rep. Sorry about that.
  • User
    User over 10 years
    If you can not import Tkinter in Python 2.x then you have got a different error/problem. The module is exactly written like Tkinter in Python 2.x.