gpt4 book ai didi

python - 将 Flask 设置为为每个环境单独配置时的非现有路径

转载 作者:行者123 更新时间:2023-11-28 22:31:21 31 4
gpt4 key购买 nike

我为每个环境和一个单独的应用程序分别配置了目录树如下所示:

myapp
├── __init__.py # empty
├── config
│   ├── __init__.py # empty
│   ├── development.py
│   ├── default.py
│   └── production.py
├── instance
│   └── config.py
└── myapp
├── __init__.py
   └── myapp.py

代码

相关代码,myapp/__init__.py:

from flask import Flask

app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.default')
app.config.from_pyfile('config.py')
app.config.from_envvar('APP_CONFIG_FILE')

myapp/myapp.py:

from myapp import app
# ...

命令

然后我设置变量:

$export FLASK_APP=myapp.py

并尝试从项目根目录运行开发服务器:

$ flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (myapp.py) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py

从项目 myapp 文件夹:

$ cd myapp
$ flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (myapp.myapp.myapp) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py

使用另一个 FLASK_APP 变量:

$ export FLASK_APP=myapp/myapp.py
# in project root
$ flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (myapp.myapp.myapp) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
# moving to project/myapp
$ cd myapp
$ flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (myapp/myapp.py) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py

其他测试未成功

$ python -c 'import myapp; print(myapp)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/user/myapp/myapp/__init__.py", line 6, in <module>
app.config.from_envvar('APP_CONFIG_FILE')
File "/home/user/.virtualenvs/myapp/lib/python3.5/site-packages/flask/config.py", line 108, in from_envvar
variable_name)
RuntimeError: The environment variable 'APP_CONFIG_FILE' is not set and as such configuration could not be loaded. Set this variable and make it point to a configuration file

$ export APP_CONFIG_FILE="/home/user/myapp/config/development.py"
$ python -c 'import myapp; print(myapp)'<module 'myapp' from '/home/user/myapp/myapp/__init__.py'>
$ flask run
Usage: flask run [OPTIONS]
Error: The file/path provided (myapp.myapp) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py

注意事项:

最佳答案

我花了一段时间,但我终于找到了:

  1. Flask 不喜欢在根级别具有 __init__.py 的项目,请删除 myapp/__init__.py。这是位于根文件夹中的那个:

    myapp
    ├── __init__.py <--- DELETE
    ...
    └── myapp
    ├── __init__.py <--- keep
    └── myapp.py
  2. 使用$ export FLASK_APP=myapp/myapp.py

  3. 指定配置的环境变量应该是它的绝对路径:export APP_CONFIG_FILE="/home/user/myapp/config/development.py"

现在一切正常\o/

$ flask run
* Serving Flask app "myapp.myapp"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

$ flask shell
Python 3.5.2+ (default, Sep 22 2016, 12:18:14)
[GCC 6.2.0 20160927] on linux
App: myapp
Instance: /home/user/myapp/instance
>>>

关于python - 将 Flask 设置为为每个环境单独配置时的非现有路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615662/

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