How can I add the tkinter to the pyproject.toml in poetry?
Asked Answered
A

1

7
  File "project.py", line 5, in <module>
    from tkinter.filedialog import askopenfilenames
ModuleNotFoundError: No module named 'tkinter'

This is always happens when I want to run my project with poetry run python project.py but if I run natively it's working flawlessly. So I guess the problem is to add the Tkinter to the .toml file but when I add tk to the dependencies it's not working properly. What should I do?

Auberbach answered 8/2, 2022 at 15:39 Comment(4)
What do you mean by "when I add tk to the dependencies it's not working properly"? How exactly is it "not working properly"?Neogothic
@Neogothic It means the tkinter.filedialog is not included when I would like to run the project.py. But all the other dependencies working perfectly if I remove all tkinter components.Auberbach
do you run Python 3 or Python 2? In Python 2 it could have name Tkinter with upper T. Do you run it on some Linux server? In most systems tkinter is installed with Python but on some Ubuntu Servers it needed to install manually library python-tkinter using tool apt (because Ubuntu Server doesn't have graphic mode so it can't display GUIs - so Python doesn't need tkinter module and it is not installed with Ubuntu Server)Quadruple
I'm having the same issue. python -m tkinter works fine (opens a mini Tk test window) but poetry run python -m tkinter fails with ModuleNotFoundError: No module named '_tkinter' (and some preceding error text too). Since tkinter is part of the std lib, you can't install it — it's not on PyPi, or anywhere — just like how you can't install os, json, etc. Not sure what the solution is here except to ask the poetry devs to fix the issue or install the necessary libraries separately as in https://mcmap.net/q/1391363/-python-tkinter-modulenotfounderror-no-module-named-39-_tkinter-39/5496433 and related questions.Session
H
1

I did also have this issue, but as @furas did mention tkinter is not installed by default on some ubuntu serverion(i run (in windows)ubuntu 22.04 LTS) with Poetry (version 1.6.1) and python 3.10

So I did have to install tkinter with apt-get by: sudo apt-get install python3-tk

Now tkinter is installed but it still cant be added to the .tomel, but if i dont have it added and run the project with "poetry run python project.py" it do run without any problem. It seams to be the same with all standard Python libraries. My .toml is then:

[tool.poetry]
name = "test-gui"
version = "0.1.0"
description = ""
authors = ["JonTheBaboon"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"
pillow = "^10.1.0"
tk = "^0.1.0"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

You mentions tk, if you run "poetry add tk", you will get another package "TensorKit", see the poetry.lock file:

name = "tk"
version = "0.1.0"
description = "TensorKit is a deep learning helper between Python and C++."
optional = false
python-versions = "*"
Heavyfooted answered 16/10, 2023 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.