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 depends on how you run the Python interprÃ©ter. If you give it a full source file, it will first parse the whole file and convert it to bytecode before execution any instruction. But if you feed it line by line, it will parse and execute the code <em>bloc by bloc</em>:</p>

<ul>
<li><code>python script.py</code> : parse full file</li>
<li><code>python &lt; script.py</code> : parse and execute by bloc</li>
</ul>

<p>The latter is typically the way you use it interactively  or through a GUI shell like <code>idle</code>.</p>

python syntax-error interpreter