【发布时间】:2021-03-23 20:10:56
【问题描述】:
我正在尝试将我的 flask+react 应用程序部署到 heroku,但它崩溃并给我以下错误:
ModuleNotFoundError: 没有名为“fetch_data”的模块
这是日志的 sn-p:
2021-03-23T19:34:05.332761+00:00 app[web.1]: Traceback (most recent call last):
2021-03-23T19:34:05.332763+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py",
line 583, in spawn_worker
2021-03-23T19:34:05.332764+00:00 app[web.1]: worker.init_process()
2021-03-23T19:34:05.332765+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base
.py", line 119, in init_process
2021-03-23T19:34:05.332765+00:00 app[web.1]: self.load_wsgi()
2021-03-23T19:34:05.332765+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base
.py", line 144, in load_wsgi
2021-03-23T19:34:05.332766+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-03-23T19:34:05.332766+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py"
, line 67, in wsgi
2021-03-23T19:34:05.332767+00:00 app[web.1]: self.callable = self.load()
2021-03-23T19:34:05.332767+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.
py", line 49, in load
2021-03-23T19:34:05.332767+00:00 app[web.1]: return self.load_wsgiapp()
2021-03-23T19:34:05.332768+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.
py", line 39, in load_wsgiapp
2021-03-23T19:34:05.332768+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-03-23T19:34:05.332768+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py", li
ne 358, in import_app
2021-03-23T19:34:05.332769+00:00 app[web.1]: mod = importlib.import_module(module)
2021-03-23T19:34:05.332769+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, i
n import_module
2021-03-23T19:34:05.332770+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-03-23T19:34:05.332771+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2021-03-23T19:34:05.332771+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2021-03-23T19:34:05.332771+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
2021-03-23T19:34:05.332772+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
2021-03-23T19:34:05.332772+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
2021-03-23T19:34:05.332772+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_remove
d
2021-03-23T19:34:05.332773+00:00 app[web.1]: File "/app/api/api.py", line 2, in <module>
2021-03-23T19:34:05.332773+00:00 app[web.1]: from fetch_data import fetch_data
2021-03-23T19:34:05.332773+00:00 app[web.1]: ModuleNotFoundError: No module named 'fetch_data'
2021-03-23T19:34:05.332985+00:00 app[web.1]: [2021-03-23 19:34:05 +0000] [9] [INFO] Worker exiting (pid: 9)
2021-03-23T19:34:05.391634+00:00 app[web.1]: [2021-03-23 19:34:05 +0000] [4] [INFO] Shutting down: Master
2021-03-23T19:34:05.392273+00:00 app[web.1]: [2021-03-23 19:34:05 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-03-23T19:34:05.544193+00:00 heroku[web.1]: Process exited with status 3
2021-03-23T19:34:05.617532+00:00 heroku[web.1]: State changed from up to crashed
现在fetch_data 是我在api 文件夹中制作的一个包。下面是api文件夹的目录结构:
.
|-- __pycache__
| |-- api.cpython-37.pyc
| `-- fetch_data.cpython-37.pyc
|-- api.py
|-- fetch_data
| |-- __init__.py
| `-- fetch_data.py
|-- static
| `-- react
|-- templates
| `-- index.html
|-- test.py
`-- venv
|-- Include
|-- Lib
|-- Scripts
`-- pyvenv.cfg
fetch_data.py 是我在api.py. 中调用的烧瓶蓝图
this is a snippet of api.py:
from flask import Flask, jsonify, render_template
from fetch_data import fetch_data
from nsepy import get_history
from datetime import date,timedelta
import pandas as pd
import os
import json
app = Flask(__name__)
app.register_blueprint(fetch_data.fetch_api)
@app.route('/')
def index():
return render_template("index.html", flask_token = "Hello World")
.
.
.
if __name__ == '__main__':
port =int(os.environ['PORT'])
app.run(host='0.0.0.0', debug=False, port= port)
这是fetch_data.py的代码:
from flask import Blueprint
from nsetools import Nse
fetch_api = Blueprint('fetch_api', __name__, url_prefix='/fetch')
@fetch_api.route('/api/<string:ticker>', methods = ['GET'])
def fetch_data(ticker):
nse = Nse()
return nse.get_quote(ticker, as_json = True)
@fetch_api.route('/gainers', methods = ['GET'])
def fetch_gainers():
nse = Nse()
return nse.get_top_gainers(as_json = True)
@fetch_api.route('/losers', methods = ['GET'])
def fetch_losers():
nse = Nse()
return nse.get_top_losers(as_json = True)
我看到一些答案提到Procfile 可能有问题,但我认为我的看起来不错。
web: gunicorn -w 1 -b 0.0.0.0:$PORT api.api:app
我对flask和heroku还是新手,所以如果这个问题的解决方案很明显,我深表歉意。提前感谢您的帮助。
【问题讨论】: