3

I'm reading PEP338 .Some words confused me:

If the module is found, and is of type PY_SOURCE or PY_COMPILED , then the command line is effectively reinterpreted from python <options> -m <module> <args> to python <options> <filename> <args> .

Do modules have types in Python?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
wow yoo
  • 855
  • 8
  • 26
  • 1
    There are *types of modules*, in that they were sourced from a bytecompiled cache file or from source. The word *type* is not the same thing as the class of an object. – Martijn Pieters Oct 22 '16 at 16:42
  • 1
    See about python file extensions http://stackoverflow.com/a/18032741/6575931 – Uriel Oct 22 '16 at 16:49

3 Answers3

5

Modules can be loaded from different sources. The author refers to 2 specific sources the module was loaded from, see the imp module documentation:

imp.PY_SOURCE
The module was found as a source file.

[...]

imp.PY_COMPILED
The module was found as a compiled code object file.

[...]

imp.C_EXTENSION
The module was found as dynamically loadable shared library.

These values are used in the return value of the imp.get_suffixes() function, among others.

The PEP states that only modules loaded from source (.py files) and from a bytecode cache file (.pyc) are supported; the -m switch does not support C extension modules (typically .so or .dll dynamically loaded libraries).

The resulting module object is still just a module object; the word type in the text you found is not referring Python's type system.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

The type of module means the type of the file where the module is stored, as python files have some possible types (and extensions.

The most common are compiled python files (pyc extension) or the regular, python plain source (py).

There are many other py file extensions, see the (almost) full list here: https://stackoverflow.com/a/18032741/6575931.

Community
  • 1
  • 1
Uriel
  • 15,579
  • 6
  • 25
  • 46
1

Quoting from the link PEP338

Proposed Semantics The semantics proposed are fairly simple: if -m is used to execute a module the PEP 302 import mechanisms are used to locate the module and retrieve its compiled code, before executing the module in accordance with the semantics for a top-level module.

Now let us refer to the documentation of imp (the import mechanism) and determine the different types of modules that can be imported imp.get_suffixes()

imp.get_suffixes() Return a list of 3-element tuples, each describing a particular type of module. Each triple has the form (suffix, mode, type), where suffix is a string to be appended to the module name to form the filename to search for, mode is the mode string to pass to the built-in open() function to open the file (this can be 'r' for text files or 'rb' for binary files), and type is the file type, which has one of the values PY_SOURCE, PY_COMPILED, or C_EXTENSION, described below.

and subsequently it explains what the different types are

imp.PY_SOURCE The module was found as a source file.

imp.PY_COMPILED The module was found as a compiled code object file.

imp.C_EXTENSION The module was found as dynamically loadable shared library.

So, the types mentioned in PEP 338 are nothing but the types of modules that can be imported and of these only PY_SOURCE or PY_COMPILED are the only two types out of the above three the command line is effectively reinterpreted from python -m to python .

Abhijit
  • 62,056
  • 18
  • 131
  • 204