Home > Back-end >  Why does Tkinter work in the terminal but not in Pycharm?
Why does Tkinter work in the terminal but not in Pycharm?

Time:02-02

Using Pycharm on Linux mint.

I installed the "future" package for the python interpreter which I'm using. Heres the script.

from tkinter import *

top = Tk()

top.mainloop()

Didn't work. It returns "ModuleNotFoundError: No module named 'tkinter'". Tkinter is infact installed. "python3 -m tkinter" confirms it. And when I compile the same code in the terminal, it displays.

CodePudding user response:

As Bryan says, you're probably not using the Python version you think you're using. PyCharm tends to install its own version of Python. Once you have more than one version of Python installed, things get trickier.

To see what's happening, try running this script:

import sys

print(sys.executable, sys.version)

Or run those similar commands from the command line. That should help clarify matters.

The sys.executable will show you the full path to your Python executable. Great for seeing where the used Python installation is located.

I don't use Python on Linux, but perhaps one of your Python installations is version 2, in which case you would need to use:

from Tkinter import *

which is another way to confirm that the Python is version 2 rather than 3. If this is the case, you'll want to move to Python 3. I don't think anyone writes new projects in Python 2 anymore. It's defunct, purely legacy.

It's also possible that Python is installed on Linux without Tkinter. There are other posts on how to install Tkinter on Linux. For instance, you can check out ImportError: No module named 'Tkinter'

CodePudding user response:

Thanks guys for the help I really appreciate it. But I found out the problem was because of Linux Mint's Software Manager. I initially downloaded pycharm using said software manager but it didnt work which is why I created the post. Then I deleted it, and downloaded pycharm through the tar.gz file from the jetbrains website. After doing that, it seems to work.

  • Related