Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

When should I use a relative import in a small Python package?

Quick answer

Use a relative import when one module needs another module inside the same package and the importing module has package context. For example, from ..formatting import format_total refers from a subpackage to a module in its parent package. Use an absolute import for an application’s main entry point. If a relative import fails, check how the module was launched: running a file directly can leave it without the package context the import needs. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a small package with a few files. What does a relative import mean, and when would I use one?

Geez

A relative import locates another module by its position within the current package. One leading dot means the current package; additional dots refer to parent-package levels. Relative imports use the from ... import ... form. [S1][S2]

For example, imagine this layout:

shop_tools/
    __init__.py
    app.py
    formatting.py
    reports/
        __init__.py
        summary.py

In summary.py, this import refers to the formatting.py module in the parent package:

from ..formatting import format_total

The two dots move from shop_tools.reports up to shop_tools; formatting identifies the module there. This style is useful for expressing that a dependency is part of the same package. [S1][S2]

Follow-up question

Would an absolute import be wrong for that same dependency?

Geez

No. You can also write:

from shop_tools.formatting import format_total

Python supports both absolute and explicit relative imports for references within a package. [S1] The absolute form spells out the package path; the relative form emphasizes the relationship between modules within the package.

For a small package, choose the form that makes dependencies easiest to follow, then use it consistently for similar relationships. Relative imports can make nearby internal links clear. Absolute imports can make the package name and destination explicit. The Python documentation permits both and does not set one universal style preference. [S1]

Follow-up question

Why might a relative import work when I use the package but fail if I run the file itself?

Geez

A relative import depends on the importing module having package context. When a source file is run directly, its import metadata differs from package execution; the import reference notes that direct execution sets __main__.__spec__ to None. Using -m supplies module metadata when applicable. [S2]

For example, running summary.py directly may not provide the context needed for from ..formatting import format_total. Instead, run a package module with its module name from a location where the package is available, for example:

python -m shop_tools.reports.summary

The exact command depends on your layout and working directory. For an application’s main module, use absolute imports: the tutorial notes that a main module has no package context for relative imports. [S1]

Follow-up question

Can you give me a quick rule and a small example I can apply to my own package?

Geez

A useful rule of thumb is: use relative imports for internal package links when the importing module has package context; use absolute imports for the application entry point. This is a practical choice, not a requirement that every package use one style. [S1]

With the example layout, shop_tools/reports/summary.py could contain:

from ..formatting import format_total


def make_summary(total):
    return format_total(total)

Then shop_tools/app.py, acting as the entry point, could use:

from shop_tools.reports.summary import make_summary

If a relative import fails, check both the import path and how the module was launched. Direct execution can lack package context, while -m supplies module metadata when applicable. [S2] The right style for other internal imports depends on which form makes your package easier to understand.

A practical checklist

  • Identify the importing module’s package and the target module’s location.

  • Use one dot for the current package and additional dots for parent-package levels. [S2]

  • Choose the style that makes internal dependencies clearest, and use it consistently.

  • Use absolute imports in the application’s main module. [S1]

  • Check how a module was launched when diagnosing a relative-import failure. [S2]

A few more useful answers

Does a relative import make a package more portable?

It expresses a relationship inside the package, but the documentation cited here does not establish that relative imports are universally more portable. Their use still depends on package context and how the module is launched. [S1][S2]

Can I use a relative import in the main script?

A module used as the application’s main module has no package context for relative imports, so use an absolute import there. [S1]

What does `from ..module import name` mean?

The two leading dots refer to parent-package levels, and module identifies a module from that level. The exact target depends on the importing module’s package. [S2]

Sources

References used for this guide.

  1. [S1] Python 3.13 tutorial: modules and intra-package references
    docs.python.org · Checked
  2. [S2] Python 3 import system reference: relative-import syntax and package context
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work