使用Conda环境(而不是virtualenv)将Python(Dash)应用程序部署到Heroku

时间:2017-12-23 02:05:58

标签: python git heroku

当我运行git push heroku master将我的应用部署到Heroku时,我不断收到错误

  

Heroku Push被拒绝,无法编译Python应用程序。找不到满足要求的版本

问题在于我使用

创建的requirements.txt文件
pip freeze > requirements.txt

转储了我的系统范围的Python库,而不仅仅是virtualenvas described here)中的库。这很奇怪,因为我从我的活跃的virtualenv中冻结了这些要求 - 这种行为应该是不可能的。

Windows上的

virtualenv总是让我失望,所以我准备尝试一个新的环境经理。

我想使用conda,但我正在努力将其部署到Heroku。我跟着Heroku's instructions for conda build-packs只是为了在构建时获得模糊/无用的错误。

如何使用Conda环境将Python应用程序部署到Heroku?

1 个答案:

答案 0 :(得分:5)

如果您使用virtualenvconda来管理环境,Heroku并不在意。使用其中一个与部署过程无关。

不要理会Conda Environment Buildpack指令,因为这些指令用于部署远程conda环境,而这不是您尝试做的。您,我的朋友,正在尝试部署远程 your_app 环境。

以下是使用dash applicationconda

的方法

为您的项目创建一个新文件夹:

$ mkdir dash_app_example
$ cd dash_app_example

使用git

初始化文件夹
$ git init # initializes an empty git repo

environment.yml中创建dash_app_example文件:

name: dash_app #Environment name
dependencies:
  - python=3.6
  - pip:
    - dash
    - dash-renderer
    - dash-core-components
    - dash-html-components
    - plotly
    - gunicorn # for app deployment

environment.yml创建环境:

$ conda env create

激活conda环境

$ source activate dash_app #Writing source is not required on Windows

确认您所处的环境是否正确。

目前应该在dash_app中:

$ conda info --envs #Current environment is noted by a *

使用app.pyrequirements.txtProcfile初始化文件夹:

app.py

import dash
import dash_core_components as dcc
import dash_html_components as html
import os

app = dash.Dash(__name__)
server = app.server

app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css";;})

app.layout = html.Div([
    html.H2('Hello World'),
    dcc.Dropdown(
        id='dropdown',
        options=[{'label': i, 'value': i} for i in ['LA', 'NYC', 'MTL']],
        value='LA'
    ),
    html.Div(id='display-value')
])

@app.callback(dash.dependencies.Output('display-value', 'children'),
              [dash.dependencies.Input('dropdown', 'value')])
def display_value(value):
    return 'You have selected "{}"'.format(value)

if __name__ == '__main__':
    app.run_server(debug=True)

Procfile

web: gunicorn app:server

requirements.txt:描述您的Python依赖项。您可以使用以下命令自动填写此文件:

$ pip freeze > requirements.txt

您的文件夹结构应如

- dash_app_example
--- app.py
--- environment.yml
--- Procfile
--- requirements.txt

请注意此目录中没有环境数据。这是因为condavirtualenv不同,它将您的所有环境存储在一个远离您的应用目录的地方。没有.gitignore这些文件......他们不在这里!

初始化Heroku,将文件添加到Git,然后部署

$ heroku create my-dash-app # change my-dash-app to a unique name
$ git add . # add all files to git
$ git commit -m 'Initial app boilerplate'
$ git push heroku master # deploy code to heroku
$ heroku ps:scale web=1  # run the app with a 1 heroku "dyno"

来源:

  1. Deploying an application with Heroku (using Conda Environments)
  2. My Python Environment Workflow with Conda
  3. Deploying Dash Apps(使用virtualenv