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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python Modules and Packages: An Introduction (Summary)

Download

Sample Code (.zip)

20.4 KB

Download

Course Slides (.pdf)

362.0 KB

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.

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.

Become a Member to join the conversation.