[英]flask run does not work but python server.py does! Environment vars set
When I try to run my very simple Flask application using flask run
it does not work but when I cd src
and python server.py
it does work.当我尝试使用
flask run
运行我非常简单的 Flask 应用程序时,它不起作用,但是当我cd src
和python server.py
它确实起作用。
File structure:文件结构:
my_project
|____init__.py
|
|__src
| |____init__.py
| |__server.py
| |__chat.py
|
|__test
| |____init__.py
| |__test_server.py
| |__test_chat.py
|
|__FLASK_ENV_VARS.sh
server.py
from flask import Flask, jsonify
import chat
api = Flask(__name__)
@api.route('/')
def root():
message = chat.say_hello()
return jsonify(message=message)
if __name__ == '__main__':
api.run(debug=True)
In order to get the tests to pass, I need to from src import chat
.为了让测试通过,我需要
from src import chat
。 When I leave it as import chat
the server runs when I try python server.py
but not flask run
(which doesn't run either way of importing chat).当我将其保留为
import chat
,服务器会在我尝试python server.py
但不是flask run
(它不会以任何一种方式运行导入聊天)时运行。 Interestingly, I get a red squggly line under chat from my IDE when import chat
that goes away with from src import chat
.有趣的是,当
import chat
与from src import chat
消失时,我在 IDE 的import chat
下看到一条红色波浪线。
test_server.py
import pytest
from src.server import api
def test_route(client):
response = client.get('/')
assert response.status_code == 200
expected_data = b'{"message":"Welcome message here"}\n'
assert response.data == expected_data
@pytest.fixture
def client():
api.testing = True
client = api.test_client()
return client
The content of the environment variables shell script is环境变量shell脚本的内容是
export FLASK_ENV=development
export FLASK_DEBUG=1
export FLASK_APP=server.py
I have tried making FLASK_APP
both src.server.py
and server.py
, neither currently works.我已经尝试使
FLASK_APP
src.server.py
和server.py
,目前都没有工作。 Of course I remember to run the shell script and echo the variables to ensure that they have changed.当然,我记得运行 shell 脚本并回显变量以确保它们已更改。
The error I get from flask run
is flask.cli.NoAppException: Could not import "my_project.server".
我从
flask run
得到的错误是flask.cli.NoAppException: Could not import "my_project.server".
When I cd src
and flask run
I get flask.cli.NoAppException: While importing "my_project.src.server", an ImportError was raised:
当我
cd src
和flask run
我得到flask.cli.NoAppException: While importing "my_project.src.server", an ImportError was raised:
The problem, I suspect lies with the way chat is imported.问题,我怀疑在于聊天的导入方式。 The tests only run with
from src import chat
.测试仅使用
from src import chat
运行。 When I run python server.py
I have to have only import chat
or it doesn't work.当我运行
python server.py
我必须只import chat
否则它不起作用。 I get ModuleNotFoundError: No module named 'chat'
as part of the stack trace or ModuleNotFoundError: No module named 'src'
depending on which style of import was used.我得到
ModuleNotFoundError: No module named 'chat'
作为堆栈跟踪的一部分或ModuleNotFoundError: No module named 'src'
取决于使用的导入样式。 I'm not sure what I am getting wrong here.我不确定我在这里做错了什么。 I am inclined to think that
from src import chat
is correct since this is what makes the tests pass, however I can't get this to work at all.我倾向于认为
from src import chat
是正确的,因为这是使测试通过的原因,但是我根本无法使其正常工作。
Did you try $env:FLASK_APP = "server.py"
你有没有试过
$env:FLASK_APP = "server.py"
try Something like this too , export FLASK_APP=my_project/src/server.py; flask run
也试试这样的东西,
export FLASK_APP=my_project/src/server.py; flask run
export FLASK_APP=my_project/src/server.py; flask run
You can also mention app = Flask (__name__) in __init__.py and then execute flask run
你也可以
app = Flask (__name__) in __init__.py and then execute flask run
提到app = Flask (__name__) in __init__.py and then execute flask run
通过将server.py中有问题的导入更改为
from my_project.src import chat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.