Concepts / Module Naming Conventions

Module Naming Conventions

The .py extension identifies the saved Python file, while the filename without .py becomes the module name used in an import statement.

  • Programming

One File, Two Roles

A Python program is also a module. The key step is saving Python code in a file whose name ends with the .py extension. Once saved this way, the file can be treated as a module, and its filename supplies the name used when importing it.

Key point

The .py extension identifies the saved Python file, while the filename without .py becomes the module name used in an import statement.

What do you think happens?

If a file is named calculator.py, what module name would an import statement use?

  • calculator.py

  • calculator

  • cal-culator

  • py.calculator

Reveal answer
Answer:

calculator

Python drops the .py extension when the filename becomes the module name used in an import statement.

From Filename to Import Name

The relationship is direct: begin with the complete filename, remove the .py extension, and use what remains as the module name. For example, the file calculator.py supplies the module name calculator. The import statement refers to calculator rather than calculator.py because Python handles the extension automatically.

calculator.pysaved filenamecalculatorfilename without .pycalculatorname used by importremove .pyuse in import statement
How does a file named calculator.py become the calculator module referenced by an import statement?
python

The import name in this example corresponds to a file named calculator.py. Writing import calculator.py would not follow the filename-to-module-name relationship described here, because the .py extension is omitted in the import statement.

Saving the First Module

Worked example

Creating mymodule

Create a simple module and identify the name used to import it.

  1. 1
    Write Python code

    Place a simple Python statement in a file. The source material describes a module containing a single string statement.

  2. 2
    Save the file

    Save the file with the name mymodule.py. Saving Python code with the .py extension creates the module file.

  3. 3
    Remove the extension for importing

    The complete filename is mymodule.py, but the module name is mymodule because the .py extension is dropped in the import statement.

  4. 4
    Write the import

    The corresponding import name is mymodule.

Result

The file mymodule.py becomes the module named mymodule when imported.

python

The important change is not a special declaration inside the file. Saving the Python code as mymodule.py gives the file its module identity. The filename and the import name are connected by removing only the .py extension.

Where Python Looks

Creating the file is only half of the task. Python must also be able to find it when the importing program requests the module. The module file must be in the same directory as the importing program or in a directory listed in sys.path.

importing programrequests a modulesame directorylocation Python can searchmodule.pyfound module filesys.path directorylocation Python can searchsearchessearchesmodule can be foundmodule can be found
Where must a Python file be located relative to the importing program for Python to find and import it successfully?
Best practice

When an import does not work, check the module's location before changing the import name. Confirm that the file is in the same directory as the importing program or in a directory listed in sys.path.

Names That Import Reliably

Module filenames should use valid Python identifiers. In particular, avoid spaces, hyphens, and special characters. These characters can lead to import errors because the filename no longer follows the naming form expected for a module name.

  • Common mistake

    Including the .py extension in the import name

    What it looks like

    The file is calculator.py, but the import is written as calculator.py.

    Why it fails

    The module name is the filename without the .py extension.

    Do this instead

    Use calculator as the module name.

  • Common mistake

    Using spaces in a module filename

    What it looks like

    saving the file as data tools.py

    Why it fails

    Module filenames should use valid Python identifiers, and spaces are not valid in those names.

    Do this instead

    Choose a filename without spaces.

  • Common mistake

    Using a hyphen in a module filename

    What it looks like

    saving the file as data-tools.py

    Why it fails

    Hyphens are not valid in a Python identifier used as a module filename.

    Do this instead

    Choose a filename without hyphens.

  • Common mistake

    Putting the module outside Python's searchable locations

    What it looks like

    The importing program requests mymodule, but mymodule.py is neither in the same directory nor in a sys.path directory.

    Why it fails

    Python must find the file in one of those locations before it can import it.

    Do this instead

    Place the file in the same directory as the importing program or in a sys.path directory.

Naming and Placement Practice

EASY

A learner saves Python code in a file named report_data.py. The importing program is in the same directory. What module name should the import statement use, and which placement requirement is satisfied?

Hints
  • Remove the .py extension to find the module name.
  • Compare the locations of the two files.
Worked example

Checking the answer

Determine the import name and placement result for report_data.py.

  1. 1
    Find the module name

    Remove the .py extension from report_data.py. The module name is report_data.

  2. 2
    Check the location

    The importing program and the module are in the same directory, which is one of the locations where Python can find the module.

Result

The import name is report_data, and the same-directory placement satisfies the stated search requirement.

Module Naming Checklist

  1. 1
    Every Python program is a module.
  2. 2
    Saving Python code with the .py extension creates a module file.
  3. 3
    The filename without .py becomes the module name used in an import statement.
  4. 4
    The module must be in the same directory as the importing program or in a sys.path directory.
  5. 5
    Use valid Python identifiers for module filenames, avoiding spaces, hyphens, and special characters.

Key Takeaways

  • A Python program saved as a .py file is a module.
  • Python removes the .py extension when deriving the module name for an import statement.
  • A module must be located where Python searches, such as the importing program's directory or a sys.path directory.
  • Valid Python identifier characters make module filenames safer to import.
  • Successful importing depends on both the module's name and its placement.

More concepts starting with M