eyeonus / Trade-Dangerous

Mozilla Public License 2.0
98 stars 31 forks source link

ModuleNotFoundError: No module named 'Tkinter' #77

Closed ghost closed 2 years ago

ghost commented 4 years ago

I think this could be helpful for some Linux folks, as this took me a while to figure out:

lsb-release -a
Distributor ID: ManjaroLinux
Description:    Manjaro Linux
Release:    20.1.1
Codename:   Mikah

I use python-virtualenvwrapper to separate my different Python environments from my system. But after running pip install tradedangerous I got the following error as I ran trade.

Traceback (most recent call last):
  File "/home/username/.virtualenvs/tdtest/lib/python3.8/site-packages/appJar/appjar.py", line 14, in <module>
    from Tkinter import *
ModuleNotFoundError: No module named 'Tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/username/.virtualenvs/tdtest/bin/trade", line 5, in <module>
    from tradedangerous.trade import main
  File "/home/username/.virtualenvs/tdtest/lib/python3.8/site-packages/tradedangerous/trade.py", line 35, in <module>
    from tradedangerous import cli, gui
  File "/home/username/.virtualenvs/tdtest/lib/python3.8/site-packages/tradedangerous/gui.py", line 45, in <module>
    from appJar import gui
  File "/home/username/.virtualenvs/tdtest/lib/python3.8/site-packages/appJar/__init__.py", line 2, in <module>
    from appJar.appjar import gui
  File "/home/username/.virtualenvs/tdtest/lib/python3.8/site-packages/appJar/appjar.py", line 28, in <module>
    from tkinter import *
  File "/usr/lib/python3.8/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libtk8.6.so: Kann die Shared-Object-Datei nicht öffnen: Datei oder Verzeichnis nicht gefunden

Running pip install tk tcl pmw within the Python virtual environment didn't help. So finally I installed tk into my main system (not into the virtualenv) and everything works fine now.

On Manjaro/Arch this is pacman -S tk but should be similar on other distros. Hope this is of help for some guys.

Fly safe, o7 CMDRs

cpeosphoros commented 3 years ago

I don't know about Manjaro, but in Debian based systems, Tkinter is an OS module, not a Python one. That's why it worked when you did your equivalent to 'apt install python3-tk' in your system.

eyeonus commented 3 years ago

You are incorrect, as that error message is specifically related to the python tkinter python module.

draeath commented 3 years ago

Not quite, @eyeonus - that's the right package. There's some OS dependencies as well - namely on tk itself. The distro package not only installs the python component but would depend on that. The confusion comes in because the tkinter python module does not come from pypi but is a part of python itself and is only be built if tk is on the system. Distro maintainers often package it separately.

In @gqgunhed you may need to figure out how to get your venv wrapper to pass --system-site-packages to the venv module when building your virtual environment, and make sure you have the appropriate -tk python package from your distro installed.

draeath commented 3 years ago

We can probably close this by now? Unless you'd rather some documentation updates occur first regarding it.

Alternatively I can look into writing up a PR that does a bit more logic and only (tries to) import the GUI code (thus the tkinter module) if it's actually present. That would avoid the issue for folks who run into this and don't actually need/want the GUI.

I see some of this has been done, but some has been missed somewhere as trade --help on a headless Debian install throws the import exception about tkinter.

eyeonus commented 3 years ago

If you want to make a PR, I would welcome it.

Tromador commented 2 years ago

In general for tkinter Python must be built with TK support at configure/compile time on a system that has appropriate tk libs/headers.

A stackoverflow showing similar issue

Absent a PR or other activity in the last 6 months or so, closing this.