DEV Community

Cover image for Quark's Outlines: Python Modules
Mike Vincent
Mike Vincent

Posted on

Quark's Outlines: Python Modules

Quark's Outlines: Python Modules

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Modules

What is a Python module?

You may want to split your program into smaller parts. Each part should focus on one job. In Python, each part can be a module.

A Python module is a file with Python code. The file ends in .py. You can load that file into your program and use the things it defines. This includes functions, classes, and values.

Python comes with many standard modules. You can also write your own.

Python lets you reuse code by loading modules.

import math
print(math.sqrt(16))
# prints: 4.0
Enter fullscreen mode Exit fullscreen mode

How does a Python module store names?

Each Python module has its own namespace. This namespace is a dictionary. It holds all the names defined in that module.

When you assign something in a module, you add to that namespace. When you read an attribute, Python looks it up in the namespace.

Python stores a module's names in a dictionary.

import math
print(math.__dict__)
# prints: 5.0
Enter fullscreen mode Exit fullscreen mode

You can use the __dict__ attribute to look inside a module.

What attributes do Python modules have?

Python modules have fixed attributes. These include:

  • __name__: the name of the module
  • __doc__: the module's docstring
  • __file__: the file path, if loaded from a file
  • __dict__: the name-to-object map

These help you inspect and debug modules at runtime.

Python gives each module built-in attributes.

import random
print(random.__name__)
print(random.__file__)
Enter fullscreen mode Exit fullscreen mode

This prints the name and file path of the random module.

How does Python import modules?

You use the import statement. Python reads the .py file and runs the top-level code once. Then you can use the things that module defines.

You can assign a module to a name, or just pull one item using from ... import.

Python uses the import system to load code from a file.

import time
print(time.time())
# prints: the current time in seconds since 1970
Enter fullscreen mode Exit fullscreen mode

A Historical Timeline of Python Modules

Where do Python modules come from?

Python modules were based on older language designs. They grew to support large programs, tools, and systems.


People built ways to reuse code

1960 — Library subroutines, FORTRAN let users load external routines by linking separate files.
1972 — Separate compilation, C language supported .h and .c files with global scope control.

People designed Python's module system

1991 — Basic import statement, Python 0.9.0 introduced the import syntax and namespace handling.
2000 — Package folders and __init__.py, Python 1.5+ allowed hierarchical modules using directories.

People expanded modules for real-world use

2001 — Module introspection, Python 2.1 added __file__, __name__, and __dict__ for debugging and inspection.
2007 — Relative imports, Python 2.5 added clean support for importing from the same package.
2008 — Importlib and meta hooks, Python 3.0 moved module logic to Python code itself.
2014 — Namespace packages, Python 3.3+ allowed packages without __init__.py files.


Problems & Solutions with Python Modules

How do you use Python modules the right way?

You can write better programs by dividing code into modules. Python modules help you organize logic and reuse parts across files. The problems below show how Python's module system solves real tasks.


Problem: How do you reuse code from another file in Python?

You wrote a function to greet users in one file. Now you want to use it in a second file without copying the code.

Problem: How do you load a function from another file?
Solution: Save the function in a .py file. Then use import to load it into the new file.

Python lets you share code between files using import.

# greet.py
def say_hello(name):
    return f"Hello, {name}!"

# main.py
import greet
print(greet.say_hello("Ada"))
# prints: Hello, Ada!
Enter fullscreen mode Exit fullscreen mode

Problem: How do you see all names in a module in Python?

You are working with a big module. You want to know what is defined in it without reading all the source code.

Problem: How do you inspect a module's contents?
Solution: Use the dir() function or look inside __dict__.

Python lets you list everything inside a module.

import math
print(dir(math))
# prints: ['__doc__', '__loader__', ..., 'sqrt', 'tan']
Enter fullscreen mode Exit fullscreen mode

This shows the names defined in the math module.


Problem: How do you store values between files in Python?

You wrote some constants like pi = 3.14. You want to use them in many scripts without repeating them.

Problem: Where should you store shared values?
Solution: Put them in a module and import the module where needed.

Python lets you define constants in one module and reuse them.

# config.py
pi = 3.14

# circle.py
import config
print(config.pi * 2)
# prints: 6.28
Enter fullscreen mode Exit fullscreen mode

Problem: How do you access a module's file path in Python?

You are debugging a project. You want to know where Python loaded a module from.

Problem: How do you find the path to a module's file?
Solution: Use the __file__ attribute.

Python lets you find where a module was loaded from.

import os
print(os.__file__)
# prints: /usr/lib/python3.x/os.py
Enter fullscreen mode Exit fullscreen mode

Problem: How do you update a module's attribute in Python?

You imported a module and want to set a new value in it. You want this change to reflect when you use the module later.

Problem: Can you change a module's data after import?
Solution: Yes. A module is just an object with attributes. You can assign to them.

Python lets you set or update values in a module.

import types
config = types.SimpleNamespace()
config.mode = "test"
print(config.mode)
# prints: test
Enter fullscreen mode Exit fullscreen mode

Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!


Mike Vincent is an American software engineer and app developer from Los Angeles, California. More about Mike Vincent

Top comments (0)