Mastering PyCharm Transcripts
Chapter: Packages
Lecture: Opening existing packages

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's begin our exploration of packages by working with an existing package,
0:04 and what one is more popular than Requests.
0:06 So let's go and just download Requests, git clone it rather
0:12 and we'll work with that, so do it this way,
0:16 go to our desktop, git clone this, there you have it, nice and quick,
0:23 and let's go open this in PyCharm,
0:25 so we'll go over here into Requests and before we open it,
0:27 let's create a virtual environment,
0:29 and we don't want to mess with the Requests for the entire system,
0:32 maybe just a little isolated thing.
0:34 So let's go over here and create a virtual environment like this
0:40 and we'll keep that open, we need that again in a second.
0:43 So we can open our Requests, remember file open directory on other OSs,
0:47 on MacOS you can do it this way which is sweet.
0:50 Down here, we have the various package management files
0:54 and this is the actual implementation of Requests.
0:59 First of all, let's look at the setup file here
1:02 and notice there are a lot of requirements that we have to install
1:05 in order to run Requests, especially if we want to do tests on it and things like that,
1:11 so let's go ahead and just click here for the moment, and let that go.
1:20 Now, this actually can take a while
1:22 especially for PyTest http bin so I'm just going to zoom ahead for you.
1:26 It looks like all the dependencies were set up, so that's great
1:30 now let's just tweak this version a little tiny bit,
1:34 let's just come over here and we'll say,
1:38 do a print statement, right at the beginning,
1:40 you would never do this but it will make it entirely obvious
1:42 like this is our custom version.
1:45 The idea being we want to test out this package
1:48 maybe we want to make some changes
1:51 try in another application that we have and then once that's working,
1:56 we could maybe do a PR back to the Request guys and whatnot,
2:00 so we could also put a variable here, our_var = 42, things like that.
2:06 Now, in order for us to use this outside of this folder
2:13 in our separate little application in theory
2:17 would be we need to register Requests with Python,
2:22 so for example, let's go over here, let's activate our virtual environment
2:28 and let's go up here and run Python and import Requests.
2:35 Notice, we did not see our little message here
2:40 let's see if it has our_var here,
2:44 it didn't look like it, right, you just have that stuff right there,
2:48 our_var, no, it has no our_var.
2:52 This is the Requests package from the system
2:56 what we want to do is we want to build a work in Python
3:00 and in our little Python app, this is just the REPL,
3:04 but just as well it could have been I ran my app right here in this environment.
3:08 So what I need to do to make this one the one that I'm working on
3:13 but for this virtual environment is
3:15 I need to either install it or set it to development mode.
3:18 So the way you would do that is you would go into Requests
3:22 and you would say Python setup.py
3:24 develop is probably what you want,
3:26 when you're working on it or install, if for some reason you want to install it
3:30 and we could come out to the shell and type that,
3:32 or we could go to PyCharm and come down here and click on tools
3:36 and notice run setup task.
3:40 Now, I found that sometimes you have to have the directory
3:43 that contains the setup files selected, not always but sometimes
3:47 so it seems like it didn't matter here
3:50 so let's try run this, now look what we get
3:52 we can have install, we could have develop,
3:56 all the cool features that come out of setup tools are right here
4:00 and what we want is develop,
4:02 we hit this and it actually creates a local symlink, a local install for this one
4:10 and it would have installed the dependencies if I had not clicked that button previously.
4:14 All right, let's try this again,
4:17 let me get out of Request because if I do it in this folder
4:19 it's going to find that one.
4:21 So now let's do it again, if I run Python and I say import Requests,
4:25 boom, this is our custom version
4:28 and I'll say something like print requests.our_var, it's 42, of course it is;
4:34 and if we had a different one, if we say Python 3 import requests,
4:41 this is not the same virtual environment,
4:44 it doesn't have that right, it doesn't have this variable.
4:48 So this is really cool, this setup lets us
4:52 register this package that we got off of github,
4:55 that could have been something we're working on for ourselves,
4:58 but this existing package, it lets us register it for consumption
5:02 in this development mode, which is really, really nice.