【问题标题】:Install python modules in heroku while using python-shell not working在使用 python-shell 时在 heroku 中安装 python 模块不起作用
【发布时间】:2019-09-03 08:59:45
【问题描述】:

我正在尝试将 python 文件导入节点 Web 应用程序。在我的本地机器上它工作正常,但是当我将它上传到 heroku 时,模块没有正确导入,并且我收到一条错误消息,提示 ModuleNotFoundError: No module named 'tensorflow'

我假设没有安装这些模块。所以我做了一个 requirements.txt 文件,并像这样在 python 中导入:

import subprocess
import sys
subprocess.call([sys.executable, "-r", "pip3", "install", 'requirements.txt'])

我仍然收到之前的错误:ModuleNotFoundError: No module named 'tensorflow'。如何在使用 python-shell 时在 heroku 中安装 python 模块?

Python 导入

import json
import pickle
import random
import tensorflow as tf
import tflearn
import numpy as np
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()

节点

const options = {
    pythonOptions: ['-u'],
    pythonPath: 'python3'
};
let pyshell = new PythonShell('./python_2/script.py', options);

这是我收到的完整输出错误:

2019-09-02T19:11:03.182951+00:00 app[web.1]: events.js:174
2019-09-02T19:11:03.182964+00:00 app[web.1]: throw er; // Unhandled 'error' event
2019-09-02T19:11:03.182967+00:00 app[web.1]: ^
2019-09-02T19:11:03.182969+00:00 app[web.1]: 
2019-09-02T19:11:03.182971+00:00 app[web.1]: Error: ModuleNotFoundError: No module named 'tensorflow'
2019-09-02T19:11:03.182973+00:00 app[web.1]: at PythonShell.parseError (/app/node_modules/python-shell/index.js:260:21)
2019-09-02T19:11:03.182975+00:00 app[web.1]: at terminateIfNeeded (/app/node_modules/python-shell/index.js:139:32)
2019-09-02T19:11:03.182978+00:00 app[web.1]: at ChildProcess.<anonymous> (/app/node_modules/python-shell/index.js:131:13)
2019-09-02T19:11:03.182980+00:00 app[web.1]: at ChildProcess.emit (events.js:189:13)
2019-09-02T19:11:03.182983+00:00 app[web.1]: at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
2019-09-02T19:11:03.182985+00:00 app[web.1]: ----- Python Traceback -----
2019-09-02T19:11:03.182987+00:00 app[web.1]: File "python_2/model.py", line 5, in <module>
2019-09-02T19:11:03.182989+00:00 app[web.1]: import tensorflow as tf
2019-09-02T19:11:03.182991+00:00 app[web.1]: Emitted 'error' event at:
2019-09-02T19:11:03.182994+00:00 app[web.1]: at terminateIfNeeded (/app/node_modules/python-shell/index.js:153:26)
2019-09-02T19:11:03.182996+00:00 app[web.1]: at ChildProcess.<anonymous> (/app/node_modules/python-shell/index.js:131:13)
2019-09-02T19:11:03.182998+00:00 app[web.1]: at ChildProcess.emit (events.js:189:13)
2019-09-02T19:11:03.183000+00:00 app[web.1]: at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
2019-09-02T19:11:03.224746+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-09-02T19:11:03.225516+00:00 app[web.1]: npm ERR! errno 1
2019-09-02T19:11:03.230041+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `node index.js`
2019-09-02T19:11:03.230381+00:00 app[web.1]: npm ERR! Exit status 1
2019-09-02T19:11:03.230929+00:00 app[web.1]: npm ERR!
2019-09-02T19:11:03.232455+00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2019-09-02T19:11:03.233317+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-09-02T19:11:03.270676+00:00 app[web.1]: 
2019-09-02T19:11:03.272904+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-09-02T19:11:03.273603+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-09-02T19_11_03_247Z-debug.log

【问题讨论】:

  • 为什么要在运行时安装依赖项?这应该在构建时完成。
  • 这很好。我计划做的是安装它们,然后删除安装它的那条线。
  • 正确的做法是什么?
  • 您已经有requirements.txtPipfilePipfile.lock,对吧?您需要其中一个才能在 Heroku 上安装 Python。只需在此处添加您的依赖项即可。
  • @Chris 我有一个requirements.txt 文件,但它仍然无法正常工作。插入requirements.txt 的正确路径是什么?目前位于:backend/python_2/requirements.txt

标签: python node.js express heroku


【解决方案1】:

假设您已经为您的应用配置了多个构建包,如果您同时拥有 nodepython,您应该能够简单地将 tensorflow 添加到现有的 requirements.txt 文件中在部署期间安装它。

该文件应位于您的应用程序的根目录中,在您的 package.json 旁边。

我刚刚添加了一个 runtime.txt 文件插入:Python-3.6.5 但我仍然收到错误

你的runtime.txt也应该在你项目的根目录下,并且你请求的Python版本应该全部小写,例如

python-3.6.5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2020-09-13
    • 2021-11-13
    • 1970-01-01
    • 2018-01-18
    • 2022-07-06
    • 2021-12-15
    • 2011-09-30
    相关资源
    最近更新 更多