#100DaysOfWeb in Python Transcripts
Chapter: Days 1-4: Flask Intro
Lecture: Persistent FLASK_APP environment variable using python-dotenv

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So there's one little gutcher that I wanted to discuss here. And that was that environment variable that we created, that flask_app.
0:08 To view it, we can just quickly some up windows we'll do it this way. We can see it their, flask app equals our demo.pie file.
0:18 Now unfortunately, what happens is when I close this terminal window when I can cancel this session or exit out we lose that data.
0:27 So let me show you that, one second. Let's deactivate and let's exit out. And now with the new terminal launched, let's
0:37 just get into our folder 1-flask. All right so there we're back in here. Now, let's activate our virtual environment.
0:52 Right, and now if we run that same command... Flask app its gone. Okay, so if we were to run flask run, we're going to have issues.
1:04 It doesn't know what it's doing. It doesn't know what file to talk to. It's just running nothing, all right. So it's even say's it here
1:11 "you did not provide the flask app environment variable". So, the problem there is, every time you close your environment for the day
1:19 if you happen to shut off your computer and you come back, you're not going to be able to do this. You're going to have to reset that variable.
1:26 And it's really frustrating. So, one work around for this, is to install a new package into our virtual environment here.
1:35 Called Python-dotenv, it's this one here let's pick install it now. If I can type pip install Python-dotenv Now, what this module allows to do, it will
1:50 allow us to specify that environment variable in a environment file, in this parent directory here. And what we do is, we create that file
2:01 that's actually going to be called .flaskenv So let's go Vim okay because we're in an environment file. We'll be titled in Linux and everything
2:12 is classified as a dot file, so Vim .flaskenv and we throw that line in there. FLASK_APP=demo.py Save that, oops. All right, and now
2:35 flask, run, there we have it. So, what this does is it now refers to that .flaskenv file. So if we close out terminal session
2:44 and we cancel it out and we come back into another week, we don't have to remember signing that environment variable again.
2:51 It's just taken care of by the .flaskenv file and the dotenv package. So, install that, get that cracking and save yourself a bit of heartache.
3:01 Just remember to put it in all of your projects from here on in.

Talk Python's Mastodon Michael Kennedy's Mastodon