0

Linux, pop_os, vs code, python3

import tkinter as tk
win = tk.Tk()
win.title("Hello World")
win.mainloop()

python main.py -> ModuleNotFoundError: No module named 'tkinter'

python3-tk and python-tk already installed with apt

python --version (in vs code terminal) -> Python 3.9.9

pip3 install tk (in vs code terminal) -> Requirement already satisfied: tk in /var/data/python/lib/python3.9/site-packages (0.1.0)

pip3 install tkinter (in vs code terminal) -> ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter

s s
  • 1
  • Try running your script from a terminal (outside vscode). If that works, the problem lies with vscode. Personally I would suggest using a text editor instead of an IDE. – Roland Smith Aug 05 '22 at 21:24
  • You cannot install tkinter with pip. – Bryan Oakley Aug 05 '22 at 21:31
  • @RolandSmith worked, however how can I make it work inside VS code itself? Or am I stuck with having to launch the program with an external terminal? – s s Aug 05 '22 at 21:39
  • 1
    There's no technical reason that you can't run it in vscode. Your vscode just isn't configured to have the path where the version of python with tkinter exists. – Bryan Oakley Aug 05 '22 at 23:34
  • You might check out this link [Installing tkinter](https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter). That's what I had to do to install it on Linux Mint. – NoDakker Aug 06 '22 at 00:03
  • If you search for "vscode select python interpreter" you will find several links on how to configure vscode for Python. – Roland Smith Aug 06 '22 at 00:05
  • It worked for me. I suggest you re-install Python. – toyota Supra Aug 06 '22 at 01:26
  • **No part of the standard library, including Tkinter, can be installed using Pip.** Whatever `tk` package you installed **is not** the Tkinter standard library. The `apt` install of `python3-tk`, only affects the system Python. If you are using a Python built from source, you need to rebuild it *after* installing a *development* Tk package. Please see the linked duplicate for details. If, on the other hand, you want to make VS Code use the system Python (where Tkinter should work, now that `python3-tk` is installed), that is a totally separate issue. – Karl Knechtel Apr 28 '23 at 21:20

1 Answers1

0

When you can successfully run the code in the external terminal, it indicates that this is not a package problem.

In the comments, Roland Smith said clearly that you need to configure the correct interpreter for vscode by the following way:

  1. use shortcut "ctrl+shift+P"
  2. type "Python: select interpreter"
  3. choose the correct interpreter.
MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13