I exported a file from Jupyter to a .py format. The code works fine on Jupyter itself, but when I try to run it directly from the command line I get the following error

Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in <module>
    import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "analysis.py", line 30, in <module>
    import matplotlib.pyplot as plt # NOTE: This was tested with matplotlib v. 2.1.0
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 2374, in <module>
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/_backend_tk.py", line 5, in <module>
    import tkinter as Tk
  File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')

Both python-tk and python3-tk are installed. I'm using Python 3.5.2 in my Jupyter code. I've seen this thread and many others about this problem, but I still can't figure it out. What puzzles me the most is that the code does run on Jupyter, but I can't run it on a terminal when I export it.

Could someone please point me in the right direction as to solve this issue? Thank you in advance.

  • Do you have the tk8.6, libtk8.6, and maybe the tk8.6-blt2.5 packages installed also? – ubfan1 Jan 25 at 19:59
  • Thanks for your reply, @ubfan1. Yes, everything was already installed. To be honest, I got tired of fighting this, so I simply created a virtual environment using conda, exported the file using that kernel on Jupyter and then ran it on my terminal through that virtual environment. Not the best solution, but a solution nonetheless. – pfreire Jan 28 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.