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