未找到部署 Flask 应用生产 'config'

Deploy Flask App Production 'config' not found

我正在构建一个 Flask 应用程序,然后尝试在另一个 venv 中安装它。 问题是我的配置文件没有被提取。

我用 python setup.py sdist 创建了 tar 文件,最后得到以下结构。

然后我创建一个新的 venv 并使用 pip install energy-1.0.tar.gz 安装 tar 文件 当我然后 start 一个 ptyhon shell 并导入应用程序时 returns ModuleNotFoundError: No module named 'config'

这是我的应用程序文件的代码,我还尝试将根目录添加到路径中。


from flask import logging

os.path.abspath(os.path.dirname(__file__))

from app import app

if __name__ == "__main__":
    gunicorn_logger = logging.getLogger('gunicorn.error')
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)
    app.run()

以及发生错误的app模块的init文件

import os
from flask import Flask
from flask_login import LoginManager

from .auth.auth_controller import auth_app
from .database import db
from .database.models import User
from .profile.profile_controller import profile_app
from .uploads.file_upload_controller import file_upload_app

app = Flask(__name__)

gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)

app.config['SECRET_KEY'] = 'secret-key-goes-here'
env = app.config['ENV']

if env == "production":
    app.config.from_object('config.Config')
elif env == "development":
    app.config.from_object('config.ConfigDevelopment')
elif env == "local":
    os.environ['AUTHLIB_INSECURE_TRANSPORT'] = '1'
    app.config.from_object("config.ConfigLocal")

堆栈:

>>> import app
Traceback (most recent call last):
  File "/private/tmp/tmp/lib/python3.9/site-packages/werkzeug/utils.py", line 873, in import_string
    module = __import__(module_name, globals(), locals(), [obj_name])
ModuleNotFoundError: No module named 'config'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/private/tmp/tmp/lib/python3.9/site-packages/app/__init__.py", line 35, in <module>
    app.config.from_object('config.Config')
  File "/private/tmp/tmp/lib/python3.9/site-packages/flask/config.py", line 161, in from_object
    obj = import_string(obj)
  File "/private/tmp/tmp/lib/python3.9/site-packages/werkzeug/utils.py", line 881, in import_string
    raise ImportStringError(import_name, e).with_traceback(sys.exc_info()[2])
  File "/private/tmp/tmp/lib/python3.9/site-packages/werkzeug/utils.py", line 873, in import_string
    module = __import__(module_name, globals(), locals(), [obj_name])
werkzeug.utils.ImportStringError: import_string() failed for 'config.Config'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'config' not found.

Original exception:

ModuleNotFoundError: No module named 'config'

所以问题是如何在使用pip安装模块后从根目录访问配置。

好的,我在这个 post pip install . creates only the dist-info not the package

的帮助下弄明白了

我必须将配置作为 py_modules 包含在 setup.py 文件中。

setup(
    ...
    py_modules=['config'],
   ...
)