windows Python Flask环境变量我的flask登录系统出现文件错误

inkz8wg9  于 2023-06-24  发布在  Windows
关注(0)|答案(1)|浏览(123)

我是在Windows上运行Python Flask的新手。我已经习惯了Linux。我的代码在Linux上运行良好,使用source .env,但由于我在Windows上,我做了一个env.bat,并在每行添加了set,如下所示:

set export SECRET_KEY=34uqhtg3rfhjqwief3woqijj4ki324jkDEWGF
set export DEBUG=True
set export APP_SETTINGS=config.DevelopmentConfig
set export DATABASE_URL=sqlite:///db.sqlite
set export FLASK_APP=src
set export FLASK_DEBUG=1

运行www.example.com时收到的错误manage.py:

File "C:\Users\admin\Documents\Dash\manage.py", line 3, in <module>
    from src import app
  File "C:\Users\admin\Documents\Dash\src\__init__.py", line 8, in <module>
    app.config.from_object(config("APP_SETTINGS"))
  File "C:\Users\admin\Documents\ws\Dash\env\lib\site-packages\decouple.py", line 248, in __call__
    return self.config(*args, **kwargs)
  File "C:\Users\admin\Documents\ws\Dash\env\lib\site-packages\decouple.py", line 107, in __call__
    return self.get(*args, **kwargs)
  File "C:\Users\admin\Documents\ws\Dash\env\lib\site-packages\decouple.py", line 92, in get
    raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: APP_SETTINGS not found. Declare it as envvar or define a default value.

manage.py:

from flask.cli import FlaskGroup

from src import app

cli = FlaskGroup(app)

if __name__ == "__main__":
    cli()

init.py:

from decouple import config
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(config("APP_SETTINGS"))

bcrypt = Bcrypt(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)

# Registering blueprints
from src.accounts.views import accounts_bp
from src.core.views import core_bp

app.register_blueprint(accounts_bp)
app.register_blueprint(core_bp)

config.py:

from decouple import config

DATABASE_URI = config("DATABASE_URL")
if DATABASE_URI.startswith("postgres://"):
    DATABASE_URI = DATABASE_URI.replace("postgres://", "postgresql://", 1)

class Config(object):
    DEBUG = False
    TESTING = False
    CSRF_ENABLED = True
    SECRET_KEY = config("SECRET_KEY", default="guess-me")
    SQLALCHEMY_DATABASE_URI = DATABASE_URI
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    BCRYPT_LOG_ROUNDS = 13
    WTF_CSRF_ENABLED = True
    DEBUG_TB_ENABLED = False
    DEBUG_TB_INTERCEPT_REDIRECTS = False

class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True
    WTF_CSRF_ENABLED = False
    DEBUG_TB_ENABLED = True

class TestingConfig(Config):
    TESTING = True
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = "sqlite:///testdb.sqlite"
    BCRYPT_LOG_ROUNDS = 1
    WTF_CSRF_ENABLED = False

class ProductionConfig(Config):
    DEBUG = False
    DEBUG_TB_ENABLED = False

这里的问题是什么?谢谢你。任何帮助都非常感激。我的

laawzig2

laawzig21#

看起来您没有正确设置环境变量。
在Windows上,您只需使用set命令,因此在bat文件中,您应该删除export命令,因为它被解释为字符串"export"
请参阅此处set文档set [<variable>=[<string>]]

相关问题