gpt4 book ai didi

python - 为什么我更新了.py文件后还要重新运行python?

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:35 25 4
gpt4 key购买 nike

我是 Python 和 Flask 的新手。我是 PHP 和 CodeIgniter 的程序员。

最近喜欢写python的代码。但是,我发现了一个问题。

这是 hello.py 代码。

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'

if __name__ == '__main__':
app.run()

写下 python 代码后,我必须在 Linux SSH shell 上运行它。

#python hello.py

像这样。

每当我更改代码时,我都必须打开 SSH shell 并重新运行 hello.py 代码。

为什么我更新.py文件后还要重新运行python?

有没有办法自动运行.py文件?

最佳答案

Flask 有一个 debug mode为此:

The run() method is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong.

有两种方法可以启用调试。在应用程序对象上设置该标志:

app.debug = True
app.run()

或者将其作为参数传递给运行:

app.run(debug=True)

关于python - 为什么我更新了.py文件后还要重新运行python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com