Cantaff0rd
Cantaff0rd

Reputation: 723

Leading underscore before the name of Python module

I have searched around for this but I did not find any answer. I'm looking at a Python code written by someone else and there is a module say "GUI_Module". This module contains a class which contains GUI related methods. The module is then imported in the __main__ Python file which makes use of those methods.

Instead of being imported like this with its actual name:

import GUI_Module

it is imported like this with a leading underscore:

import _GUI_Module

Now I know about most meanings of underscores in Python but I have not found anything that explains what a single underscore before the name of a module that we are importing is supposed to do.

Upvotes: 14

Views: 10839

Answers (3)

Mark Amery
Mark Amery

Reputation: 154934

In the implementation of the standard library in CPython, the _ prefix seems to indicate that a module is "private", in the sense that you shouldn't import it directly. Usually, this is because its functionality is imported and exposed by some "public", documented module.

Such a private module may be an accelerator module - a fast C implementation that exists alongside a slower pure-Python implementation of the same module (and which the pure-Python version tries to import everything from, so that the faster version is always automatically used if available).

Not all modules implemented in C are accelerator modules, though. In some cases, the _-prefixed module written in C is the only implementation available. For instance, CPython doesn't contain a pure-Python implementation of the ctypes or sqlite modules; these modules respectively depend on importing stuff from the _ctypes or _sqlite3 modules (which are implemented in C) and hence can't be used by a Python interpreter that doesn't support Python's C API.

And not all _-prefixed modules are even implemented in C! For example, the _pydecimal module is a pure-Python implementation of the decimal module. The implementation of decimal in CPython tries to import _decimal (implemented in C) if it's available, and if not, falls back to importing _pydecimal.

The only thing that seems to be universally true of these _-prefixed modules is that they're all implementation details of some documented, "public" module that doesn't have a _-prefix, and that you're supposed to use that module instead of directly importing stuff from the _-prefixed one.

Upvotes: 17

snakecharmerb
snakecharmerb

Reputation: 55799

In the Python standard library, a leading underscore in a module name may denote an accelerator module: a module written in C that provides a more performant implementation of the pure Python version, for example _csv and csv. Accelerator modules are described in PEP-399.

Outside the standard library, the leading underscore conventionally denotes a private module, that is one that should not be used directly. This could be because the main module contains a package's public API, and the underscore version contains implementation- or platform-dependent code.

Upvotes: 2

Stop harming Monica
Stop harming Monica

Reputation: 12620

It does the same as adding any other character. The module name changes and that results in a different module being imported.

There is also a convention that a name starting with an underscore is internal and should not be used by clients.

Upvotes: 0

Related Questions