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