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