Python for Absolute Beginners Transcripts
Chapter: Using external Python packages
Lecture: Demo: Creating a virtual env

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The first thing that we have to do to get started with these external packages is to set up a place where can install them and manage them.
0:08 Now, this is really easy, but it's not entirely obvious. So, let me just tell you a little bit of background
0:14 and give you an example about why we're doing this before we go down this path, when you're working with these external libraries, they have versions.
0:21 And these versions change over time ideally as they go forward in time they're always able to run older code that was written
0:28 on them, but this is not always the case. Sometimes, a version one of the library won't run stuff that was written against version .5.
0:35 If you're working on two projects on your computer and let's say they both use the Flask web framework one has to you 1.1, another has to use 0.2
0:44 and they're not compatible, how are you going to install the same library with two versions? You can't and it turns out that you also want
0:52 to maybe know what are all the packages I'm using for this project by itself, not that I've happened to install for other projects, as well.
1:00 So, because of that, Python has this thing called Virtual Environments, these are like little isolated copies of Python that let you install
1:06 and manage it separately, so it's isolated. Now, the way we do this is pretty easy but let me put you at a quick article here
1:15 this one at, this is Brett Cannon he's one of the core developers on Python and he talks about a
1:21 Quick-and-Dirty Guide on How to Install Packages for Python talks about these virtual environments
1:25 gives you some examples, so you might want to check that out for more reading, but let's just get started. I want to go to this folder here.
1:31 This is our folder 10 for our Get Help Repository. I got this cool little plug-in or extension for our finder here called Go to Shell
1:39 I think Windows has something like Go to Command Prompt or Power Tool or something like that I'm not sure what the best way
1:46 on Windows is to do the same thing but you can always just CD over there. So, in here where our files and what not are
1:52 we're going to run a command that will tell Python to make a little copy of itself. And it has this library called pip
1:59 pip is how we install and manage these external systems and external libraries, but one of the things you can do
2:05 is say, pip list, and here's a whole bunch of stuff that somehow got installed into my computer but I don't want those, I want it separate
2:12 little tiny isolated one, so what I'm going to do is I'm going to say, Python3 -m for run a module. venv the name of the module is virtual environment.
2:23 venv then I'm going to give it a folder which I also by convention call venv. That's a little bit annoying
2:31 that it kind of looks like that or unclear I don't know but it's fine you'll get used to it. So that's going to take a moment.
2:38 And then if you take a look here there's a folder called venv and if we look in there there's a bin, and if we look in bin, you can see
2:48 there's hey a little Python and then there's a little pip and so on. But what we care about is activate
2:54 activate right there. So on Windows this is scripts not bin but otherwise it's basically the same. To make this active what we have to do in the Shell
3:04 is we have to say dot space or you can say source space. One of those, either those are fine on macOS and on Linux on windows just don't put anything.
3:15 I'll show you the Windows command in a second. We say venv/bin/activate. Now see my prompt here? It gets this in here now if I say pip list
3:25 it's just those two. And it's kind of annoying but it always installs a version that it came with not the lastest version.
3:31 So you usually want to run that little command now if we ask for pip list then we get that. Cool. So on macOS and Linux you say this and on Windows
3:40 you say, you get the dot, you just say venv\script\activate which is activate dot that. And that will have the same effect.
3:49 So now we've got this little isolated version of Python and we can install things in here. So right now as you saw
3:55 we have our pip list. Just as these two. But we're going to use the colorama library. We can install that PyCharm or we can install that here.
4:04 I'll do one library here and one library in PyCharm. So we can say pip install colorama. The library that lets us add color output.
4:12 And now if we ask for pip list. You can see colorama is installed as well. Okay great. So now we're ready to get going with this environment
4:21 and this program so I'm going to put that actually it's already ready over here let's load this up in PyCharm.
4:28 Now one thing that can happen over here is the project interpreter it might not have detected this. Sometimes it does sometimes it doesn't.
4:37 So you can see whether or not you can choose an exisiting one and browse over to it and so on. But you want to go and click add if it doesn't already
4:45 find it because it selected it, it wasn't in the list. So here you can see colorama, pip, and, so on.
4:49 You can even add stuff through PyCharm here, I can add request with a common library, or I can click the install or if there's an update I can hit like
4:59 here this one has an update, okay hit that little up that and we give it a second. Move over to this side. Didn't mean to click that up sorry.
5:06 Right here it's doing something now it was updated. Cool. So that's a nice visual way to manage that stuff as well.
5:11 One thing you want to do with these virtual environments is make sure they're ignored. See how they're like grayed out? Whatever.
5:17 Okay great. Alright so we're now ready to use this library we can come down here and we can say import colorama and because we pip installed it into
5:29 our virtual environment, the virtual environment you can see down here, is active right there. That means that we can use this library
5:36 and PyCharm won't give us an error it knows about it and we can start programming against it.

Talk Python's Mastodon Michael Kennedy's Mastodon