<p>Does it:</p>
<ol>
<li>first compile all the code into bytecode, and only then executes it,</li>
<li>or does it compile the minimum possible piece of code and executes immediately?</li>
</ol>
<p>For example, this code:</p>
<pre class="lang-python prettyprint-override"><code>a = 1
b = 2
c = a + b
</code></pre>
<p>Does CPython compiles the entire thing into this bytecode:</p>
<pre class="lang-none prettyprint-override"><code>  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (a)

  2           4 LOAD_CONST               1 (2)
              6 STORE_NAME               1 (b)

  3           8 LOAD_NAME                0 (a)
             10 LOAD_NAME                1 (b)
             12 BINARY_ADD
             14 STORE_NAME               2 (c)
             16 LOAD_CONST               2 (None)
             18 RETURN_VALUE
</code></pre>
<p>and then executes it,</p>
<p>or</p>
<p>does it take one minimal piece of code at a time (<code>a = 1</code>, then <code>b = 2</code>, then <code>c = a + b</code>) and compiles it code and then executes, before proceeding to the next piece, for example:</p>
<pre class="lang-python prettyprint-override"><code>a = 1
</code></pre>
<p>Into</p>
<pre class="lang-none prettyprint-override"><code>  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (a)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE
</code></pre>
<p>which is then executed?</p>
<p><strong>Does CPython compile the entire code or one piece at a time?</strong></p>

<interpreters><python>
Does CPython compile the entire code or one piece at a time?
CC BY-SA 4.0
2023-05-23T08:03:33.023Z
Hi there, as it stands, this post is extremely broad and doesn't focus on a single question. Please edit the question to clarify what it is you specifically want to know about the python interpreter and make sure it's objectively answerable.
lyxal
CC BY-SA 4.0
2023-05-23T09:33:25.487Z
The question seems to be better phrased as "is the CPython bytecode interpreter eager or lazy in compiling to bytecode?". That's probably an on-topic question but the answer is very short: it's eager, whole source files are compiled to bytecode when they are loaded. A better question might be along the lines of *"how could an interpreter lazily compile source code to bytecode?"* mentioning Python as an example of the kind of language you're asking about. This would allow for more informative answers than just yes/no.
kaya3
CC BY-SA 4.0
2023-05-23T09:34:37.700Z
That said, compiling to bytecode is fast, especially when the bytecode is specifically designed for the language to be compiled to, so there is unlikely to be any performance reason to do it lazily.
kaya3
<p>Python will always compiles to bytecode before executing. You can view the bytecode of some function using the <code>dis</code> standard library method:</p>
<pre class="lang-py prettyprint-override"><code>def f():
    print(&quot;Hello World&quot;)

import dis
dis.dis(f)
&quot;&quot;&quot;
Output:

  1           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello World')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
&quot;&quot;&quot;
</code></pre>
<p>A module is compiled when it is imported, which is not nececairly at the start of execution if it gets imported later.</p>
<p>The compiled bytecode for every module except the main one is stored as a <code>.pyc</code> file. If a <code>.pyc</code> file exists that is newer than the file it will be used and the file will not be compiled again. The main module will never be cached.</p>
<p>It is possible to import a <code>.pyc</code> or a <code>.pyo</code> file directly even if the original module doesn't exist. This is sometimes used to &quot;closed source&quot; python modules but these files can be quite trivially decompiled.</p>

CC BY-SA 4.0
2023-05-23T08:11:47.243Z
2023-05-23T08:11:47.243Z
mousetail 'he-him'
CC BY-SA 4.0
2023-05-25T08:28:00.533Z
@NickolayChistov I don't understand what you are saying
mousetail 'he-him'
CC BY-SA 4.0
2023-05-25T08:35:44.970Z
Sorry, I not good at English.
чистов_n
CC BY-SA 4.0
2023-05-25T08:42:10.033Z
I think your answer is incorrect. If i wrote code it works... but if I add it into a function, it works about one and a half times faster! Why? Because the function is a minimal piece of code, and it compiles one time and executes, but a code compiles by line and executes. It slower.
чистов_n
CC BY-SA 4.0
2023-05-25T08:43:38.927Z
@NickolayChistov That's probably just because it's using global loads instead of local loads. It's compiling the entire thing at once in both cases.
mousetail 'he-him'
CC BY-SA 4.0
2023-05-25T08:47:56.610Z
Well... I accept this answer. Thank you.
чистов_n
# Does CPython compile the entire code or one piece at a time?
Posted by: username_0 on 2023-05-23
Does it:

first compile all the code into bytecode, and only then executes it,
or does it compile the minimum possible piece of code and executes immediately?

For example, this code:
a = 1
b = 2
c = a + b

Does CPython compiles the entire thing into this bytecode:
  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (a)

  2           4 LOAD_CONST               1 (2)
              6 STORE_NAME               1 (b)

  3           8 LOAD_NAME                0 (a)
             10 LOAD_NAME                1 (b)
             12 BINARY_ADD
             14 STORE_NAME               2 (c)
             16 LOAD_CONST               2 (None)
             18 RETURN_VALUE

and then executes it,
or
does it take one minimal piece of code at a time (a = 1, then b = 2, then c = a + b) and compiles it code and then executes, before proceeding to the next piece, for example:
a = 1

Into
  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (a)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE

which is then executed?
Does CPython compile the entire code or one piece at a time?
**Tags:** interpreters python
> **Comments:**
> **username_1**: Hi there, as it stands, this post is extremely broad and doesn't focus on a single question. Please edit the question to clarify what it is you specifically want to know about the python interpreter and make sure it's objectively answerable.  
> **username_2**: The question seems to be better phrased as "is the CPython bytecode interpreter eager or lazy in compiling to bytecode?". That's probably an on-topic question but the answer is very short: it's eager, whole source files are compiled to bytecode when they are loaded. A better question might be along the lines of *"how could an interpreter lazily compile source code to bytecode?"* mentioning Python as an example of the kind of language you're asking about. This would allow for more informative answers than just yes/no.  
> **username_2**: That said, compiling to bytecode is fast, especially when the bytecode is specifically designed for the language to be compiled to, so there is unlikely to be any performance reason to do it lazily.  
---
## 1 answers
### Answer by username_3 on 2023-05-23. Score: 2 | Accepted answer
Python will always compiles to bytecode before executing. You can view the bytecode of some function using the dis standard library method:
def f():
    print("Hello World")

import dis
dis.dis(f)
"""
Output:

  1           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello World')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
"""

A module is compiled when it is imported, which is not nececairly at the start of execution if it gets imported later.
The compiled bytecode for every module except the main one is stored as a .pyc file. If a .pyc file exists that is newer than the file it will be used and the file will not be compiled again. The main module will never be cached.
It is possible to import a .pyc or a .pyo file directly even if the original module doesn't exist. This is sometimes used to "closed source" python modules but these files can be quite trivially decompiled.
> **Comments:**
> **username_3** (2023-05-25): @NickolayChistov I don't understand what you are saying  
> **username_0** (2023-05-25): Sorry, I not good at English.  
> **username_0** (2023-05-25): I think your answer is incorrect. If i wrote code it works... but if I add it into a function, it works about one and a half times faster! Why? Because the function is a minimal piece of code, and it compiles one time and executes, but a code compiles by line and executes. It slower.  
> **username_3** (2023-05-25): @NickolayChistov That's probably just because it's using global loads instead of local loads. It's compiling the entire thing at once in both cases.  
> **username_0** (2023-05-25): Well... I accept this answer. Thank you.  
---
