简体   繁体   English

导入matplotlib在Heroku上失败

[英]import matplotlib failing on Heroku

[(Basically identical to this from a few months ago: import matplotlib failing with No module named _tkinter on heroku . However, the only solution provided doesn't seem to work. (Unfortunately I can't comment on the answer given there since I don't have enough StackOverflow reputation.))] [(几个月前基本上与此相同: 导入matplotlib在heroku上没有名为_tkinter的模块失败 。但是,提供的唯一解决方案似乎不起作用。(不幸的是我不能评论那里给出的答案,因为我没有足够的StackOverflow信誉。))]

I've been plotting using matplotlib in my app. 我一直在我的应用程序中使用matplotlib进行策划。 Everything works fine locally. 一切都在当地很好。 However when I push my app to Heroku I get the error: 但是,当我将我的应用程序推送到Heroku时,我得到错误:

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

I've tried to circumvent Tkinter by doing: 我试图绕过Tkinter:

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

However this still throws the same error. 但是,这仍然会引发同样的错误。

Has anyone found a solution for this or have a Heroku app with matplotlib that is working? 有没有人为此找到了解决方案或者有matplotlib的Heroku应用程序正在运行? I'm running Python 2.7.13 (that's also the version Heroku installs when pushing the app). 我正在运行Python 2.7.13(这也是Heroku在推送应用程序时安装的版本)。

For me this works: 对我来说,这有效:

Change the matplotlib backend from tkinter to something else. 将matplotlib后端从tkinter更改为其他内容。 At the very beginning of the program do this: 在程序的最开始这样做:

import matplotlib
matplotlib.use('Agg')

This way the rest of the program will use the backend that you set ('Agg', 'SVG', etc, etc) 这样程序的其余部分将使用您设置的后端('Agg','SVG'等)

Another option would be to try and mess with the The matplotlibrc file per: https://matplotlib.org/users/customizing.html#the-matplotlibrc-file 另一个选择是尝试和matplotlibrc文件混淆: https//matplotlib.org/users/customizing.html#the-matplotlibrc-file

I contacted Heroku support and got the following answer: 我联系了Heroku支持并得到了以下答案:

" The default Heroku Python build doesn't contain the libs needed for tkinter to be imported successfully. You can find a list of the underlying libraries we offer by default here: https://devcenter.heroku.com/articles/cedar-ubuntu-packages 默认的Heroku Python构建版不包含成功导入tkinter所需的库。您可以在此处找到我们默认提供的基础库列表: https//devcenter.heroku.com/articles/cedar-ubuntu -packages

In cases like these, we provide the opportunity for developers to extend the platform using third party buildpacks https://devcenter.heroku.com/articles/third-party-buildpacks many of which you can browse at https://elements.heroku.com/buildpacks These can install extra packages and dependencies at deploy time. 在这些情况下,我们为开发人员提供了使用第三方构建包扩展平台的机会https://devcenter.heroku.com/articles/third-party-buildpacks ,其中许多可以在https://elements.heroku浏览.com / buildpacks这些可以在部署时安装额外的包和依赖项。

Please be aware that third party buildpacks are an option but the buildpacks themselves are unsupported. 请注意,第三方buildpack是一个选项,但buildpack本身不受支持。 They might not be maintained by the original authors and have stopped working. 他们可能不会被原作者维护并停止工作。 If that's the case, you'll need to contact the original author to see if they can help. 如果是这种情况,您需要联系原作者,看看他们是否可以提供帮助。 There's some suggestion on this SO post that other users have used custom Python buildpacks to run this successfully https://stackoverflow.com/a/18184536 在这个SO帖子上有一些建议,其他用户已经使用自定义Python buildpacks成功运行它https://stackoverflow.com/a/18184536

Another option might be to use the experimental Apt buildpack here: https://github.com/heroku/heroku-buildpack-apt You would need to run 另一种选择可能是在这里使用实验性的Apt buildpack: https//github.com/heroku/heroku-buildpack-apt你需要运行

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt

You would also need to add a file named Aptfile at the root of the project with the following contents: 您还需要在项目的根目录中添加名为Aptfile的文件,其中包含以下内容:

python3-tk

Unfortunately, this Aptfile doesn't have the same dependency resolution so you will have to specify any other packages manually. 不幸的是,此Aptfile没有相同的依赖项解析,因此您必须手动指定任何其他包。 "

I installed the following buildpack: https://github.com/thenovices/heroku-buildpack-scipy . 我安装了以下buildpack: https//github.com/thenovices/heroku-buildpack-scipy Which solved the issues with matplotlib. 这解决了matplotlib的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM