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

Share Improve this question asked Jan 24 at 1:55 ViicesViices 11 bronze badge 4
  • 1 os.getenv pulls from the shell environment. It doesn't know anything about .env files. That's all done by the dotenv module. If you aren't using that module, then .env files are ignored. – Tim Roberts Commented Jan 24 at 2:07
  • @TimRoberts How come if I remove the CLIENT_ID and CLIENT_SECRET from the .env file I cannot access to my external api? These variables are in .env and defined there only, and im accessing it via getenv without calling load_dotenv() – Viices Commented Jan 24 at 2:17
  • 1 Are YOU accessing CLIENT_ID and CLIENT_SECRET, or is it being done in another module? – Tim Roberts Commented Jan 24 at 3:55
  • @TimRoberts I have a file/module called IGDB.py which is a class that resembles the external api im using and handles all the authentication logic of the external api. It calls os.getenv("CLIENT_ID") and os.getenv("CLIENT_SECRET") in one of its methods – Viices Commented Jan 24 at 9:01
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

本文标签: