P
P
PyLearner2018-05-19 18:41:30
Python
PyLearner, 2018-05-19 18:41:30

How does module compilation work in python?

I'm reading the documentation of modules in Python, I can't understand the penultimate paragraph in the compilation block: https://docs.python.org/3/tutorial/modules.html#co...

Python does not check the cache in two circumstances. First, it always recompiles

What always recompiles? Does python always recompile or does the cache recompile? And why always, if at the top of the documentation it says that the module is recompiled only if the cache is out of date.
and does not store the result for the module that's loaded directly from the command line

What is meant by result?
Second, it does not check the cache if there is no source module. To support a non-source (compiled only) distribution, the compiled module must be in the source directory and there must not be a source module.

To support a compiled version where there is no source, the compiled version must be in the source folder (Which is missing???)?
Thanks for the explanation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-05-19
@PyLearner

First, it always recompiles and does not store the result for the module that's loaded directly from the command line.

This means that the script passed directly to the interpreter will always be recompiled. If you write python test.py, then it doesn't matter if there is a test.pyc. And if test.py is imported from another script or interactive interpreter session, the compiled version from the cache will be loaded.
If there is no source file, the cache in the __pycache__ directory will be ignored.
In order for the compiled version to be executed in the absence of the source, it is necessary that the pyc-file is not in __pycache__, but instead of the py-file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question