我正在使用flask和flask-restful开发REST API。我目前在flask-cli上遇到问题,因为在公司环境中无法使用该应用程序运行。可能是什么问题?
在企业中,我们将conda环境用于python 3应用程序。目前api的环境由运行在CentOS 7上的带有烧瓶1.0.2和带有烧瓶的0.37的python 3.6.8组成,即使是通过conda的需求或enviroment.yml重新创建环境,我也无法运行该应用程序,不同从我的个人计算机ubuntu 18.04上,使用miniconda3和env python 3.6.8,具有相同的flask软件包,我可以进行测试而没有问题。
错误:
import socket
import select
import msvcrt
IP = "192.168.1.154"
port = 123
sockets = []
def write():
pass
def main():
sock = socket.socket()
sock.connect((IP, port))
while True:
rlist, wlist, xlist = select.select(sockets, sockets, [])
for current_socket in rlist:
if current_socket is sock:
data = current_socket.recv(1024)
print(data)
else:
sockets.append(current_socket)
write()
if __name__ == '__main__':
main()
脚本主要:
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
ModuleNotFoundError: No module named 'main'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 325, in __call__
self._flush_bg_loading_exception()
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 313, in _flush_bg_loading_exception
reraise(*exc_info)
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 302, in _load_app
self._load_unlocked()
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 246, in locate_app
'Could not import "{name}".'.format(name=module_name)
flask.cli.NoAppException: Could not import "main".
脚本 init .py:
from app import create_app, db
from flask_migrate import Migrate
app = create_app('development')
Migrate(app,db)
@app.shell_context_processor
def shell_context():
return dict(
app = app,
db=db
)
我希望能够在公司环境中运行测试和运行应用程序。因为即使使用
from flask_restful import Api
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
api=Api(app, prefix="/api/v1")
db.init_app(app)
from app.resources.catcherDicts import Catcher
api.add_resource(Catcher, "/catcher")
return app
命令export FLASK_APP = main.py
导出应用程序路径也无法运行该应用程序。
答案 0 :(得分:1)
错误:
ModuleNotFoundError:没有名为“ main”的模块
告诉您无法从main.py
模块中找到cli.py
模块。发生这种情况的原因是,使用以下代码行:main.py
将进入应用程序的入口点设置为export FLASK_APP = main.py
。我怀疑此环境变量不正确,我将尝试直接运行您的条目文件并查看其是否有效,如果可以,则您的环境变量不正确。此外,如果您不是从包含入口点的目录中运行文件,请确保<path>
的设置正确,否则flask run
无效。
希望有帮助!