在heroku上部署matplotlib失败,应该如何正确地操作?

5
我已经在我的虚拟环境中使用pip安装了Matplotlib。一开始安装失败,但是在我执行easy_install -U distribute之后,安装就很顺利了。
以下是我在Git仓库根文件夹下所执行的操作:
virtualenv env
source env/bin/activate
pip install gunicorn
pip install numpy
easy_install -U distribute
pip install matplotlib

然后,我使用pip freeze > requirements.txt来创建一个requirements.txt文件。以下是结果:

argparse==1.2.1
distribute==0.7.3
gunicorn==17.5
matplotlib==1.3.0
nose==1.3.0
numpy==1.7.1
pyparsing==2.0.1
python-dateutil==2.1
six==1.3.0
tornado==3.1
wsgiref==0.1.2

在尝试部署我的应用程序时出现了问题:

(env)gofrendi@kirinThor:~/kokoropy$ git push -u heroku
Counting objects: 9, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 586 bytes, done.
Total 5 (delta 3), reused 0 (delta 0)

-----> Python app detected
-----> No runtime.txt provided; assuming python-2.7.4.
-----> Using Python runtime (python-2.7.4)
-----> Installing dependencies using Pip (1.3.1)
       Downloading/unpacking distribute==0.7.3 (from -r requirements.txt (line 2))
         Running setup.py egg_info for package distribute

       Downloading/unpacking matplotlib==1.3.0 (from -r requirements.txt (line 4))
         Running setup.py egg_info for package matplotlib
           The required version of distribute (>=0.6.28) is not available,
           and can't be installed while this script is running. Please
           install a more recent version first, using
           'easy_install -U distribute'.

           (Currently using distribute 0.6.24 (/app/.heroku/python/lib/python2.7/site-packages))
           Complete output from command python setup.py egg_info:
           The required version of distribute (>=0.6.28) is not available,

       and can't be installed while this script is running. Please

       install a more recent version first, using

       'easy_install -U distribute'.



       (Currently using distribute 0.6.24 (/app/.heroku/python/lib/python2.7/site-packages))

       ----------------------------------------
       Command python setup.py egg_info failed with error code 2 in /tmp/pip-build-u55833/matplotlib
       Storing complete log in /app/.pip/pip.log

 !     Push rejected, failed to compile Python app

To git@heroku.com:kokoropy.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:kokoropy.git'
(env)gofrendi@kirinThor:~/kokoropy$ 

似乎Heroku服务器无法正确安装Matplotlib。
当我执行“easy_install -U distribute”时,可能不会被pip记录。
Matplotlib还有几个非Python库依赖项(例如:libjpeg8-dev,libfreetype和libpng6-dev)。 我可以在本地安装这些依赖项(例如:通过apt-get)。 但是,这也没有被pip记录。
所以,我的问题是:如何在Heroku部署服务器上正确安装Matplotlib?
4个回答

6

终于我能够管理这个了。

首先,我使用了这个构建包:https://github.com/dbrgn/heroku-buildpack-python-sklearn。为了使用这个构建包,我运行了这个命令(也许这不是必需的步骤):

heroku config:set BUILDPACK_URL=https://github.com/dbrgn/heroku-buildpack-python-sklearn/

我将 requirements.txt 改成了以下内容:

argparse==1.2.1
distribute==0.6.24
gunicorn==17.5
wsgiref==0.1.2
numpy==1.7.0
matplotlib==1.1.0
scipy==0.11.0
scikit-learn==0.13.1

这里最重要的部分是我安装了matplotlib 1.1.0(目前最新版本为1.3.0)。可能会出现一些“已弃用的numpy API”警告。但在我的情况下,似乎一切正常。
这是结果(页面网站可能会因为我使用的是免费服务器而宕机): http://kokoropy.herokuapp.com/example/plotting A matplotlib generated figure

我刚发布了一个关于这个问题的问题。不幸的是,你的解决方案似乎不再起作用了。:( - MichaelRSF

2

对于那些正在查找此答案的人,我刚刚在最新的Heroku上部署了最新版本的matplotlib / numpy作为要求(分别为1.4.3、1.9.2),没有任何问题。


2
我也曾在使用 Heroku 和 Matplotlib 时遇到困难,而且很难找到答案。这篇文章对我很有帮助:http://dsimpson1980.github.io/simple_web_ui/heroku_setup.html。
基本上你需要在 requirements.txt 中打包 numpy 并上传项目。然后再将 pandas 和 matplotlib 添加到 requirements.txt 中。

这对我来说是一个问题,截至2018年9月6日,这个解决方案完美地解决了它。 - binzabinza

0

我之前遇到了同样的问题,后来在 Heroku 上构建应用程序时注意到安装 matplotlib 会重新安装 numpy,并在那一点上崩溃。我从要求文件中删除了 numpy,然后顺利进行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接