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/

相关文章:

javascript - 如何在 Sequelize 模型中使用导入?

javascript - 没有 sudo 无法运行 npm install。将权限设置为 .npm 不起作用

python - 重新运行程序时出现问题

python - 为什么 python 切片中允许使用非整数内置类型?

javascript - 在 Express static 中使用中间件在 Node js 中不起作用

node.js - 致命: Invalid refspec (Heroku nodejs)

static - Heroku Cedar 纯 Rack 静态站点

java - 部署到 Heroku 中的 java Spark 项目的文件路径

python - 使用循环链表插入功能

python - numpy.rint 没有按预期工作