Welcome back, everyone, to this lecture on modules and packages when creating complex code bases,
we typically don't just use a single dot pie file instead.
We would like to break up functionality across multiple python files and import our classes or functions
as needed.
Let's explore how to easily do this within Python.
I'm going to jump back to our code editor.
OK, so here I am inside a visual studio code, and I've expanded the explorer because I'm first going
to start off by creating two new files one I'm going to call.
My module, Dot Pi.
And let's do two things inside my module.
I'll define a function that let's say is super useful, like the use or, let's say, useful.
Function and all this does is this kind of really silly function, just says print using the useful
underscore funk.
All right.
So I have that available for me.
It's a function within my module, that pie.
I will also later on in this file, define a class.
We'll call it useful class.
And let's just say it has a very simple method.
You just instantiate it, let's say in it, and you instantiate it with some message you want to relay
later.
So say salt, that message is equal to message, and then it's going to have a method called, let's
say, report.
Takes and self and report just prints out the message itself, says self that.
Message.
OK.
So notice two things I have here, I have a function that is really useful to me and I want to use it.
It's in my module that PI, as well as a class with a method I can call.
So now we want to be able to use both this function and this class, as well as the methods associated
with that class in another file.
How do we do that?
I'm going to create a new file and now let's call it my program Dot Pi.
And so my program that PI wants to be able to use functionality from my module that PI notice right
now that they're actually located within the same folder called Django Lectures.
Later on, we're going to show you how to create essentially a package directory that you can then use
that an import from other locations.
But what I can do for right now, because they're located in the same directory that is, they're both
located under Django lectures.
I can say the following I can say from.
My module.
Import and then I can actually import the function useful function.
So I say import useful func.
And then here, later on in my code, I can actually then execute useful funk.
So now look what I'm going to do down here at the terminal.
I'm going to say.
Python and I'm going to run my program, the PI, I hit enter and it says using the useful function.
So now I understand that I can actually call functions here in my module, that pi from my program,
that pi.
Note that when you start doing imports, you may actually see Python automatically cash some of this
functionality that way when you run this again in the future.
It should, technically speaking, run faster for our level of understanding of Python, though it can
kind of just ignore it.
Don't delete it, but just go ahead and let it live there.
Now we can also use classes, so instead of importing use func, what I could also do is say from my
module.
Imports and I can import the useful class, and the way this would work is I create my instance of a
class so I can say my instance.
Is equal to an instance, a useful class.
Remember, it needs a message to be defined, so I'll just have this be.
Hello.
And then I'm going to say my instance and then I have a excu report.
Save those changes.
Run that and then it says hello.
So that's how you can actually go from a file that's located in the same directory, like my module,
that pie and then say from my module that is the name of that pie file import.
And you can either import functions or classes or both.
If you want to do multiple imports, you could do something like this comma and then say useful func
and also use that.
So then I could over here actually execute useful func and you can see that works pretty much the exact
same way.
Hello.
And then using that useful func.
Now let's imagine I have another folder.
How would I actually use that?
Well, what I can do is I'm going to create a new folder and we're going to call it my.
Package note that I'm creating another folder, I'm not actually creating another Python file yet wanting
to do in order to use my package.
And let Python know that this directory name, which is technically not a Python file, can also be
included as part of the pathway, is two things inside of my package.
I need to create an empty file called Underscore Underscore.
I end it underscore underscore that PI.
This is going to let Python know that it's OK to use this folder name as part of its directory structure.
When you're importing a file, it's actually labeling everything inside this folder as part of a package.
And now let's actually create a new file here that will be called, let's say, my sub module Dot Pi
and inside my sub module that PI, I will go ahead and create another kind of silly function here.
So we'll say.
My sub funk and all this does is it prints out using a function from.
My sub module, that pie.
OK, so notice the directory structure here, I have my package in order for Python to understand that
this is now a package director I can use, I now have in it and then my sub module, so I come back
to my program.
And then what I can do because it hasn't in at that pie.
I can now say from my package, dot my sub module, import some functionality from that particular file.
So I come here and now that name is my sub func.
So I come here and say, Import my sub func.
So notice the syntax here.
I now say from that director name dot the the actual Python file name import.
Then the class and the function.
Previously, we just did from the module.
Import this now because I have this in it, that pie file.
That's to underscore.
Remember that I can begin to use the directory names themselves.
So here then I can call my sub function.
Have it run an execute.
I'm going to run Python, my program, the pie again.
And now it says, here, hey, I'm using a function for my sub module.
That pie?
OK.
And that's the very basics of what we need to know for modules, imports and packages.
There's technically a lot more that goes into it than what you've seen here.
But just to get us started with Django, this is more than enough later on as we do various other types
of imports.
We'll let you know as we're doing them the specific context and syntax for that.
But this is what you need to know so far.
If the two Python files are in the same directory, you can easily just import them by saying from the
module that pie import whatever classes or functions you're looking for.
If you want them organized, then their directory, which you can begin to do, is simply open up that
folder.
Add in in in it that pie that is empty and then add in the modules like myself module and then say from
that directory that and then the module.
So that's a difference in a package and a module.
The package is that actual directory, which should then include different types of modules, was themselves
are just Python files.
OK, that's it.
I'll see in the next lecture.