Mastering PyCharm Transcripts
Chapter: Packages
Lecture: Creating new packages

Login or purchase this course to watch this video and the rest of the course contents.
0:02 We've seen how PyCharm helps us work with existing packages
0:05 when we played with Requests.
0:07 But what about creating new packages?
0:10 Now, over here I've created this folder for Requests
0:13 I didn't put it in the source control
0:15 because all I did was clone the get repository
0:18 but this one we want to keep,
0:20 so notice I have a package folder in a lower down environment here
0:25 and I guess we could even go and say we don't want to look at it,
0:29 but one problem that you run into is the tooling I'm going to try to show you—
0:34 doesn't work because PyCharm discovers that this is a package
0:39 and it's like, well I don't need to do the create package stuff
0:42 because hey, you already have a package
0:44 even though I really want to create another package here.
0:47 So in order to make this work,
0:49 what we're going to do is open that directory by itself,
0:51 here we have it, put it on PyCharm and off it goes.
0:55 Okay, so let's create an application
0:57 that's going to use a package we're going to create
0:59 and let's create the package itself,
1:01 so what actually defines a Python package in its true essence?
1:05 Not very much to be honest, it is a directory that has __init__
1:09 so let's say we're going to have the package
1:12 so what do we get, we got a directory with a in it and it's empty.
1:18 We can put our implementation here
1:20 or we can put it separate, you take your pick
1:23 so we could define something external and import it here
1:30 and the stuff that we imported would be in the package
1:33 or we could just define it here
1:35 for this simple thing I'm just going to define it in the __init__
1:37 and what we're going to have is also itself quite simple,
1:40 we're going to have a calculator, a calculator class
1:43 and the calculator class is going to have a couple of features,
1:46 have the ability to add and turn X plus Y
1:52 and let's also have a subtract.
2:02 Now we actually wanted to have this class so we can play with it
2:05 but PyCharm is saying this could be static
2:07 and let's just say you know what—
2:10 for this one I don't care if these could be static,
2:12 I want them to be instances just for the heck of it.
2:15 Okay, so this is great, we have this package
2:17 but in practice, it's not enough
2:20 we need to install the package, how do you install the package?
2:22 You have a, so let's go and create that now
2:27 so we have this selected, we can go tools
2:29 and actually say not run but create a
2:33 so this package is going to be called the_package
2:38 and it's going to be version 0.0.1, something like that,
2:43 url is going to be http://the_package
2:46 and the license will be MIT
2:48 I'll put me in here and my email address.
2:55 Description, amazing package,
2:58 all right so if you've done a bunch of stuff
3:01 you maybe recognize these pieces and where they go
3:04 but when I click ok, what I get is a
3:08 now, one thing that's annoying probably at some point this will go away
3:11 PyCharm will notice, this .env is actually not a source of packages
3:17 it technically contains many Python packages
3:20 but as you can see these are all just crappy built in ones,
3:24 so let's clean this up,
3:26 the idea is PyCharm is trying to find all the sub packages and list them
3:29 so we could have like the package and then down here
3:33 we could have a new package advanced or whatever
3:39 and then down here, we might say we have the_package.advanced
3:46 It was trying to help us with that, but obviously it was a little overzealous
3:53 Okay great so this is super cool, right
3:54 it looks like we should be able to run this,
3:56 let's go down to the Python console here,
3:58 I think the fact that it's added this
4:02 is probably going to not let us have this fail, I'd like to have this fail.
4:06 But I think it's going to work, look at that
4:10 we could say c = the_package.calculator, like that,
4:16 c.add (7, 11)
4:20 18, our package works, it's amazing.
4:23 Okay, so let's go and see how we can work with this
4:27 let's go create one more app, this app is going to consume this as well,
4:31 I'll call this the_app, all right,
4:34 and Python file called package_app
4:41 here we'll say import the_package
4:45 and we'll just do a little print using the package
4:54 we'll do the same thing
5:02 all right, it looks like it may be working let's try to run it and see what happens,
5:06 I think it probably will work— yeey, it works.
5:08 However, that's only because PyCharm has altered the path to include this
5:12 so let's go over here and let's copy this path, and do it this way
5:20 so let's cd over here
5:22 actually, go back really quick and activate our virtual environment
5:27 and then we can go to the_app
5:30 alright, here we are, if we just say Python package,
5:35 no module or package, why—
5:36 because we haven't run the, let's go back,
5:41 there, now we have the maybe that belongs in there, I don't know,
5:49 I haven't really organized this as well as I should
5:51 but just to give you guys the sense what we're working with here,
5:53 if we run and we say develop
5:56 it did work pretty well, let's go over to setup
6:00 okay great they're using setup tools
6:03 the old version of PyCharm used to use distutils,
6:06 which didn't have the develop version.
6:11 Okay, great so this worked very well
6:13 now if we go back to our, the_app here
6:20 remember we ran this before and it failed
6:23 what about now— of course it works
6:25 because we installed the package that we just created in PyCharm.
6:29 So what did we do— again, you would never jam this together like this
6:34 but we have our package we created, we gave it the __init__
6:37 and we even have our sub package, if we really cared about that,
6:41 and then we created the
6:44 by using the details, basically the tools here
6:47 now it goes to run,
6:53 and then we ran it, right here, like so,
7:00 and that installed it into our virtual environment,
7:02 and then we can of course run our app, using the package.