Heroku 上的 Python 和 Node.js

标签 python node.js heroku

我已经开始制作一个在 Heroku 上运行的 Node 服务器。在我尝试使用(非官方的)Duolingo API 之前,它一直运行良好。我编写了以下 Python 脚本来连接到 API:

import duolingo
import simplejson as json

lingo  = duolingo.Duolingo('harleyrowland')
print json.dumps(lingo.get_user_info())

我的 Node 服务器通过以下命令使用它:

var python = require('python-shell');

module.exports = {
  getData: function(callback){
    python.run('duoScript.py', function (err, results) { 
      console.log(err);
      console.log(results);
      var res = JSON.parse(results);
      var language = res.language_data.es.language_string;
      var streak = res.language_data.es.streak;
      var level = res.language_data.es.level;
      var levelPerecentage = res.language_data.es.level_percent;
      var fluency = res.language_data.es.fluency_score;
      var nextLesson = res.language_data.es.next_lesson.skill_title;
      return callback({language, streak, level, levelPerecentage, fluency, nextLesson});
    });
  }
}

在本地一切正常。

当我将它推送到 Heroku 时,代码不起作用,我开始在 Heroku 日志中收到以下错误:

{ [Error: ImportError: No module named duolingo]
     2016-10-06T00:02:32.133315+00:00 app[web.1]:   traceback: 'Traceback (most recent call last):\n  File "duoScript.py", line 1, in <module>\n    import duolingo\nImportError: No module named duolingo\n',
    executable: 'python',
    options: null,
    script: 'duoScript.py',
    args: null,
    exitCode: 1 
}

因此,我继续 Heroku API并发现我需要添加一个 requirements.txt 文件。所以我做了:

duolingo-api==0.3
simplejson==3.8.2

还是不行。然后我找到了this回答并添加了一个 .buildpacks 文件:

https://github.com/heroku/heroku-buildpack-python.git
https://github.com/heroku/heroku-buildpack-nodejs.git

我仍然得到同样的错误。

知道是什么导致了这个错误吗?

更新1

我想我也会显示我的 Procfile,以防这是问题所在:

web: node index.js
pipinstall: pip install -r requirements.txt

更新2

没有 pipinstallgit push heroku master 的输出:

$ git push heroku master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=true
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  5.9.1
remote:        engines.npm (package.json):   unspecified (use default)
remote:        
remote:        Downloading and installing node 5.9.1...
remote:        Using default npm version: 3.7.3
remote: 
remote: -----> Restoring cache
remote:        Loading 2 from cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (not cached - skipping)
remote: 
remote: -----> Building dependencies
remote:        Installing node modules (package.json)
remote: 
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (nothing to cache)
remote: 
remote: -----> Build succeeded!
remote:        ├── ejs@2.4.1 extraneous
remote:        ├── emailjs@1.0.8 extraneous
remote:        ├── express@4.13.3
remote:        ├── http-status@0.2.3
remote:        ├── nodemailer@1.4.0
remote:        ├── nodemailer-smtp-transport@1.0.4
remote:        ├── python-shell@0.4.0
remote:        └── xoauth2@1.2.0
remote:        
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 13M
remote: -----> Launching...
remote:        Released v25
remote:        https://arcane-anchorage-33274.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.

最佳答案

尝试删除已弃用的 heroku-buildpack-multi并使用 Heroku buildpacks命令:

$ heroku buildpacks:add --index 1 heroku/nodejs
$ heroku buildpacks:add --index 2 heroku/python
$ heroku buildpacks
=== foobar Buildpack URLs
1. heroku/nodejs
2. heroku/python

来自文档:

The buildpack for the primary language of your app should always be the last buildpack in the list. This ensures that defaults for that primary language are applied instead of those for another language, and allows Heroku to correctly detect the primary language of your app.

关于Heroku 上的 Python 和 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39897505/

相关文章:

docker - 为什么 Heroku 每次都重建我的 Docker 容器?

ruby-on-rails - Heroku 上的私有(private)酒吧 | rails 3

python - 对多个项目进行分组

python - 用于提取域和子域的正则表达式

python - ipython iptest ImportError(没有名为 ipython 的模块)

node.js - mongoose 通过嵌套对象 ID 查找返回整个文档

python - 如何处理KeyError : "[' blah'] not in index"

node.js - 如何将事件从nodeJS/express发送到Angular

javascript - Firebase 云函数中的嵌套 HTTP 请求

node.js - 未从 Slack Slash Commands API 接收任何数据