Visual Studio Code for Python Developers Transcripts
Chapter: Managing Environments
Lecture: Setting Environmental Variables

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Another thing for us to think about as we're getting our workspace setup is, well, where do our application settings go?
0:07 Depending on the type of project that you're working on, you might have a variety of different settings you need to store,
0:12 like API keys or access tokens, deployment settings, or even customized values specific to your application.
0:19 Now, of course, we can have these values hard-coded inside of our application code, but that's usually not a good idea for a variety of reasons.
0:26 For one, it could end up inside of source control, which can end up potentially exposing sensitive information about our application.
0:33 Also, it makes these values so much harder to change. So now every time we need to make an update to a configuration value,
0:40 we're going to have to end up redeploying our application. A better option is to use some type of
0:45 flexible configuration system that will allow us to easily read and write these values. A good option for that is using environment variables.
0:53 Now, environment variables are a platform-neutral way of storing dynamic configuration values usually as key-value pairs that can be accessed by
1:01 applications running on the operating system. It turns out Python has really good support for
1:06 working with environment variables using a standard library. Also, if you're going to use environment variables,
1:12 I definitely recommend you check out and see how they work inside of your operating system. Now, a good way to use environment variables locally in
1:20 development mode is using .env files. These are just simple text files that hold those key-value pair settings. So as your application,
1:28 it could look for this particular file, load up those settings, and now it could use them as it's running.
1:34 Again, I recommend that these files are mainly used during development time.
1:38 During production, you probably want to use whatever the native environment variable support is inside of your operating system.
1:44 With that being said, we can help with a Visual Studio code, but we can see how it helps us work with environment variables.
1:52 The first thing that we're going to do is create a new .env file in the root of our workspace. I'm going to add this.
1:58 I'm going to add this app name setting, and I'm going to set that to Podcast API. Now let's head over to our module and see if we could read this.
2:06 So I'm going to import the OS module from the standard library. And now let's head down to the main function.
2:14 So inside of main, I want to see if I could read this using the os.getenv function.
2:20 Now, I really like this function because it makes it easy to read these settings, and it does not return an exception if it can't find it.
2:26 So that's usually pretty helpful. Let's go ahead and set a breakpoint here. And we're going to debug this and we just see if it can read the value.
2:34 Now, I know we haven't spoken about debugging, but this would be a good, you know, primer into it. Okay, so it's loaded up our operation.
2:44 Notice how it's also set the virtual environment for us too. That's pretty cool. I'm going to hit run. Run. All right, now it's down an app name.
2:56 I'm going to step over. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
3:05 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
3:15 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
3:25 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
3:35 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
3:45 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
3:55 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
4:05 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
4:15 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
4:25 And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run. And I'm going to hit run.
4:35 And I'm going to hit run.


Talk Python's Mastodon Michael Kennedy's Mastodon