将Python应用程序部署到Heroku

时间:2018-04-21 08:08:40

标签: python reactjs heroku npm deployment

我刚刚创建了一个Heroku帐户,我正在尝试部署我现有的代码。当我尝试git push heroku master时,我收到以下错误:

 Counting objects: 348, done.
 Delta compression using up to 4 threads.
 Compressing objects: 100% (207/207), done.
 Writing objects: 100% (348/348), 172.64 KiB | 0 bytes/s, done.
 Total 348 (delta 138), reused 279 (delta 116)
 remote: Compressing source files... done.
 remote: Building source:
 remote: 
 remote:  !     Warning: Multiple default buildpacks reported the 
 ability to handle this app. The first buildpack in the list below 
 will be used.
 remote:            Detected buildpacks: Python,Node.js
 remote:            See 
 https://devcenter.heroku.com/articles/buildpacks#buildpack-detect- 
 order
 remote: -----> Python app detected
 remote: -----> Running pre-compile hook
 remote: ----->Pre-compile hook
 remote: -----> Running Webpack
 remote: jquery-webpack-stats.json created
 remote: webpack-stats.json created
 remote: bin/run_webpack: line 15: npm: command not found
 remote:  !     Push rejected, failed to compile Python app.
 remote: 
 remote:  !     Push failed
 remote: Verifying deploy...
 remote: 
 remote: !  Push rejected to paytientdesktop.
 remote: 
 To https://git.heroku.com/paytientdesktop.git
 ! [remote rejected] master -> master (pre-receive hook declined)
 error: failed to push some refs to 
 'https://git.heroku.com/paytientdesktop.git'

这是我的app.json文件:

   {
   "name": "myapp",
   "description": "Myapp Heroku app.",
  "scripts": {
 "postdeploy": "python manage.py migrate"
},
"env": {
  "ALLOWED_HOSTS": {
  "description": "Django ALLOWED_HOSTS setting, e.g.: 
  .appname.herokuapp.com"
  },
  "DISABLE_COLLECTSTATIC": {
    "description": "Heroku setting to disable Django collectstatic (it 
  is run by bin/post_compile)",
  "value": "1"
},
"DJANGO_SETTINGS_MODULE": {
  "description": "Django settings Python import path",
  "value": "myapp.settings.production"
},
"SECRET_KEY": {
  "description": "Django SECRET_KEY setting",
  "generator": "secret"
  }
 },
 "formation": {
 "web": {
  "quantity": 1,
  "size": "free"
  },
  "worker": {
  "quantity": 1,
  "size": "free"
  }
 },
 "addons": [
  {
    "plan": "heroku-postgresql:hobby-dev",
    "options": {
    "version": "9.5"
    },
  "as": "DATABASE"
  },
  {
    "plan": "heroku-redis:hobby-dev",
    "options": {
    "version": "3.2"
    },
    "as": "REDIS"
  },
  {
  "plan": "sendgrid:starter"
  },
  {
    "plan": "papertrail:choklad"
  },
  {
  "plan": "opbeat:free"
  }
],
 "buildpacks": [
  {
    "url": "heroku/nodejs"
  },
  {
    "url": "heroku/python"
   }
 ]
}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

这里发生了什么?

Heroku使用buildpacks构建应用程序,每个应用程序都特定于特定的编程语言和工具集。

在许多情况下,Heroku可以通过查找存储库中的某些指标文件来检测它应该使用哪个构建包。例如,如果您的存储库在您的存储库根目录中包含requirements.txt个文件或PipfilePipfile.lock文件,则会调用the official Python buildpack

上面部署失败的输出包含以下信息:

 remote:  !     Warning: Multiple default buildpacks reported the 
 ability to handle this app. The first buildpack in the list below 
 will be used.
 remote:            Detected buildpacks: Python,Node.js
 remote:            See 
 https://devcenter.heroku.com/articles/buildpacks#buildpack-detect- 
 order

Heroku默认只运行一个buildpack,它不知道它是否应该为你的应用程序使用它的Python buildpack或它的Node.js buildpack。它选择了Python。

我该如何解决?

好消息是您可以在单个应用程序中使用multiple buildpacks。您只需要对tell Heroku which buildpacks it should use, and in what order进行一些手动操作。

以下是一个适合您的示例:

  1. 首先,设置一个buildpack。我喜欢在这里使用任何感觉像主语言的东西。

    heroku buildpacks:set heroku/python
    
  2. 接下来,按照正确的顺序添加其他构建包,该顺序由--index参数控制。看起来你的Python构建依赖于npm,所以你需要Node.js buildpack在 Python buildpack之前运行

    heroku buildpacks:add --index 1 heroku/nodejs
    
  3. 通过运行heroku buildpacks确认您的构建包已正确配置。您应该首先看到Node.js buildpack,然后是Python buildpack。

  4. 再次将您的代码推送到Heroku并观察它编译!

  5. 为什么我的app.json没有工作?

    您的app.json文件列出了Node.js和Python构建包。为什么不起作用?

    我从未使用app.json,而且我已经使用Heroku大约五六年了。在Heroku上运行应用程序当然不是必需的。

    看起来app.json专门用于Heroku的Platform API,它将用于启动一个全新的应用程序(与将代码部署到已定义的应用程序相反)。