简体   繁体   English

Python 执行流程

[英]Python Execution flow

I just need to know How the python executes its code.我只需要知道 python 如何执行其代码。

I am not asking the code flow instead of how it converts the python code to the machine code that runs on the CPU.我不是在问代码流,而是问它如何将 python 代码转换为在 CPU 上运行的机器代码。

While converting code to machine code, is it uses any compiler?在将代码转换为机器代码时,它是否使用任何编译器?

Because it saves.pyc file but Python is an interpreter.因为它保存.pyc 文件,但 Python 是解释器。

The interpreter is different from the compiler.解释器不同于编译器。

The interpreter also converts the high level language code to the machine code.解释器还将高级语言代码转换为机器代码。

What is the reason for the python uses the compiler? python使用编译器的原因是什么?

After creating the.pyc file then, how and, when it will use this compiled file.在创建 .pyc 文件之后,它将如何以及何时使用此编译文件。

Please do not mark this question as duplicate... I searched for the answer but so many answers say only about the control flow of the program请不要将此问题标记为重复...我搜索了答案,但很多答案仅说明程序的控制流

Hope this helps.希望这可以帮助。

Step 1: The interpreter reads a python code or instruction.第 1 步:解释器读取 python 代码或指令。 Then it verifies that the instruction is well formatted, ie it checks the syntax of each line.If it encounters any error, it immediately halts the translation and shows an error message.然后它验证指令的格式是否正确,即检查每一行的语法。如果遇到任何错误,它会立即停止翻译并显示错误消息。

Step 2: If there is no error, ie if the python instruction or code is well formatted then the interpreter translates it into its equivalent form in intermediate language called “Byte code”.Thus, after successful execution of Python script or code, it is completely translated into Byte code.步骤2:如果没有错误,即如果python指令或代码格式正确,则解释器将其翻译成称为“字节码”的中间语言的等效形式。因此,在成功执行Python脚本或代码后,它是完全翻译成字节码。

Step 3: Byte code is sent to the Python Virtual Machine(PVM).Here again the byte code is executed on PVM.步骤 3:字节码被发送到 Python 虚拟机(PVM)。这里再次在 PVM 上执行字节码。 If an error occurs during this execution then the execution is halted with an error message.如果在此执行期间发生错误,则执行将停止并显示错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM