In CPython, I think, import means compile the py file to a pyc file and execute the file in the current frame, and the next time CPython will load the pyc file directly without compiling again. What about import in ironpython? I guess ironpython don't have a pyc-like format. Does it compile every time when import?
When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.
The entire file becomes a module and can be imported inside another file to refer to the code. With module functionality, you can break your code into different files instead of writing everything inside one file. Later, using import, you can refer to the code inside the file you need.
Modules can define functions, classes, and variables that you can reference in other Python . py files or via the Python command line interpreter. In Python, modules are accessed by using the import statement.
Yes, IronPython recompiles the imported module on every run. Twice, actually.
It's complicated.
On the first pass, the Python code is parsed into an AST, the AST is converted into a DLR expression tree, and the expression tree is stored. When it is time to execute it, the expression tree is compiled into a set of instructions for a simple stack-based interpreter and the module code is executed on that interpreter. It's not fast when running, but it has very little start up time.
After a piece of code has run for a while, IronPython gets fed up with how slow it is, goes back to the expression tree, and recompiles the code into a .NET delegate. This means that it gets converted to MSIL and then native code by the .NET JIT. This code is fast to execute, but takes time to create.
This conversion is done a per-function (or even per-loop) basis, so that if you use one function from a module repeatedly and none of the rest, only the one commonly-used function will undergo full IL code generation and JITting.
None of this compilation is saved to disk, however. The pyc.py program included with IronPython can precompile the code, but it's not done automatically because the code generated at runtime is different than the code generated by pyc.py. The runtime code is usually collectible, while the code generated by pyc.py is not - and generating non-collectible code at runtime leads to memory leaks. pyc.py should make imports faster by saving a few steps, but I'm not sure by how much.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With