#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
0:01 that I wanted to discuss here.
0:03 And that was that environment variable
0:05 that we created, that flask_app.
0:07 To view it, we can just quickly some up windows
0:11 we'll do it this way.
0:13 We can see it their, flask app equals our demo.pie file.
0:17 Now unfortunately, what happens is
0:19 when I close this terminal window
0:21 when I can cancel this session or exit out
0:23 we lose that data.
0:26 So let me show you that, one second.
0:27 Let's deactivate and let's exit out.
0:33 And now with the new terminal launched, let's
0:36 just get into our folder 1-flask.
0:43 All right so there we're back in here.
0:46 Now, let's activate our virtual environment.
0:51 Right, and now if we run that same command...
0:56 Flask app its gone.
0:58 Okay, so if we were to run
1:01 flask run, we're going to have issues.
1:03 It doesn't know what it's doing.
1:05 It doesn't know what file to talk to.
1:07 It's just running nothing, all right.
1:09 So it's even say's it here
1:10 "you did not provide the flask app environment variable".
1:13 So, the problem there is, every time you
1:16 close your environment for the day
1:18 if you happen to shut off your computer
1:20 and you come back, you're not going to be
1:22 able to do this.
1:23 You're going to have to reset that variable.
1:25 And it's really frustrating.
1:27 So, one work around for this, is to install
1:31 a new package into our virtual environment here.
1:34 Called python-dotenv, it's this one here
1:37 let's pick install it now.
1:39 If I can type pip install python-dotenv
1:47 Now, what this module allows to do, it will
1:49 allow us to specify that environment variable
1:53 in a environment file, in this parent directory here.
1:58 And what we do is, we create that file
2:00 that's actually going to be called .flaskenv
2:03 So let's go Vim
2:06 okay because we're in an environment file.
2:09 We'll be titled in Linux and everything
2:11 is classified as a dot file, so
2:13 Vim .flaskenv
2:16 and we throw that line in there.
2:20 FLASK_APP=demo.py
2:26 Save that, oops. All right, and now
2:34 flask, run, there we have it.
2:37 So, what this does is it now refers
2:39 to that .flaskenv file.
2:41 So if we close out terminal session
2:43 and we cancel it out and we come
2:45 back into another week, we don't have to
2:47 remember signing that environment variable again.
2:50 It's just taken care of by the .flaskenv file
2:53 and the dotenv package.
2:56 So, install that, get that cracking and save
2:59 yourself a bit of heartache.
3:00 Just remember to put it in all of your
3:01 projects from here on in.