Jupyter Notebook: Set & Get Environment Variables
519 views
Dec 11, 2024
š Subscribe for more: https://bit.ly/3lLybeP š Follow along: https://analyzingalpha.com/jupyter-notebook-environment-variables-tutorial #python #jupyternotebook 0:00 Introduction 0:06 What are Environment Variables? 0:30 How to Read Environment Variables 0:50 printenv Command (*nix) 1:00 set Command (Windows) 1:06 %env Magic Command (Jupyter Notebook) 1:15 Setting Environment Variables 1:30 Set Environment Variables Using os.environ 1:54 Set Environment Variables Using %env 2:24 Setting Environment Variables Using .env Files 2:59 Installing python-dotenv 3:25 Creating a .env File 4:30 Loading a .env Files with python-dotenv 4:40 Environment Variable Tips
View Video Transcript
0:00
hello everyone today you're going to
0:01
learn everything that you need to know
0:03
when working with environment variables
0:04
within Jupiter notebook let's get
0:06
started the first thing to know is that
0:08
environment variables aren't scary
0:10
they're just key value pairs so for
0:11
instance the key is username and the
0:13
value is Leo Smiggle for this instance
0:15
and you pass these key value pairs into
0:18
applications or your operating system to
0:21
change the behavior at runtime so for
0:22
instance you might want to pass in an
0:24
API key for some various API
0:27
subscription service that you're using
0:29
but the first thing we want to know is
0:31
let's how to read our environment
0:33
variables to make sure that we're
0:34
setting them properly and there's really
0:36
two ways to well there's three ways here
0:38
but a real easy way to do it is to print
0:40
end in the unix-like machines set in
0:44
Windows machines or if you're writing
0:46
Jupiter notebook you can just use the
0:47
percent M magic command so let's take a
0:50
look at these examples so you'll see
0:52
here that I've typed print M but my
0:54
terminal on my bash shell and I get all
0:57
of my environment variables simple
0:59
enough you can do the same thing with
1:01
set and the command prompt in Windows
1:04
and finally I opened up Google collab
1:06
type percent M and printed out all of
1:10
the environment variables and the Google
1:12
collab notebook
1:14
now that we know how to read them the
1:16
next thing we want to do is learn how to
1:17
set them well we can either set them
1:19
directly or read from a file setting
1:21
them directly is just more temporary in
1:23
nature and obviously reading from a file
1:25
you know if you want to save an API key
1:27
or something like that that's how you
1:28
can do it and we'll cover that in a
1:30
second so first things first to set it
1:32
directly you can import OS and use OS
1:35
dot Environ and set the you know key
1:38
value pair into just to make note you'll
1:42
notice they're all what my keys are
1:44
always Capital with underscores that's a
1:46
convention because these are case
1:48
sensitive so you'll do want to make sure
1:50
that you're using the proper naming
1:52
conventions key value pair easy enough
1:54
the next thing and arguably the easier
1:56
way to do it is if you're actually
1:57
within Jupiter notebook you can use that
2:00
percent M magic that we discussed
2:03
earlier and just type in the key value
2:05
pair so here I've created one for my
2:07
magic VAR equals my value or you can use
2:10
the set M they essentially do the same
2:12
thing and now I type percent M and it
2:15
prints out both of these to match both
2:17
of these environment variables simple
2:20
enough now
2:21
what happens if we want to store them
2:23
right well the most conventional way is
2:26
to create a DOT EnV file you know we
2:29
just call them dot m files that we store
2:31
these key value pairs in so we don't
2:33
have to remember them or keep typing
2:34
them in and one thing to note is that
2:37
there's actually a package called
2:39
python.m that reads these and if you
2:42
want to save them to your GitHub or
2:45
wherever you can actually use something
2:46
like git so you can actually put
2:48
your API key in save these two you know
2:51
GitHub and encrypt them so you never
2:52
have to worry about losing your keys or
2:54
forgetting you know if you switch your
2:56
machine to have have them so anyways
2:58
installation we simply use pip to
3:01
install it into our virtual environment
3:02
I do have a video on Virtual
3:04
environments I'll link that uh you know
3:06
in the description below and provide a
3:08
card and if you ever want to pass an
3:10
operating system command directly from
3:13
within jupyter notebook you can just
3:15
prepend the bang or exclamation mark and
3:17
just type bang pip and then install
3:19
python.m then that will install this
3:21
package into your environment
3:23
now to now all we need to do is create
3:26
that.n file we want to create a dot m
3:28
file with our various key value pairs so
3:31
if you wanted an API key you just do API
3:33
underscore key and then give it the API
3:35
key and then you'd be able to read you
3:38
know whatever it is you want right so
3:39
there's no limit to how many key value
3:42
pairs uh you know reasonably that you
3:45
have obviously disk disk space and other
3:47
limitations uh are you know if you want
3:50
to call me on that one but you get the
3:51
point you can have as many as you want
3:52
essentially all right now once we have
3:55
our dot m file which is located in the
3:57
same directory as our jupyter notebook
3:59
we can load it so you'll see here we can
4:02
import
4:03
load.mp from dot m which is a package we
4:06
just installed and then we just call it
4:08
right and the key is we don't actually
4:10
have to provide it a file name because
4:12
it's conventional or standard to use the
4:14
dot m file as the name and then it'll
4:17
automatically load everything in and
4:18
you'll notice that we have this my other
4:20
variable which is the you know the
4:23
secondary variable we loaded in here
4:25
right because we already had my value
4:28
from the OS dot Environ so my other bar
4:30
you know it printed it out great we know
4:33
that that loaded successfully so that's
4:36
pretty much everything that you need to
4:37
know some tips again make sure you
4:40
understand that it's you know these are
4:41
case sensitive it's best practice to and
4:44
conventional to use all uppercase and
4:46
underscore with descriptive names and if
4:49
you want to save your dot m files uh go
4:51
ahead and use something like get Crypt
4:53
and then you can keep all of your end
4:55
files in GitHub or gitlab or wherever
4:58
you're storing these so hopefully you
4:59
like this video if you did please let me
5:02
know if you like this different version
5:03
it was more of a presentation version
5:05
versus me just typing through jupyter
5:06
notebook if you actually like to watch
5:08
me type in jupyter Notebook instead of
5:11
doing this presentation format let me
5:12
know I'm trying to get a feel for uh you
5:15
know what I'm going to be doing next and
5:17
how you want to see it and have it
5:19
presented because there's going to be a
5:20
lot of new content coming soon so have a
5:23
wonderful day thanks for joining me and
5:24
I'll see you in the next one
#Computer Science
#Educational Software
#Other