ModuleNotFoundError: No module named 'Tkinter' and mixed 3.8.x python versions
When I run python3 from terminal (not inside pycharm) I am able to use tkinter and the version is 3.8.5:
gidi@gidi-Precision-3510:/usr/bin$ python3
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
>>>
When I launch terminal from Pycharm this is the output:
/usr/bin/python3 /app/pycharm/plugins/python-ce/helpers/pydev/pydevconsole.py --mode=client --port=39219
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/home/gidi/PycharmProjects/board'])
PyDev console: starting.
Python 3.8.8 (default, Nov 10 2011, 15:00:00)
[GCC 10.2.0] on linux
import tkinter
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/app/pycharm/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'tkinter'
How do I set my Python version in Pycharm to be 3.8.5 ?
请先登录再写评论。
When you run python3 from the /usr/bin directory, it doesn't mean it will use /usr/bin/python3.
If you want to use the same interpreter in PyCharm, then run which python3 in your system terminal and then use the interpreter it prints in PyCharm https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
I tried that. I does not make a difference whether I call the link or the actual executable:
gidi@gidi-Precision-3510:~$ ls /usr/bin/python3
lrwxrwxrwx 9 root 14 Jan 16:20 /usr/bin/python3 -> python3.8
I set my interpreter accordingly:
/usr/bin/python3.8 /app/pycharm/plugins/python-ce/helpers/pydev/pydevconsole.py --mode=client --port=35639
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/home/gidi/PycharmProjects/board'])
PyDev console: starting.
Python 3.8.8 (default, Nov 10 2011, 15:00:00)
[GCC 10.2.0] on linux
import tkinter
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/app/pycharm/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'tkinter'
What is the output of which python3?
gidi@gidi-Precision-3510:~$ which python3
/usr/bin/python3
Hmm, that's strange.
Could you attach a screenshot of your Interpreter Paths? https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html
Do you see these entries when you print sys.path in the system terminal?
Run python3 in the terminal and then:
Are there any additional paths?
Here is the output:
gidi@gidi-Precision-3510:~$ python3
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 sys
>>> sys.path
['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/gidi/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.8/dist-packages']
Let's try the following:
1) Remove all interpreter paths except /usr/lib/python3.8
2) Add the following paths instead:
/usr/lib/python3.8/lib-dynload
/home/gidi/.local/lib/python3.8/site-packages
/usr/local/lib/python3.8/dist-packages
/usr/lib/python3/dist-packages
/usr/lib/python3.8/dist-packages
After that, check if Tkinter is importable.
Sergey, I'm grateful for all your help. I could not add the last three paths:
I am able to change directory to this folder, though:
gidi@gidi-Precision-3510:~$ cd /usr/local/lib/python3.8/dist-packages/
gidi@gidi-Precision-3510:/usr/local/lib/python3.8/dist-packages$
Same thing for folders /usr/lib/python3 and /usr/lib/python3.8:
gidi@gidi-Precision-3510:~$ cd /usr/lib/python3/dist-packages
gidi@gidi-Precision-3510:/usr/lib/python3/dist-packages$
gidi@gidi-Precision-3510:~$ cd /usr/lib/python3.8/dist-packages/
gidi@gidi-Precision-3510:/usr/lib/python3.8/dist-packages$
This is the current status of my interpreter paths. With this setting tkinter cannot be imported.
Where is tkinter located in your system?
Just print it with print(tkinter)
You can then try adding that directory to Interpreter Paths.
If that doesn't work, I suggest creating a virtualenv as it seems PyCharm doesn't work well with this particular system interpreter for some reason.
Thanks, Sergey. I created a virtualenv for my project. I ran "pip install tk" in the activated environment. Pycharm still does not import tkinter correctly. I tried following the advice about installing future and using tkinter from this module (see https://stackoverflow.com/questions/53797598/how-to-install-tkinter-with-pycharm). However, this causes a chain of missing tkinter imports in other files.
I also installed python3.9 and tried using it from Pycharm. However, it refuses to detect the file in /usr/bin.
@Gidi Gal and all:
I had the same issue: tkinter not being recognized from within pycharm. But found a solution:
Situation: I use Arch Linux. I installed PyCharm through the software library, that I believe installs a FlatPak or something like that. I am not very aware how it works, but I understand it is self contained. As such, it has its own version of Python, that for some reason does not include tkinter (or at least does not work).
Solution: I uninstalled PyCharm from the software app repository, and the reinstalled the traditional way (sudo pacman -S pycharm-community-edition). Now works like a charm :-)
Hope it helps...