admin管理员组文章数量:1194733
Im working on my flask backend api. I have a .env file in the directory with the following variables: CLIENT_ID
, CLIENT_SECRET
, MYSQL_PASSWORD
, FLASK_APP
. I use os.getenv("CLIENT_ID")
and os.getenv("CLIENT_SECRET")
and the external api im connecting to is successful and im able to request data from it.
I do the same thing for connecting to my db, by accessing os.getenv("MYSQL_PASSWORD")
and configuring the URI:
db_pswrd = os.getenv("MYSQL_PASSWORD")
app.config["SQLALCHEMY_DATABASE_URI"] = f"mysql://root:{db_pswrd}@localhost/my_db"
However once I try to connect to mysql database via Flask-SQLAlchemy the following error happens:
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
(Background on this error at: )
Note that I heavily truncated the error message.
So now I used the function load_dotenv()
from python-dotenv module before calling
os.getenv("MYSQL_PASSWORD")
and the database error disappeared and im able to connect to the db. How come I dont have to call this function and can directly access CLIENT_ID
and CLIENT_SECRET
via os.getenv()
for the external api yet I cannot do that for MYSQL_PASSWORD
? These env variables are all in .env
Im working on my flask backend api. I have a .env file in the directory with the following variables: CLIENT_ID
, CLIENT_SECRET
, MYSQL_PASSWORD
, FLASK_APP
. I use os.getenv("CLIENT_ID")
and os.getenv("CLIENT_SECRET")
and the external api im connecting to is successful and im able to request data from it.
I do the same thing for connecting to my db, by accessing os.getenv("MYSQL_PASSWORD")
and configuring the URI:
db_pswrd = os.getenv("MYSQL_PASSWORD")
app.config["SQLALCHEMY_DATABASE_URI"] = f"mysql://root:{db_pswrd}@localhost/my_db"
However once I try to connect to mysql database via Flask-SQLAlchemy the following error happens:
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
(Background on this error at: https://sqlalche.me/e/20/e3q8)
Note that I heavily truncated the error message.
So now I used the function load_dotenv()
from python-dotenv module before calling
os.getenv("MYSQL_PASSWORD")
and the database error disappeared and im able to connect to the db. How come I dont have to call this function and can directly access CLIENT_ID
and CLIENT_SECRET
via os.getenv()
for the external api yet I cannot do that for MYSQL_PASSWORD
? These env variables are all in .env
1 Answer
Reset to default 0You said you're using flask. Flask does a load_dotenv
call during its setup. If you call os.getenv
before you import flask
, it won't see the .env
variables, but any calls AFTER the import will.
本文标签:
版权声明:本文标题:python - Why does os.getenv work with other environment variables, but cannot retrieve Mysql password variable in .env file? - S 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738453123a2087602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
os.getenv
pulls from the shell environment. It doesn't know anything about.env
files. That's all done by thedotenv
module. If you aren't using that module, then.env
files are ignored. – Tim Roberts Commented Jan 24 at 2:07CLIENT_ID
andCLIENT_SECRET
, or is it being done in another module? – Tim Roberts Commented Jan 24 at 3:55os.getenv("CLIENT_ID")
andos.getenv("CLIENT_SECRET")
in one of its methods – Viices Commented Jan 24 at 9:01