How does python interpreter run the code line by line in the following code?
<p>I have read that the interpreter runs the code line by line and reports the error if any at the same time and stops the further execution.
So in python, consider the file <code>ex1.py</code>,</p>

<pre><code>print "Hello world"
12variable = 'bye'
print 12variable
</code></pre>

<p>Now according to the working of interpreter, the interpreter would run the first line i.e. it prints hello world first and then show the syntax error in the next line (line-by-line working). Hence the expected output is:</p>

<pre><code>Hello world
12variable = 'bye'
         ^
SyntaxError: invalid syntax
</code></pre>

<p>But the actual output is -</p>

<pre><code>12variable = 'bye'
         ^
SyntaxError: invalid syntax
</code></pre>

<p>Why it is not printing <code>Hello World</code> at the first?</p>

<p>It's a myth that Python is a fully interpreted language. When CPython runs a script the source code is parsed (this is where it will catch syntax errors), and compiled into bytecode (sometimes these are cached in your directory as <code>.pyc</code> files) before anything is executed. In this regard Python is not all that fundamentally different than Java or C# other than that it doesn't spend much time doing any optimizations, and I believe the bytecode is interpreted one instruction at a time, instead of being JITed to machine code (unless you're using something like PyPy).</p>

python syntax-error interpreter