0
votes

Hello thank you for taking a look at my question here.

I am new to coding and am learning python. I am using Linux, specifically Ubuntu. I wrote a simple tkinter-based application which when launched displays a countdown to a particular date and then has a button to click to close the window. I have run this application in PyCharm and it works perfectly there. I also runs perfectly in IDLE. in both, tkinter imports and runs just fine.

I located the .py file for my aplication in the terminal and used

$ chmod +x main.py

then

$ ./main.py 

I get the response

ubuntu@ubuntu:~$ ./main.py 
Traceback (most recent call last):
  File "./main.py", line 9, in <module>
    import tkinter
  File "/usr/local/lib/python3.8/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'

Most similar questions I have managed to find suggest that tkinter must be installed, but certainly it would appear that tkinter IS installed, it ran in PyCharm and in IDLE, I have followed the directories listed to see that they are in fact on the system, I have run

ubuntu@ubuntu:~$ pip install tk
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: tk in ./.local/lib/python3.8/site-packages (0.1.0)

I have run

ubuntu@ubuntu:~$ python3
Python 3.8.5 (default, Feb  4 2021, 18:26:47) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/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'

everything I do seems to comfirm tkinter is installed yet will not import in Terminal. Any help?

update First of all I want to thank everyone who has commented so far, your insights have certainly led me to progress here.

I have discovered that I have 2 seperate installations of python 3.8.5

I showed above what happened when I used $python3 and >>>import tkinter in terminal. I just now tried calling the other installation in terminal by it's file path and issuing the same command:

root@ubuntu:/# /usr/bin/python3.8
Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()

tkinter imports. I do not get the above error.

I knew I had to have a working tkinter somewhere on my system because PyCharm and IDLE both successfully ran it. terminal however, still fails to import it when I simply attempt to run the .py file.

So now I suppose that my issue becomes how do I remove the Feb 4 installation and keep the Jul 28 installation, or how do I get the Feb 4 installation to import tkinter.

2
pip install tk installs TensorKit, not tkinter. You need to install via system package manager: sudo apt-get install python3-tk. - acw1668
$ sudo apt-get install python3-tk Reading package lists... Done Building dependency tree Reading state information... Done python3-tk is already the newest version (3.8.5-1~20.04.1). 0 upgraded, 0 newly installed, 0 to remove and 61 not upgraded. - code4321
Is the folder where tkinter is installed listed in your pythonpath? - pavel
@pavel sorry, I'm not sure what pythonpath means. does that mean whether it is installed in the same directory? tkinter is installed in /usr/local/lib/python3.8/tkinter/ - where can I find pythonpath? - code4321
pythonpath is one of environmental variables that lives on your system and contains locations of directories where Python packages are installed. - pavel

2 Answers

0
votes

I had a similar Problem once on ubuntu, that pip was linked to the python2 installation, have you tried to work with pip3:

pip3 install tkinter
0
votes

In the end, I determined I had system python in /usr/bin/ and a self-compiled python of the same version in /usr/local/bin/ when I uninstalled the self-compiled installation, and ram my application from terminal everything worked perfectly, tkinter imported and my application ran from terminal exactly as it had from both IDEs I have used.

I uninstalled according to these instructions: https://unix.stackexchange.com/questions/190794/uninstall-python-installed-by-compiling-source simply replacing references to 2.7 with 3.8

thank you to everybody who commented, you were a great help and @acw1668 in particular, your comment led to me indifying that I had installations under both file paths. Much thanks.