简体   繁体   中英

Unable to import tkinter in Python3, but it works in python2 (Python3.7 macOS Catalina)

Running python3 to bring me into python, and then simply running import tkinter results in the following error:

File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

However when going into the python shell (not python3), import tkinter works fine as expected. How can I get tkinter to work with Python3?

Please note the following:

  • I am on MacOS Catalina
  • I am working with Python 3.7.3 (based off python3 --version )
  • It looks like my python3 executable is ( /usr/bin/python3 )
  • The path for tkinter is /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/
  • I am able to run idle in Terminal, but I can't run idle3
  • I don't think I am using a virtualenv or pyenv or anything like that

You need a separate Tcl/Tk installation. Try a newer version of the Python3 binary installer from python.org, which bundles Tcl/Tk.

Don't install tkinter through idle . Try idle3 . If nothing, install python3-tk using your OS installer. (I have apt . I'm on Debian.). Good luck!

Don't use _ in import _tkinter

Use import tkinter

I am Sure that this will work. If it works then tick my answer.

Fist open Command Prompt and run pip install tkinter Then open your python project and press Ctrl + S. Then run your project. I hope my answer is useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM