运行 Flask 应用程序可以使用 `python` 而不是 `uwsgi` 吗?
Can run Flask application with `python` but not `uwsgi`?
我正在尝试将 uWSGI 设置为 运行 一个简单的 Flask 应用程序。我遵循了 this tutorial,效果很好,但是在我的项目中实现相同的概念时它不起作用。
这是我的目录结构:
/project
--/bin
----server.py
----wsgi.py
当我在 /project
目录中时,python 命令 python bin/wsgi.py
工作正常。然而,uwsgi 命令 uwsgi --socket 0.0.0.0:8000 --protocol=http --wsgi-file bin/wsgi.py
没有。这是 server.py
:
from flask import Flask, jsonify, request
app = Flask(__name__)
# all my routes below...
这里是wsgi.py
:
import os
from server import app as application
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
application.run(debug=True)
这是我得到的错误:
Traceback (most recent call last):
File "bin/wsgi.py", line 2, in <module>
from server import app
ImportError: No module named server
谢谢!
为什么 python bin/mod.py
有效?
因为 Python 设置 sys.path
以包含您的 bin
目录,基于目录 mod.py
(命令行参数)位于:
As initialized upon program startup, the first item of this list, path[0]
, is the directory containing the script that was used to invoke the Python interpreter.
您可以通过将 import sys
和 print(sys.path)
添加到您的 wsgi.py
来检查这一点。第一个元素是您的 bin
目录(完整的绝对路径)。
为什么 uwsgi --wsgi-file bin/wsgi.py
没用?
因为解释器不知道去哪里找server.py
。 uWSGI 没有采用 bin/wsgi.py
的路径,也没有将它添加到嵌入式解释器的 sys.path
(通过 Py_SetProgramName
内部 API 调用,PySys_SetPath
或其他方式) - 它刚刚初始化了解释器并加载了文件。
基本上,uWSGI 不是 Python 解释器,它只是承载它,嵌入。
If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
为什么 --pythonpath $(pwd)/bin
固定的东西?
因为你告诉 Python 解释器在哪里寻找导入的模块。 :-)
我正在尝试将 uWSGI 设置为 运行 一个简单的 Flask 应用程序。我遵循了 this tutorial,效果很好,但是在我的项目中实现相同的概念时它不起作用。
这是我的目录结构:
/project
--/bin
----server.py
----wsgi.py
当我在 /project
目录中时,python 命令 python bin/wsgi.py
工作正常。然而,uwsgi 命令 uwsgi --socket 0.0.0.0:8000 --protocol=http --wsgi-file bin/wsgi.py
没有。这是 server.py
:
from flask import Flask, jsonify, request
app = Flask(__name__)
# all my routes below...
这里是wsgi.py
:
import os
from server import app as application
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
application.run(debug=True)
这是我得到的错误:
Traceback (most recent call last):
File "bin/wsgi.py", line 2, in <module>
from server import app
ImportError: No module named server
谢谢!
为什么
python bin/mod.py
有效?因为 Python 设置
sys.path
以包含您的bin
目录,基于目录mod.py
(命令行参数)位于:As initialized upon program startup, the first item of this list,
path[0]
, is the directory containing the script that was used to invoke the Python interpreter.您可以通过将
import sys
和print(sys.path)
添加到您的wsgi.py
来检查这一点。第一个元素是您的bin
目录(完整的绝对路径)。为什么
uwsgi --wsgi-file bin/wsgi.py
没用?因为解释器不知道去哪里找
server.py
。 uWSGI 没有采用bin/wsgi.py
的路径,也没有将它添加到嵌入式解释器的sys.path
(通过Py_SetProgramName
内部 API 调用,PySys_SetPath
或其他方式) - 它刚刚初始化了解释器并加载了文件。基本上,uWSGI 不是 Python 解释器,它只是承载它,嵌入。
If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
为什么
--pythonpath $(pwd)/bin
固定的东西?因为你告诉 Python 解释器在哪里寻找导入的模块。 :-)