Tutorial about the LEGB rule and scope: https://realpython.com/python-scope-legb-rule/#understanding-scope
More information about packages: https://realpython.com/python-modules-packages/
Tutorial about the LEGB rule and scope: https://realpython.com/python-scope-legb-rule/#understanding-scope
More information about packages: https://realpython.com/python-modules-packages/
00:00 In this lesson, you are going to create a package. The first thing to do is let’s have a look at the structure of our file. And here you see we have a number of modules that we would like to stick in our package.
00:13
The first thing to do is to add a folder. So click on add folder and give it the name my_package
.
00:24 And the second thing then to do is to move your modules into that package so into that folder. So you click on the first one, hold down Shift, click on the last one.
00:34
You can see they’re now all selected. And then you just drag them into that folder. You will get a warning: Do you want to do this? Yes, I do. And now you can see they are in my_package
in that folder because I can close the folder, open the folder, and then they show up again.
00:52
The third thing you have to do is you have to create within my_package
in that folder so click on that. You have to create a new file.
01:02
So go new file. But that file has to have a specific name and that name is __init__
.py
.
01:14
You can see this file sits in the my_package
folder. So what’s going to happen every time your package is imported, whatever code is in this __init__.py
file will be executed.
01:26
There doesn’t have to be anything in there, but just to show you how that works, let’s just put a print()
function in there that says “initiated”.
01:35 So what we expect to happen is that every time the package is imported, the word “initiated” will be printed in the terminal or in the REPL depending on where the package is imported.
01:48
So, let’s give that a go. If you move to your terminal, and you have to make sure that you are in the directory, the folder where your package is saved. And remember we made that a subdirectory of what in my case is called underscores
.
02:04 So I have to be in that folder, and from there you open up the REPL
02:11
and let’s import my_package
.
02:15
And the word “initiated” shows up. So that is because we had, let’s quickly show you that again because in this __init__.py
file we had this print()
function here, this one line of code.
02:27
So that is executed because in the REPL we imported my_package
. Okay, so what does that do for me? So well, let’s have a look. Let’s use dir()
.
02:39
So what can we find in the namespace where you can see my_package
.
02:44
Excellent, success. We have imported our package. So let’s have a look at what’s actually in my_package
. Well, I know what’s in my_package
, there’s a whole bunch of modules in there.
02:54 Let’s have a look. I do need to spell that correctly though.
02:58
And I see nothing. There are no modules in my namespace. So I have imported my_package
, but none of the modules. That is indeed correct. If you want this import statement to work in the way you expected, as in you expected to import all the modules in my_package
, then you need to add some extra code to that __init__.py
file.
03:23
But that is a little bit beyond the scope of this lesson, but I will include a link to a tutorial that explains how to do that. What you can do though is you can use a different import statement and you can use from
, so from my_package
03:41
import shapes
. shapes
is one of the, before I hit Enter, I’ll show you again. So shapes
is one of the modules that we have put under the my_package
subfolder so we are hopeful that that works.
03:57
So in the terminal, I’ll now click Enter and no error message. Awesome. So let’s try dir()
again. What do we have in the namespace? Well, my_package
is still there from before, as you remember, there was nothing in there, but now we have shapes
as well.
04:12
That’s great. So let’s have a look and see what is in shapes
. There you go. In shapes
, you have circle
and square
and _PI
.
04:21
Oh, and there’s the pi
here as well. Where does that come from? Well, let’s have a look again, at shapes
we have _PI
, we have pi
at the top, and then we have circle
and we have square
.
04:34
And we also have a function here _validate
,
04:40
and indeed _validate
is here as well. So what that means is you now can use everything that’s in the namespace. So I could, for example, instantiate a circle
using the Circle
class.
04:54
And let’s give that a radius of three. Oh, hang on. Why is this not working? I thought Circle
was in the namespace. Yes, it is. Circle
is here, but it is in the shapes
namespace.
05:06
So that means that you have to go through shapes
or you use the shapes.
notation, let’s use that then. shapes.circle(
3)
, right?
05:18
So that’s a circle with a radius of three. And how is that going to help me? Well, let’s have a look at what we could do with circle
. So let’s do the dir()
of shapes.circle
.
05:32
And what is available to us where there’s all these dunder methods here, but at the very end you’ll find calculate_area()
. Remember we had a method in the Circle
class that calculates the area of the circle.
05:46
In our code we have shapes Circle
, and then we have our method calculate_area()
.
05:55
Let’s try it out. I’ve instantiated the circle here as c
, so I can use c.calculate_area()
, don’t forget the brackets. And we have 28.26 as we have seen in previous examples.
06:14
So our package works. We have successfully imported my_package
. Well, we have successfully imported the shapes
module from my_
package
to then calculate the area of a circle.
06:27 Now there is much more to know about packages, but I will provide a link to a good tutorial. I will see you in the next lesson for some more exciting stuff.
Become a Member to join the conversation.