Python Forum
Did interpreter 'compile' all import modules(from thrid-party) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Did interpreter 'compile' all import modules(from thrid-party) (/thread-30361.html)

Pages: 1 2


Did interpreter 'compile' all import modules(from thrid-party) - jamesyuan - Oct-17-2020

Did interpreter 'compiles' all import modules(installed from third-party by pip) when a programme is running? I edited the code from site packages path, I found the editions reflect in the programme. However, all packages installed by pip should be binary code, I did not re make/build, why these editions were activated.


RE: Did interpreter 'compile' all import modules(from thrid-party) - bowlofred - Oct-17-2020

No, stuff installed by pip does not have to be binary. Compiled C code is possible, but so is just regular python code.


RE: Did interpreter 'compile' all import modules(from thrid-party) - Gribouillis - Oct-17-2020

Pure python modules are compiled into bytecode modules (.pyc files) when they are imported. Python loads the bytecode directly if the .pyc file is newer than the .py file. In your case, the next time you imported the module after your edits, it was "recompiled" and a new .pyc file was written.


RE: Did interpreter 'compile' all import modules(from thrid-party) - jamesyuan - Oct-17-2020

(Oct-17-2020, 05:40 PM)bowlofred Wrote: No, stuff installed by pip does not have to be binary. Compiled C code is possible, but so is just regular python code.

Hi, thank you very much. Does compiled C means the code is written in Python and compiled by C compiler.


RE: Did interpreter 'compile' all import modules(from thrid-party) - jamesyuan - Oct-17-2020

(Oct-17-2020, 06:11 PM)Gribouillis Wrote: Pure python modules are compiled into bytecode modules (.pyc files) when they are imported. Python loads the bytecode directly if the .pyc file is newer than the .py file. In your case, the next time you imported the module after your edits, it was "recompiled" and a new .pyc file was written.

So much appreciation!

May I ask a further question? If the installed modules are binary, does interpreter also need to convert it to bytecode at first when I import them?


RE: Did interpreter 'compile' all import modules(from thrid-party) - Gribouillis - Oct-18-2020

jamesyuan Wrote:If the installed modules are binary, does interpreter also need to convert it to bytecode at first when I import them?
I don't understand what you mean by 'binary'. If (C extension) modules are distributed as shared libraries (.dll or .so), they are not converted to python bytecode. You could also have compiled .pyc file on the module path without their python source file. They would be imported as well.
jamesyan Wrote:Does compiled C means the code is written in Python and compiled by C compiler.
No, standard python code cannot be compiled by a C compiler.


RE: Did interpreter 'compile' all import modules(from thrid-party) - jamesyuan - Oct-18-2020

(Oct-18-2020, 07:09 AM)Gribouillis Wrote:
jamesyuan Wrote:If the installed modules are binary, does interpreter also need to convert it to bytecode at first when I import them?
I don't understand what you mean by 'binary'. If (C extension) modules are distributed as shared libraries (.dll or .so), they are not converted to python bytecode. You could also have compiled .pyc file on the module path without their python source file. They would be imported as well.
jamesyan Wrote:Does compiled C means the code is written in Python and compiled by C compiler.
No, standard python code cannot be compiled by a C compiler.

Thanks a lot, so, in general, all code(written by yourself and from third-party) will be converted into bytecode by interpreter, and then executed in virtual machine, right?


RE: Did interpreter 'compile' all import modules(from thrid-party) - Gribouillis - Oct-18-2020

Exactly.


RE: Did interpreter 'compile' all import modules(from thrid-party) - jamesyuan - Oct-18-2020

(Oct-18-2020, 02:17 PM)Gribouillis Wrote: Exactly.

Big Grin Big Grin Big Grin Thanks a lot. .pyd(DLL) file could be imported directly, so it means the modules imported will be converted to byte code for VM, or pyd file consists of byte code. As Python interpreter does has Linker like C language, how and when dll files are used.


RE: Did interpreter 'compile' all import modules(from thrid-party) - bowlofred - Oct-19-2020

Python source code (normally found in a .py file) is parsed and turned into bytecode by the interpreter. Then the python virtual machine executes the bytecode.

If this python code is a module (not a script), then the bytecode step is saved as a .pyc file. Future runs can load the pre-compiled bytecode for faster startup (but not faster execution).

If you want to import code from another language (like C/C++ or whatever) into a python project, you can do so. This will normally require compile to machine code and create a .so or .pyd file. These are not python bytecode and are not run by the python virtual machine, but called out to like any other shared library or DLL.

These binary extensions tend to be avoided when possible because the code is hardware or OS specific and can't be run by any python interpreter, but might be necessary for speed or for access to existing (non-python) code.