Python Modules and Packages: An Introduction (Summary)
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
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.
Chris Bailey RP Team on Jan. 28, 2020
Hi @dsnyderOcnn, Thanks for the head up! It has been fixed, try again or refresh.
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.
Priya katta on Jan. 30, 2020
Thanks Chris! Python modules and packages concepts are much clearer now.
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
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.
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.
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!
Ravi on Feb. 3, 2020
Thanks Chris, it really helps
Sergey Morozik on Feb. 6, 2020
Great course. Brought to order and systematized all I have learned just googling things quickly. Thank you!
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.
mikesult on Feb. 18, 2020
Thanks Chris, excellent course. It really filled in some gaps in my python knowledge.
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.
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).
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.
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!
Cristian Palau on May 7, 2020
Thank you Chris for this course!
stephenwhite597 on June 4, 2020
Nicely presented material Chris, good to see this topic layed out cleanly and systematically explained in a manner very understandable at all levels.
Should be made part of the standard teaching to get people to understand that modularisation and seperation of concerns is an important aspect of structuring a complex system that can be left behind for others to maintain and understand in the future.
Patricio Urrutia on July 5, 2020
Hi Chris, thanks for the course. It was very helpful!
I have a doubt, things like, for example, pandas are modules then? I had imagined that Series and DataFrames were in different modules of the “package” pandas, but it seems, due the way we import it, it is a module.
Or it is a way to accomplish this? (having all the modules and submodules inside the “pkg namespace). Maybe through the init.py file?
Thanks!
Alain Rouleau on July 16, 2020
Already knew a fair amount when it came to creating and working with modules. But now I know so much more, thanks! Definitely going to use what I just learned.
Knowing how to work with packages, modules and creating your own hierarchy is kind of the missing link when it comes to Python, if you ask me. Usually that whole subject is not taught or explained very well. But you made it so simple.
Dennis Smith on Dec. 7, 2020
I have referred back to the article version of this course countless times but these videos worked well to give me a deeper understanding and solidify the knowledge. Well done Chris!
Alex Duzsardi on Jan. 1, 2021
Thank you for this course , it clarified a few things for me.
Krauss on March 2, 2021
Hey Chris, thanks for the great video course. It indeed helped me understanding and filling up some knowledge gaps. One question remains though, is there a way to get the import
command to append the newly created modulo’s path dinamically? I’ve tried using the __init__.py
file to do the append
command but id doesn’t work.
qmark42 on April 19, 2021
Thank you Chris. This course has answered several questions I have had for quite a while. Such things as what does __init__
mean and is it special, have been answered along with some dot notation questions. I will have to review the manuscript as my memory is not so great at times but now I know where to look. Again, Thank you.
nestor on April 23, 2021
I’m preparing myself to get a Python skill to develop one Real Time measurement application using Raspberry Pi 3B, AC and DC energie sensor, this App must have connectivity options for Wi-Fi and Bluetooth and show the measurements in the GUI for mobile or a computer, so, whats Python’s tools do you recomend me to to know and review for this project if I need to do......Real Time, GUI, connectivity protocols Wi-Fi & BLE…?.......I have previus experience programming but in LabVIEW......nothing to do with Python. Thanks for your recomendation.
Carlos on Aug. 1, 2022
Thanks Chris! Comprehensive tutorial. Just a comment related to bpython dunder name, at least 0.20.1 on top of Python 3.8.13, Linux: running the script it is console instead of main
Tyrone on Oct. 8, 2022
Thanks for this great course Chris! I learnt heaps and you made working with made working with modules and packages much clearer and now it seems so easy. Love your easy paced style, clear language and the way you lift the mist in such a matter of fact way. Well done!!.
colmedob on Jan. 14, 2023
Great course, Chris. As my project grew, I needed a way to test functions independently of the main code, and this course gave me the solution.
Become a Member to join the conversation.
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.