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:00 You saw how to take an existing package and get going with it, add features and play with it and test it and so on.
0:07 Let's now go and create a new package and notice over here in our course demos
0:13 we've got this package folder around create something called 'calc' you can imagine this might be
0:20 a silly little calculator package that we could import and do basic math with something like that. The way these packages usually get structured,
0:28 the willow look back, for example, at requests. The folder really this GitHub thing is called request but in there
0:35 there's the package name and the package has a dunder init_py in Python To make something a package technically all you need is a folder and a Dunder
0:45 init.py. So for example, I could say 'calc' and then here is used in Mac the creative files to just
0:52 do this touch. There doesn't have to be anything in this file. It's zero bytes. It's an empty. If we have a folder that contains this file to Python,
1:03 that means a package but it doesn't mean a package in the PyPI pip, install sense of things. We're going to need more details.
1:10 We need also an implementation. So what's pretty common is to have the source older
1:14 the top level folder for the package and then the package itself to be a subdirectory with the same name in there.
1:21 Let's go and open this in PyCharm and turn it into a full blown package Let's go and add a new virtual environment.
1:34 We're going to want one to install our package and possibly its dependencies into over here We've got our Dunder init. It's red. We open it up,
1:43 you can see that it's empty. I'm gonna command 'Alt+A' and mac os that adds it to turn it green so it doesn't look broken or anything like that.
1:52 So we've got this calculator package and let's just give it a couple of functions. Sometimes people put the implementation directly in this Dunder,
2:00 init_py others as we saw with the request there might be like an API folder and then they use an import statement to add it in.
2:08 So I'm just gonna do this simple thing right now and put a couple of functions and you know what the implementation is going to be nice and easy.
2:18 So we're gonna 'return x + y'. Let's have another one called subtract like this. So this is what our calculator is going to do.
2:30 You're going to say import calc, then count.add and count.subtract and get your numbers back. But in order for us to use this again,
2:37 it has to be a proper Python package, Not just in the local sense, but we want to share it. It needs to have more information.
2:43 So check this out. Remember we saw the tools run setup py don't have a run setup.py Why we don't have a setup py but check this out,
2:52 create a setup.py So this will help you get everything set up just like you're supposed to. So let's type a little info in here.
3:01 Alright. It fills it out. We hit go and look what we've got.
3:03 We've got all the little details that we need right here before I apparently misspelled calculator We know that the name of the packages,
3:12 calc the version is this. If we had sub packages like if I had a folder in here that was for advanced math, we could have, you know,
3:22 'calc.advanced' and so on. So there might be more than one of those here That's why that's a list I made up some fake URL for mit
3:31 michael kennedy in my email and the best calculator ever. Don't you think? So it looks like things are pretty good to go.
3:39 We could do things like pull the version. We could also go and set other values like the description. We could set the long description,
3:47 we could set the long description format to say mark down but this is a good starting place and this is really all we need.
3:54 So if we want to test this app we could go and write unit tests or we could go and create a folder directory called 'bin' maybe and then I'll write a
4:03 little test app. What I want to do is actually go to my test app and import this package. The safest best way to do that is just like we
4:13 saw before now run a set up, develop experience here, make sure everything gets set up.
4:21 So now Python button this is installed as part of this and we can just go down here and say import there it is and check it out.
4:30 And let's just get some numbers here. We'll say we're just going to have no error checking and just assume this is an
4:37 integer. Who knows if that's a good idea? Probably not in general, but we're gonna cast it over to an integer and then
4:44 we're going to just print out a couple of things. We're adding the calculator using the 'calculator.ad'. And then we're gonna do the subtract.
4:59 Here's our little app. You want to see if the package that we've created and installed by running Python setup py develop or we could do the pip,
5:07 install command as well. Let's run it and see what we got. First number is 7, 200. The sum is 207. The difference is negative 193.
5:18 1 93. Done how cool is that? So we've created this package and let's go see what else we can do with it
5:25 Let's try this Python. See if we can create a wheel and what do we get up here? Let's see we've got a Dist folder and then there's our
5:41 wheel. This is what we could do 'wnl' or some tool like that to upload to PyPI. You can bet there's already a 'calc' available on PyPI in some
5:50 way or another. But we've got our dist and we've got our build here as well. We're building stuff. We'd want to ignore this folder probably and not put
5:58 it in source control. I'm guessing that's up to you. But because we have this setup py that was created, we can now do, first of all,
6:05 we can work with it properly in our app and be sure that it's installed into
6:09 Python locally. But then we can do things like this where we're actually using the tooling to build distributable versions of our package.
6:18 But then on PyPI we could put that on an internal Py Dev server bunch of options where to go from here.

Talk Python's Mastodon Michael Kennedy's Mastodon