Okay. When we talk about the word 'package', and what I'm saying here
is a package,
it's a Python package.
And really, all a Python package is, is a collection of modules,
just a collection of Python files.
Really, that's all it is.
It might have some non Python files in there, but really,
you're just looking for the Python files.
So usually packages are put into their own folders, and
and they use many files that relate to each other.
So what I'm going to do, is I'm going to open up my Desktop
entirely, and create a new file in here.
So I'm on my Desktop.
I have files that we've seen before throughout the rest of this course.
I'm going to go ahead and create a new folder in here called 'MyPackage'.
Now, we need to tell Python that this is not just a regular
folder, and that this is actually supposed to be a package. So,
what we do is we create a new file in here.
So I'm going to create a new file called '__init__.py',
and this file is going to have absolutely nothing in it.
This simply just tells Python,
"Hey, you know, when you go to look into the MyPackage folder
and there's an '__init__.py' in there, just treat it as a package, please.
So once that's created, you don't have to do anything with it.
Now, I'm going to create a new file in here
called 'my_functions.py', and give it a name up here,
'my_functions.py', and by the way, this is not necessary.
I just like seeing that.
It also helps viewers see which file I'm in a little easier as well.
I know it's here and here, but sometimes that's not totally
apparent. I'm actually one of those people that even though
it is right there, sometimes my eyes just don't see it,
so I like to put it there as well.
And you never know what other text editors people are going to
be using when they download these files, so
anyways, that is my explanation of why I write that up
there. And now in here, I'm going to create a new function
completely, and I'm going to call it 'a_new_function()',
and all this is going to do is say, 'print(
"This is from my_functions.py")'. That's it.
I'll make that a tad smaller, too.
Now, beside the 'MyPackage' directory, I'm going to create
a new file in here.
So I'm just going to copy this over, and I'm going to call
this 'package_program.py'.
Delete all that. 'package_program.py'.
And so here I've got 'package_program.py', and it's living
beside the folder called 'MyPackage'.
Now, what I can do, is say 'from MyPackage.my_functions',
and you can see that VS Code is actually trying to autofill that for me,
'import', and what am I trying to import from here?
I can import a function called 'a_new_function'.
Let's go ahead and import that.
And let's simply run that as a function.
[no audio]
So I'm in my Desktop here, and we can see the files that I have.
And I've got 'MyPackage',
that's a folder; and 'package_program',
that's the file that we're using right now. So,
'python package_program.py', and it says, "This is from
my_functions.py'.
And so really, all we've done here is very similar to what
we did with a module,
but instead of just being in another file, we said it's also
in another folder, and we can do this on and on and on.
So if we wanted to, we could create a subfolder in 'MyPackage',
create a new folder, call it 'MySubFolder'.
[no audio]
In here let's go ahead and create a new file, '__init__.py',
and let's create a new one in here called, a new file called
'subfolder_functions.py',
and in here we can create
[no audio]
a variable. So let's create a variable, sort of like a constant,
and in Python, we don't actually have constants,
but what we do have is a naming convention.
So instead of saying a variable name is like, 'name =',
something, when we have a variable that we don't want to change
we usually use uppercase, and that tells us that it's supposed to be a constant.
So I could say 'COURSE = "Python for Everybody".
And in here I can have a function called 'which_course_is_this',
and due to scoping, we know we have access to this,
so we can just simply say, 'print(
"The course is"
[no audio]
COURSE)'. So let's go back here, close that file 'package_program',
let's also do 'from MyPackage.MySubFolder.
subfolder_functions import', and I'll make that couple lines
smaller. We want to import both 'COURSE', and this.
We don't need to, we could actually just import this, and
it'll work totally fine.
But if we wanted to, we could import that variable as well.
So let's go ahead and execute this.
And so now we're importing a function that we made from
'MyPackage.my_functions',
and then we're also importing a function from a file called
'subfolder_functions', a folder above that,
the parent folder is 'MySubFolder', and the parent above
that is 'MyPackage'.
And when we run this, when we run this in Python, we'll see
"This is from my_functions.py".
And this one is, "The course is: Python for Everybody".
And we actually don't have any 'print' statements in here whatsoever.
And so we've now created what is effectively a package.
Now to clarify how some of this is actually working,
when you import from something you can say 'from MyPackage',
and usually we name it
whatever the regular package is called, it doesn't have to be
uppercase or anything like that.
Like we saw earlier that you could have 'from colorama import
Fore', but the weird thing here is this '.' notation.
So sort of what this is doing here is instead of for whatever
reason, and I actually don't know this, but for whatever
reason, we use dots instead of slashes.
And so in Python, we can say 'from MyPackage' folder,
look in 'MySubFolder' folder,
and then the last one is likely to be a Python file, which
you just automatically assume,
so you don't have to write '.py',
and instead we use this thing called '.' notation where folder
name, folder name, file name, import your function.
We could if we wanted to, do this a different way as well.
We could say 'from MyPackage.MySubFolder import
subfolder_functions', and then we could run 'subfolder_functions.
which_course_is_this'
And let's go ahead and execute this just to see that this is going to work.
Now, I commented out both functions that we're running originally,
so this one is not going to show up anymore.
[no audio]
And there it is. Just the one shows up, and we can even alias this if we
want to. You might see this from time to time, because
'subfolder_functions' is kind of a long name,
so you might see something like this 'as', I don't know,
'subfolder_functions', 'sff'.
And then we can use that instead.
And now this looks a lot like a class, doesn't it, where
we have an object or a class name,
and then we've got a method in it.
And every now and then I mentioned that Python is, everything
in Python is pretty much an object.
Files are no different.
And I do remember saying that. Files are no different in the
sense that we imported this file, and now we have a function
in here, and that's the method.
Very interesting how they're very, very similar, isn't it?
So just for funsies let's go ahead and run this, and it still works.
[no audio]