I'm trying to import a python file into node web application. On my local machine it works fine, but when I upload it to heroku, the modules don't get imported correctly, and I get an error message saying, ModuleNotFoundError: No module named 'tensorflow'.
I'm assuming that the modules are not installed. So I made a requirements.txt file, and imported in python like this:
import subprocess
import sys
subprocess.call([sys.executable, "-r", "pip3", "install", 'requirements.txt'])
I still get the previous error of: ModuleNotFoundError: No module named 'tensorflow'. How can I install python modules in heroku while using python-shell?
Python Imports
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()
Node
const options = {
pythonOptions: ['-u'],
pythonPath: 'python3'
};
let pyshell = new PythonShell('./python_2/script.py', options);
Here's the full output error I'm receiving:
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. (/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
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. (/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
解决方案
Assuming you've already configured multiple buildpacks for your app, which if you've got both node and python you likely have, you should be able to simply add tensorflow to your existing requirements.txt file to have it installed during deployment.
That file should be in the root directory of your application, beside your package.json.
I just added a runtime.txt file inserting: Python-3.6.5 but I'm still getting the error
Your runtime.txt should also be in the root directory of your project, and your requested Python version should be all lowercase, e.g.
python-3.6.5