Python Basics: Modules and Packages (Overview)

As you gain experience writing code, you’ll eventually work on projects that are so large that keeping all the code in a single file becomes cumbersome.

Instead of writing a single file, you can put related code into separate files called modules. You can put individual modules together like building blocks to create a larger application.

In this video course, you’ll learn how to:

  • Create your own modules
  • Use modules in another file through the import statement
  • Organize several modules into a package

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. You can also check out the other Python Basics courses.

Note that you’ll be using IDLE to interact with Python throughout this course.

Download

Course Slides (.pdf)

4.7 MB
Download

Sample Code (.zip)

651 bytes

00:00 Welcome to another Python Basics course on realpython.com. I’m Philipp, and today you’ll learn about modules and packages. At this point, you probably have run a single Python script and feel somewhat comfortable with it, but how about coordinating multiple Python scripts in one project?

00:20 As you gain experience writing code, you’ll eventually work on projects that are so large that keeping all the code in a single Python file becomes cumbersome. Instead of writing a single file, you can put related code into separate files.

00:34 These files can be put together like building blocks to create a larger application in Python. These building blocks are called modules.

00:44 During this course, you’re using IDLE, which stands for Integrated Development and Learning Environment. IDLE comes with most Python distributions out of the box.

00:53 If you need a quick refresher on using IDLE, then check out one of our video courses in the Python Basics series entitled Setting Up Python. Alternatively, if you want to take a deeper dive, then you may watch the Starting with Python IDLE Video course. Of course, you can use any other editor of your choice, but if you want to follow along closely, it’s a good idea to stick with IDLE for this video course.

01:20 In this course, you’ll learn how to create your own modules. You’ll also learn how to use modules in another file via the import statement and adjust import statements to avoid namespace conflicts. Also, you’ll learn how to organize several modules into a package, and if some of those terms don’t make sense to you yet, no worries.

01:39 That’s what the course is for. So let’s get started.

George on April 9, 2024

I want to create modules and packages to get more structure into my programs. I use the init.py way to let the main program find the package or module. Something I am missing because the module I try to import cannot be found.

Bartosz Zaczyński RP Team on April 10, 2024

@George Did you name your file init.py or __init__.py? It has to use the double underscores to be properly recognized by Python.

Become a Member to join the conversation.