-1

I've write a simple script in python with pycharm. In this script I use tkinter. If I run the script in pycharm everything it's ok, but if I run the script using shell with command python script.py I receive the error

ImportError: No module named tkinter

Dennis A. Boanini
  • 477
  • 1
  • 5
  • 19

2 Answers2

0

PyCharm and your sysytem python interpreters may have different versions and different modules installed. Check PyCaharm python version and use it by defaul or just write full version name. For example:

python3.4 script.py
Bogdan Koliesnik
  • 780
  • 6
  • 14
  • PyCharm use python version 3.4. If I run `python3.4 script.py` I receive the same error `Traceback (most recent call last): File "script.py", line 1, in from tkinter import * File "/usr/local/lib/python3.4/tkinter/__init__.py", line 38, in import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named '_tkinter'` – Dennis A. Boanini Aug 16 '16 at 09:33
  • Try to rename your package import from `tkinter` to `Tkinter`. [Found here](http://askubuntu.com/questions/602060/importerror-no-module-named-tkinter) – Bogdan Koliesnik Aug 16 '16 at 10:14
  • 1
    @BogdanKoliesnik, tkinter is python3, Tkinter is python2, how would changing to Tkinter work in python3? – Padraic Cunningham Aug 16 '16 at 10:17
0

ImportError: No module named tkinter

Simply put, the module named tkinter is not visible to the python binary that you are using to run the script. Possible areas to investigate maybe

  1. Check whether you are using the same python executable as pyCharm is using.
  2. If using virtual-environments, make sure to enable it and that all dependencies are met.
  3. Your pyCharm might have reference to the library tkinter from libraries already set. That way you may need to install it.
  4. By default tkinter comes packaged in python binaries, to check run import tkinter on the shell, if you end up with an error import _tkinter # If this fails your Python may not be configured for Tk then you need to re-build your python. Check this post here for more details regarding this.
Community
  • 1
  • 1
Saif Asif
  • 5,516
  • 3
  • 31
  • 48
  • If I run python3.4 by shell I have this result: `phate@debian:~$ python3.4 Python 3.4.2 (default, May 5 2016, 23:18:17) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.4/tkinter/__init__.py", line 38, in import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named '_tkinter'` PS: If I write some script and I would share with community using github, how can I do this – Dennis A. Boanini Aug 16 '16 at 10:32
  • "If I run python3.4 by shell I have this result:..." That information should have been edited into the question instead. "PS: If I write some script and I would share with community using github, how can I do this" - This is an unrelated question. Stack Overflow is **not a discussion forum**, and we take **one** question at a time. – Karl Knechtel Apr 30 '23 at 00:23