This function can be found in the importlib.machinery
module . There are many constants inside that relate to various extensions of the Python module:
>>> import importlib >>> importlib.machinery.SOURCE_SUFFIXES ['.py', '.pyw'] >>> importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES ['.pyo'] >>> importlib.machinery.EXTENSION_SUFFIXES ['.pyd'] >>> importlib.machinery.DEBUG_BYTECODE_SUFFIXES ['.pyc'] >>>
This way you can easily join them in global set 1 :
>>> set(importlib.machinery.SOURCE_SUFFIXES + ... importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES + ... importlib.machinery.EXTENSION_SUFFIXES + ... importlib.machinery.DEBUG_BYTECODE_SUFFIXES) {'.pyw', '.py', '.pyd', '.pyc', '.pyo'} >>>
You may also be interested in the all_suffixes
function:
>>> importlib.machinery.all_suffixes() ['.py', '.pyw', '.pyc', '.pyd'] >>>
Note that this function will replace .pyc
with .pyo
if Python is running with the -O
or -OO
. To avoid this, you can:
>>> set(importlib.machinery.all_suffixes() + ... importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES + ... importlib.machinery.DEBUG_BYTECODE_SUFFIXES) {'.pyw', '.py', '.pyd', '.pyc', '.pyo'} >>>
This ensures that both .pyc
and .pyo
are in the set.
Finally, you should be wary of importlib.machinery.BYTECODE_SUFFIXES
. As @MartijnPieters notes in the comments, it will always be equal to either OPTIMIZED_BYTECODE_SUFFIXES
or DEBUG_BYTECODE_SUFFIXES
. This means that if you add it to the collection, you will get either a duplicate .pyc
value or a duplicate .pyo
value (if you are not using a set of courses).
From docs :
importlib.machinery.BYTECODE_SUFFIXES
A list of strings representing the suffix of the recognized file for bytecode modules. Set the value to DEBUG_BYTECODE_SUFFIXES
or OPTIMIZED_BYTECODE_SUFFIXES
depending on whether __debug__
is true.
I did not use this constant because I need both OPTIMIZED_BYTECODE_SUFFIXES
and DEBUG_BYTECODE_SUFFIXES
in the collection. So there is no reason to add it.
1 I decided to use the kit because they have faster search times than lists. Meaning, they are better suited for a global collection of values ββthat will not change and that do not require a special order. In addition, they ensure that we do not accidentally add duplicate extensions to the collection.