Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

git - Deploy a Python (Dash) app to Heroku using Conda environments (instead of virtualenv)

When I run git push heroku master to deploy my app to Heroku I keep getting errors

Heroku Push rejected, failed to compile Python app. Could not find a version that satisfies the requirement

The problem was that the requirements.txt file I made with

pip freeze > requirements.txt

made a dump of my system wide Python libraries instead of just the libraries in my virtualenv (as described here). This was very strange because I froze those requirements from my active virtualenv - this behavior should not have been possible.

virtualenv on windows has always slowed me down so I'm ready to try a new environment manager.

I want to use conda but am struggling to deploy with it to Heroku. I followed Heroku's instructions for conda build-packs only to get vague/unhelpful errors at build time.

How can I deploy a Python app to Heroku using Conda environments?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Heroku doesn't care if you're using virtualenv or conda to manage environments. Using one or the other is mostly irrelevant to the deployment process.

Don't bother with the Conda Environment Buildpack instructions since those are for deploying a remote conda environment which is not what you are trying to do. You, my friend, are trying to deploy a remote your_app environment.

Here's how to do that with a dash application and conda:

Create a new folder for your project:

$ mkdir dash_app_example
$ cd dash_app_example

Initialize the folder with git

$ git init # initializes an empty git repo

Create an environment.yml file in 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

Create the environment from environment.yml:

$ conda env create

Activate the conda environment

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

Confirm that the environment you're in is correct.

It should currently be in dash_app:

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

Initialze the folder with app.py, requirements.txt, and a Procfile:


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: describes your Python dependencies. You can fill this file in automatically by running $ pip freeze > requirements.txt on the command line.


Your folder structure should look like

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

Notice how there's no environment data in this directory. That's because conda unlike virtualenv stores all your environments in one place neatly away from your app directory. There's no need to .gitignore those files... they're not here!

Initialize Heroku, add files to Git, and deploy

$ 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"

Sources:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...