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>Because your understanding of the interpreter is faulty. While it is possible for the behaviour you are describing to occur for a subset of errors it is not the common case for many (most?) errors.</p>

<p>If the interpreter can construct what it thinks is a valid program but there is an error at run time then what you are describing will happen.</p>

<p>Since the case you are pointing at is a syntax error that prevents a valid program being constructed the behaviour is as you see it.</p>

python syntax-error interpreter