Skip to content

Python Modules

Every Python file is a module.

You can import a module from other files, and that's the base of any program of moderate complexity, as it promotes a sensible organization and code reuse.

In the typical Python program, one file acts as the entry point. The other files are modules and expose functions that we can call from other files.

The file dog.py contains this code:

def bark():
    print('WOF!')

We can import this function from another file using import, and once we do, we can reference the function using the dot notation, dog.bark():

import dog

dog.bark()

Or, we can use the from .. import syntax and call the function directly:

from dog import bark

bark()

The first strategy allows us to load everything defined in a file.

The second strategy lets us pick the things we need.

Those modules are specific to your program, and importing depends on the location of the file in the filesystem.

Suppose you put dog.py in a lib subfolder.

In that folder, you need to create an empty file named __init__.py. This tells Python the folder contains modules.

Now you can choose, you can import dog from lib:

from lib import dog

dog.bark()

or you can reference the dog module specific function importing from lib.dog:

from lib.dog import bark

bark()
→ Download my free Python Handbook!

THE VALLEY OF CODE

THE WEB DEVELOPER's MANUAL

You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

Related posts that talk about python: