Can just catch the
ImportError
,then will work python 2 and 3 in both Windows and Linux.try: # Python 2 import Tkinter as tk except ImportError: # Python 3 import tkinter as tk root = tk.Tk() w = tk.Label(root, text="Hello, world!") w.pack() root.mainloop()