Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Modules and Packages: An Introduction (Summary)

Download

Sample Code (.zip)

20.4 KB
Download

Course Slides (.pdf)

362.0 KB

00:00 Hey, congratulations! You completed the course. This video is going to be not only a conclusion, but a review of everything that you’ve covered throughout this course.

00:11 It started with an intro and course overview, and then you dove right into writing your own module and importing it.

00:22 The next video let you explore the module search path. Where will your computer look for the modules you’re importing? The import statement was next, and all the different forms that it can take.

00:39 The next video explored namespaces a little deeper by talking about the dir() function and how you can use it to look up the local symbol table.

00:53 After that, you got to practice executing a module as a script, and you learned about the attribute __name__. Then you learned if you import a module a second time, it doesn’t reload the contents inside of it, so you’d have to quit the interpreter and restart it, but you also saw that there is a tool in the importlib module that you can import called reload(). As a project grows, it starts to make sense to look at organizing multiple modules into a package, in that sort of directory structure.

01:32 Then you learned about the __init__.py file that can help you with package initialization.

01:41 The next video was all about wildcard importing and the ways that you can set up your module or packages to set up your user for success if a user is going to use the wildcard (*) when they’re importing.

01:55 As the project may grow and the amount of modules that you have increases, it may make sense to break up the package into subpackages, and how that affects your namespaces. And finally, there was this conclusion and course review.

02:09 I want to thank you for watching this course. Make sure that you take time to practice with what you’ve learned.

Avatar image for dsnyder0cnn

dsnyder0cnn on Jan. 28, 2020

Hmmm…it looks like the last section, “Python Modules and Packages: An introduction (Summary)” has been incorrectly linked to “Python Packages”. Please update/replace the summary. Thanks.

Avatar image for Chris Bailey

Chris Bailey RP Team on Jan. 28, 2020

Hi @dsnyderOcnn, Thanks for the head up! It has been fixed, try again or refresh.

Avatar image for Felix Vadan

Felix Vadan on Jan. 29, 2020

Thanks for this great course Chris! Made working with modules and packages much clearer. Now I need to watch it again.

Avatar image for Priya katta

Priya katta on Jan. 30, 2020

Thanks Chris! Python modules and packages concepts are much clearer now.

Avatar image for Ravi

Ravi on Jan. 30, 2020

Hi Chris, I have a question which is not related to modules, In some places when you written classes you have given Classname(), what is the difference does it make when writing as Classname() vs Classname. I know python supports both, but want to understand how python works internally with these notations

Avatar image for Chris Bailey

Chris Bailey RP Team on Jan. 30, 2020

Hi @Ravi, This may be hard to get into deeply within a comment, but I will try to see if I can help shed some light on your question. When defining a simple new class, that isn’t based on an existing class, you would use the syntax class Classname: with no () after, and the methods and such would follow after the : indented underneath. When creating an object based on that class you would use the syntax x = Classname(). That is generally what I was doing in most of these examples as my classes were really just placeholders to show the type of content you can work with in modules and packages. If you want a deeper dive I would suggest this article Object-Oriented Programming (OOP) in Python 3 or the related video course Intro to Object-Oriented Programming (OOP) in Python. I hope this helps.

Avatar image for Chris Bailey

Chris Bailey RP Team on Jan. 31, 2020

Hi @Ravi, I think I may have misconstrued your question. So I will take another tack with this answer. When creating a class that is not based on / inheriting from an existing object, the () is optional and typically not used. I looked through the docs for python 3.8 on classes and it doesn’t show that style, only class Classname: . The class will still be “based” on, or in other words, always inherit from the class object. So the syntax class Classname:, class Classname():, and class Classname(object) will give the same result in Python 3. I think you may be reading code that may be trying to be more “Agnostic”, in that it could work with versions of Python 2. Here is a link to a discussion that goes deeper into it on Stack Overflow.

Avatar image for Phil M

Phil M on Feb. 1, 2020

Hi Chris,

This was a great course. It really exposed me to more of the nuts & bolts of Python. This is something I am struggling to understand, working on my Django project. I really appreciate your sharp quick and concise approach to the subject of OOP in Python Modules and Packages-even though I found it necessary to started and stopped the video many…times to practice and make sure I was on the same page - it really worked!

bphthon, very nice! This was my first time using it and I really like it. I had an issue installing it to macOS Mojave (10.14.6) but found a solution here, just incase anyone else has this issue.

Thank you - Well done!

Avatar image for Ravi

Ravi on Feb. 3, 2020

Thanks Chris, it really helps

Avatar image for Sergey Morozik

Sergey Morozik on Feb. 6, 2020

Great course. Brought to order and systematized all I have learned just googling things quickly. Thank you!

Avatar image for Pygator

Pygator on Feb. 16, 2020

Really understand Package and module hierarchy now. And starting to think how i can better structure my projects with these new concepts.

Avatar image for mikesult

mikesult on Feb. 18, 2020

Thanks Chris, excellent course. It really filled in some gaps in my python knowledge.

Avatar image for pshapard

pshapard on April 7, 2020

Great course Chris. I had a general understanding of packages and modules, but I gained a deeper insight into sub-packages and dot notation.

On to the next course.

Thanks, Patrick.

Avatar image for Adam Benson

Adam Benson on May 1, 2020

Thank you for presenting the concepts in a organized way. My question is about the application. Why, or really when, will I need to use this? Is there an exercise I should follow to see how this will be applicable and what happens if I don’t use these lessons (I could have missed it - wouldn’t be the first time).

Avatar image for Chris Bailey

Chris Bailey RP Team on May 1, 2020

Hi @Adam Benson, Here are a couple of examples of using modules and packages.

First, if you are creating something more complex than a single script that runs by itself. When you start to have more than a couple hundred lines of code or where the functionality makes sense to divide your code up into functional parts. It’s why in Python you import from math to have constants and other special math functions, or you import from datetime to do things with those type of objects in your code. Python the language itself is organized into modules and packages. It is inefficient to have all the code as one big file, when you can only import and use what you need.

Second, if you want to re-use parts of your code in later projects. Maybe you built a great function that cleans up names and addresses for your marketing emails. Not only could you now re-use that code, but someone else on your team could to. If you share those modules in a central place, you and your team could import what they need. Also, you won’t need to find a spot to copy and paste it into your one large script. You can import only what you need.

Third, if you want to share it with the world. The next level is packaging all your code up and sharing it on a packaging index. This article goes into that. How to Publish an Open-Source Python Package to PyPI, there is also a video course on this topic.

I hope I am answering your question correctly.

Avatar image for jeffcabral7

jeffcabral7 on May 2, 2020

Really enjoyed this course Chris! This is a great example on how a basic/introductory course should be presented. Thanks!

Avatar image for Cristian Palau

Cristian Palau on May 7, 2020

Thank you Chris for this course!