导入matplotlib失败,在heroku上没有名为_tkinter的模块

问题描述:

我试图在我的应用程序中使用 matplotlib 。我在python2.7中创建了virtualenv,pip安装了matplotlib,并且它在本地成功运行。

I'm trying to use matplotlib in my application. I created a virtualenv in python2.7, pip installed matplotlib, and it's successfully running on local.

但是,当我将应用程序部署到 heroku (在 pip freeze 和其他必要步骤之后),我的应用程序崩溃。当我检查日志时,我看到以下内容:

However, when I deploy the app to heroku (after pip freeze and other steps necessary), my app crashes. When I check the log, I see the following:

import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

这很奇怪,因为该应用程序在本地下成功运行。 heroku python环境未配置为运行 matplotlib ?如果是这样,我应该采取什么措施来实现这一目标?

It's weird because the app was successfully running on local under venv. Is the heroku python environment not configured to run matplotlib? If so, what steps should I take to enable that?


matplotlib.use('Agg')
import matplotlib.pyplot as plt

这将设置您的Matplotlib 后端使用Agg而不是Tk。至少为我工作: - )

This will set your Matplotlib backend to use Agg instead of Tk. Just worked for me at least :-)