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


Talk Python's Mastodon Michael Kennedy's Mastodon