ModuleNotFoundError: No module named 'tkinter'

1,420 views
Skip to first unread message

Bhushan Dhamale

unread,
May 24, 2017, 1:44:01 AM5/24/17
to cobra pie
When I execute my COBRApy scripts, I get the following error:
/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/solvers/__init__.py:73: UserWarning: No LP solvers found
warn("No LP solvers found")
Traceback (most recent call last):
File "se2.py", line 5, in <module>
from cobra import Model, Reaction, Metabolite
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/__init__.py", line 11, in <module>
from cobra import design, flux_analysis, io
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/flux_analysis/__init__.py", line 8, in <module>
from cobra.flux_analysis.gapfilling import gapfill, growMatch
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/flux_analysis/gapfilling.py", line 9, in <module>
from cobra.core import Model
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/core/__init__.py", line 6, in <module>
from cobra.core.gene import Gene
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/core/gene.py", line 13, in <module>
from cobra.util import resettable
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/util/__init__.py", line 8, in <module>
from cobra.util.array import *
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/cobra/util/array.py", line 7, in <module>
import pandas as pd
File "/home/bhushan/environments/COBRApy/my_files/pandas.py", line 3, in <module>
import matplotlib.pyplot as pyplot
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/six.py", line 92, in __get__
result = self._resolve()
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/home/bhushan/environments/COBRApy/lib64/python3.6/site-packages/six.py", line 82, in _import_module
__import__(name)
ModuleNotFoundError: No module named 'tkinter'


In an attempt to fix it, I tried sudo yum install tkinter.
I get the following output:
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 5.7 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 3.4 kB 00:00:00
ius | 2.3 kB 00:00:00
nvidia-diag-driver-local | 2.4 kB 00:00:00
updates | 3.4 kB 00:00:00
epel/x86_64/primary_db FAILED
http://repo.ugm.ac.id/epel/7/x86_64/repodata/b481c7ee9f843b2897727bf168c73f51de11cce67808a8c518b28cf84ab543cb-primary.sqlite.xz: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
To address this issue please refer to the below knowledge base article

https://access.redhat.com/articles/1320623

If above article doesn't help to resolve this issue please create a bug on https://bugs.centos.org/

epel/x86_64/primary_db FAILED
https://mirror.lzu.edu.cn/epel/7/x86_64/repodata/b481c7ee9f843b2897727bf168c73f51de11cce67808a8c518b28cf84ab543cb-primary.sqlite.xz: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
(1/2): epel/x86_64/primary_db | 4.7 MB 00:00:09
(2/2): epel/x86_64/updateinfo | 799 kB 00:00:11
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: epel.scopesky.iq
* extras: centos.excellmedia.net
* ius: archive.linux.duke.edu
* updates: centos.excellmedia.net
Package tkinter-2.7.5-48.el7.x86_64 already installed and latest version
Nothing to do

When I execute my COBRApy script again, the error still persists.
I would really appreciate any solution to this problem.

Justin Taylor

unread,
May 26, 2017, 7:31:34 AM5/26/17
to cobra pie
Bhushan,

This is not an issue with cobrapy, but more of an issue you are having with matplotlib. I will attempt to point you in the right direction.

On gitter, you mentioned that you are not utilizing matplotlib, but I see that you are importing a matplotlib module in one of your own scripts, pandas.py. I believe you have written this script, because I saw this script (or at least another one with the same name) in a different question you have posted in this group. If you wrote pandas.py, then you are in fact utilizing matplotlib and therefore tkinter.

File "/home/bhushan/environments/COBRApy/my_files/pandas.py", line 3, in <module>
import matplotlib.pyplot as pyplot

The issue is that the pyplot module is not able to find the default tkinter backend. Matplotlib gives the user the ability to use a variety of GUI backends (https://matplotlib.org/faq/usage_faq.html#what-is-a-backend). 

You can do one of two things to try to solve your problem:

1. Figure out why the tkinter backend is not working 

2. Use a different backend

The fastest way to find the answer to your problem would be to search Google for the phrase:

matplotlib ModuleNotFoundError: No module named 'tkinter'

If you are truly interested in using matplotlib, I suggest solving that problem first, without trying to run all of your cobrapy code. 

For example, it may be easier to start a new virtual environment in a new directory, install matplotlib, and try to run the tutorial (https://matplotlib.org/users/pyplot_tutorial.html). If you have errors running the tutorial, then you have narrowed down the search space for your solution

virtualenv -p /usr/bin/python3.6 venv   # start a virtual environment with Python 3.6 as the distribution of choie
. venv/bin/activate                     # activate your virtual environment
pip install matplotlib                  # install necessary packages in your environment
# try to run tutorial

To summarize, if you are interested in using matplotlib, isolate the problem to some simple matplotlib code first. Make sure that you can create the simple plot in the tutorial. In other words, do not attempt to fix this problem by also running all of your scripts that contain cobrapy logic. 

Hope this helps.

Best,
Justin

Justin Taylor

unread,
May 26, 2017, 7:48:42 AM5/26/17
to cobra pie
One more thing, if you actually did write pandas.py, you may want to rename this file to something else (ex. pandas_demo.py). pandas is the name of a Python package, so you do not want to be creating files with the same name. 


On Wednesday, May 24, 2017 at 1:44:01 AM UTC-4, Bhushan Dhamale wrote:

Bhushan Dhamale

unread,
May 31, 2017, 7:01:43 AM5/31/17
to cobra pie
You hit the nail right on its head!
Renaming
pandas.py to pandas_demo.py, deleting the newly created file pandas.pyc and folder __pycache__ fixed the problem.
Thanks a ton Justin! :)

Justin Taylor

unread,
May 31, 2017, 10:32:42 AM5/31/17
to cobra pie
Bhushan,

Glad to hear it. I think what was happening was that Python wanted to import your pandas.py script as a module, instead of importing the actual pandas package. 

Best,
Justin

Bhushan Dhamale

unread,
Jun 6, 2017, 2:54:33 AM6/6/17
to cobra pie
Yup. I think that's what was happening.
Reply all
Reply to author
Forward
0 new messages