Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Virtual environments
0:00 Now we're going to use one of these libraries from PyPi, one of these external
0:03 libraries, and we're going to use pip to install it,
0:06 but that means we need to address one more concept.
0:10 If we install this library into our global operating system environment,
0:15 we could be setting ourselves up for big trouble.
0:17 What would that be? Well, imagine
0:19 application one depends on an older version of a library.
0:23 Over time, Somehow that's changed in a way that's incompatible to the program,
0:27 but you want to use a new application, build a new application, that uses the new version
0:32 of that library. So one app needs the old version of the API,
0:37 the newer app must have the new version of
0:40 the API. How could those both exist at the same time?
0:43 Well, if you just pip install your library,
0:46 it's not going to be possible.
0:48 You could install one and then uninstall it,
0:49 and so the other, and that's not how we should do things.
0:52 So let's open the terminal here,
0:54 and we're gonna go over and go to my desktop,
0:57 make a directory just called "test".
0:59 Now, remember if I "pip install requests",
1:02 whatever version it gets that's gonna be controlling what application libraries are available for every app.
1:08 We don't want that. So what we can instead do is we can create
1:11 what's called a virtual environment. So the way we do that
1:15 is we say "python -m venv".
1:18 So have Python run the module library within itself,
1:21 called "venv" for virtual environment,
1:24 and I'm gonna put it into a directory called "venv". Those air a
1:27 little bit weird, confusing, but it's a convention that's used all over the place,
1:31 so I don't wanna stray from it.
1:33 Now, we want to run Python 3.
1:36 There we go. So now if we look in here,
1:39 you can see that there's a bin folder and in the bin folder there's an activate
1:43 script. So right now if I ask which python, on Windows you would ask "where python",
1:47 it's the System one, okay?
1:50 But if I say "dot", this dot means apply to the current shell rather than run in
1:56 a new shell, I must say ". venv/bin/activate".
2:01 Now notice my prompt changes and I say "which python",
2:06 now it's this one, okay.
2:08 It's this current one. If I say "pip list" and ask what things are installed
2:13 for this python, nothing. If I run Python and I try to import requests,
2:17 it'll say "Nope, we only have Pip and setup tools as our external libraries,
2:23 nothing else". So if we get out of here, now
2:25 we can "pip install requests", installs locally along with its dependencies,
2:35 and here we have those and we can now run Python, import requests, and
2:39 we could even do things like "requests dot
2:42 get" to Google or wherever.
2:46 And if we could print out,
2:47 you could say the response is equal to that last thing that came out.
2:51 Here's the text that apparently is Google.
2:56 a mess, but nonetheless, we went and got that off the Internet by installing it.
2:59 So here's the deal, this application now has this isolated local virtual environment,
3:06 and when we upgrade or downgrade or install things,
3:10 it's just this list. So,
3:12 for example, I can type "deactivate" to get out of this virtual environment,
3:15 and if I type "pip list" now,
3:16 it just shows me all the random stuff in my global python.
3:21 We can use these virtual environments which we create by saying python, or
3:26 python3, depending how you run your python, -m venv
3:30 venv and it creates it and off we go.
3:33 So we want to do the same thing for this application.
3:36 So right now you can actually see what version of python we're on, 3.9 down here,
3:40 and if you click "interpreter settings",
3:42 it will show you that it's the global one.
3:45 This is, you know, user local,
3:46 this is global one. We don't want that.
3:49 So what we can do is either through over here we can click,
3:52 "add" or that's what you were actually after you would just go over here,
3:55 say "add interpreter" and then what we can do is we can pick a location.
3:59 So here we've got the base thing,
4:01 the thing we're going to run with the python
4:04 -m venv, and then we want to go to
4:07 our project directory. Convention is to go to the folder at the top level of your project
4:12 and there create a folder, the virtual environment, called "venv".
4:18 Now, PyCharm doesn't help us much this time,
4:21 but we just type the
4:23 "venv" on the end,
4:24 we hit ok, it's gonna create the virtual environment for us.
4:28 Then we come down to the terminal and notice once we open a fresh copy of it,
4:31 it has this venv
4:33 and if we ask "which python" on Windows you have to ask "where", notice it's the right
4:37 one. It's out of our virtual environment.
4:38 And if we ask, "what's installed", shows us that nothing is installed. Again,
4:43 we could come over here, we click this "interpreter settings" that would show us again
4:46 nothing is installed. We could actually install requests over here like this,
4:51 and we could just click "install package",
4:53 but I'll show you an even simpler and shorter way. If we go over here and
4:57 just try to use "import requests",
5:03 PyCharm is going to say there's a problem,
5:04 this is not going to run. And if we try to run it,
5:06 you can see "no module named requests".
5:08 But check this out. If we put the cursor on the squiggly bits and hit
5:11 "alt + Enter" it says "Do you want to install this package?"
5:15 Why, sure! Please do! notice at the bottom?
5:17 It just installed requests. And now,
5:20 if we run it again, hey,
5:22 look at that. It works! And we go back to our interpreter settings,
5:26 now requests is installed right there.
5:28 Cool. So that's how we can manage virtue environments from within PyCharm,
5:31 but you already saw that you could just use the terminal or the command prompt to
5:36 do them directly. What I found was in the early days,
5:39 I would do most of my stuff in PyCharm because it helped a lot, but as
5:42 I got better and better, I didn't.
5:43 There was just little things like I wish it worked slightly different,
5:46 so I'm gonna use my own way of doing that.
5:48 So now I don't use PyCharm very often to create the virtual environment,
5:51 but I did find it super helpful when I was getting started.