Effective PyCharm (2021 edition) Transcripts
Chapter: Packages
Lecture: Opening existing packages

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In order to begin exploring PyCharm's package features.
0:03 Let's start by working with an existing package.
0:06 So over here on the GitHub PSF,
0:08 Git Hub organization, we have requests,
0:10 one of the most popular python packages you've probably heard of it.
0:14 Use it to go and make http requests instead of a browser using python.
0:19 So let's go and actually get that installed on downloaded and set up on our system
0:24 So we could theoretically work on it.
0:26 So first of all, let's go to the desktop and we'll just get clone this
0:29 Then we could open this way through PyCharm by open from source control but
0:35 just kinda start this way, show a little more grassroots way.
0:39 So I want to open up this,
0:40 remember drag and drop on the PyCharm Mac os others open directory and browse to it.
0:45 Here it is this is request and this is like the homepage that you
0:50 would see for if you just went to the GitHub Repo,
0:53 right, This is the Read me over here is the code.
0:56 We don't need this. We go to request.
0:58 Here's the actual code like here's where you have requests,
1:02 general method and then the various specializations down here.
1:06 Like give me the head, the post, that put, the patch, and so on.
1:09 The first thing we need to do is make sure that we have a working PyCharm
1:12 interpreter. We don't So I'm gonna create a new one here.
1:18 Then PyCharm says there's some requirements for this package.
1:22 Many packages themselves have requirements and when you pip install the package,
1:26 you get the transitive closure of those dependencies and their dependencies and so on.
1:30 So let's go over here and click install these.
1:33 It turns out that PyCharm actually misses one for this package but it doesn't really
1:37 matter because we're going to need to do some other stuff to work with it anyway
1:40 so this can take a little moment.
1:42 I'm gonna let it run and then we'll go from there.
1:45 Success we've got it all installed now in order for us to work with us and
1:49 have python believe that this package,
1:52 this source code here. This is actually what we're supposed to work with.
1:56 Not some other package that might be installed in some other way or to have this
2:00 one set up. What we need to do.
2:01 We need to go down here.
2:03 There's a couple ways we can do it.
2:04 One of them is we can say "python setup.py develop" that will basically install requests
2:10 in place rather than copying it over to site packages which means as you edit these
2:15 files will immediately pick up the changes which is exactly what you want while you're working
2:19 on the package now, how do I know to do this?
2:22 Maybe I don't PyCharm has some cool tricks or helpers up its sleeve here,
2:26 go to tools here, notice there's a bunch of things but one of them is
2:31 run a setup py task and check this out.
2:34 We can do things like we could create a binary distribution like a wheel.
2:39 If we wanted to push this to PyPI we could install it,
2:42 we could register it, we could do all sorts of things but what we want
2:46 is develop, install package in development mode.
2:48 And we can also just type development.
2:50 It asks us if there are any options,
2:53 there are none. So we could go notice we're actually installing a couple other dependencies
2:58 that for some reason are not listed in the setup py but are actually needed.
3:02 There we go. We've got it all set up and installed and notice there's this
3:06 "request.egg-info". This is what python uses to know about installed packages.
3:12 And so it's gone right into the root directory so we can come down here into
3:16 our little console and we can say import requests and which requested.
3:21 We just get we got this one right here.
3:24 Let's see how we might know that.
3:25 So we come over and do a couple of things just to show you that this
3:30 is the version we're working with.
3:31 Let's just say print right at the top.
3:33 You would never ever do this but we can make a print statement run during import
3:37 So we could say running custom version of requests.
3:43 Yeah. Now if we go down here and we make sure we have a fresh
3:48 version we import request notice it says running custom version of requests.
3:54 This is the one that we just made the changes to and you can see that
3:57 they're getting added immediately. Well that's pretty fun.
4:01 But let's go to the API down here and add something a little more
4:04 fun. Let's say we're going to add a grab it method here.
4:11 Real simple way to come over here and say I want to grab a URL
4:15 data and print it out and return it if you want to return to
4:18 his text. And what we're gonna do is we're gonna say response equals uh just
4:23 get on a call to get this at the top of the function passed through the
4:27 URL, make sure it worked raised for status,
4:30 turn 'response.text'. I think of this as like a simplified way to just
4:35 call it, grab text from the URL.
4:39 Okay. And let's go to the top up here,
4:43 make sure that this is getting imported.
4:47 We don't make our grab it text export here,
4:50 then we won't be able to use it as part of the package.
4:53 All right now, we should be able to go and write some python code.
4:57 That's gonna do this. Let's go over here to our desktop and in the request
5:02 Remember on windows you don't do the dot And this is scripts not been why
5:11 these have to be different. I don't know,
5:14 I understand why the dot not why the bin is different.
5:16 Okay, so let's go over here and we can say import requests.
5:18 We gotta run python, we say import requests.
5:22 There's our custom version of requests.
5:25 Super cool. And let's say some call it html is going to be requests.grab_text
5:32 Go get it from 'talkpython.fm'.
5:40 Oh, it's taken a minute.
5:41 That's a good sign. What is our text Huge.
5:45 That's too much. Let's do a little just the 1st 250 characters.
5:50 What do we get not enough to see the title?
5:53 Let's do 500. Maybe we need more.
5:56 But you can see we're getting the 'talk python to me' podcast details right off the
6:01 website. How cool is that?
6:02 So we're able to go get a package off source control,
6:06 put it onto a machine, put it into development mode,
6:11 add a feature to it, granted not a super important feature.
6:14 Right? You can put these two things together to for yourself,
6:18 grab text, give it to URL.
6:20 Boom, Here comes the text back or an exception.
6:22 If there's an error, we did this by creating a folder where the package lived
6:27 then we created virtual environment and activated.
6:31 Then we came over and we did tools run setup py task and then we're good
6:37 to go, we just make changes.
6:38 Tried, tried working with the code Probably what you would do is one of two
6:42 things and practice as you would create a little example app that would consume it and
6:47 then exercise it if you want to be fairly dynamic or like they have over here
6:51 you would create some tests that you could run.