Python compiled file generated even though wrong syntax
<p>I wrote a small program 'test1.py'</p>

<pre><code>print abc
print 'the above is invalid'
</code></pre>

<p>Now i write a different python program 'test2.py'</p>

<pre><code>import test1
print 'this line will not get executed'
</code></pre>

<p>Q1:To my surprise,i can see test1.pyc file is successfully generated. Why?
Since test1.py conatins an invalid statement in first line, why at all is test1.pyc file getting generated? What exactly does the compiler check(is it the syntax or something else?).I am getting confused.Please clarify.</p>

<p>Q2: I also read that the compiled python file will be further interpreted.Is that true? </p>

<p>Q3:Compiler converts the program to machine code as a whole.This does not need to be further interpreted via an interpreter? Is it true? If so,then what about Question2?</p>

<p>Q4:I also read that compiled code is closer to the machine. When we use an interpreter,it converts the code into intermediate code which needs to be further converted to machine code.Is that correct? So, compiled code is closer to the machine than interpreted code?</p>

<p><h2>Q1:</h2>
Neither file contains any syntax errors. The module test1 can therefore successfully be compiled into instructions the interpreter can read. The *compiling however has no code introspection that can determine ahead of time whether a variable is defined at any given point or not. </p>

<p>*I like to think of this conversion more as translation than compilation, as it largely does not alter the code structure at all, but rather translates it into instructions that are easier for the interpreter to read. Compilation implies that code inspection is taking place beyond simple syntax checking. (Google translate will often give something grammatically correct, but it may or may not make any sense.</p>

<h2>Q2:</h2>

<p>The python interpreter understands what's called <a href="http://akaptur.com/blog/2013/11/17/introduction-to-the-python-interpreter-3/" rel="nofollow">bytecode</a>. It's functionally a program written in c that takes the *compiled code and executes it on the machine. For each variation of hardware you want to run your code on, a version of this program(specifically ceval.c) is compiled to work with that hardware (be it x86, arm, mips, etc...), and interprets the bytecode which is the same no matter what hardware you are running. This is what allows python (and many other interpreted languages) to be cross-platform</p>

<h2>Q3</h2>

<p>No this is not true. *compiled python code runs through the same interpreter normal code does. The benefit of *compiled python code is in loading time of modules. Before any python code is executed it is converted into bytecode then sent to the interpreter. With a script this is done each time, but when python imports a script as a module it saves a copy of the already parsed bytecode to save itself the trouble next time.</p>

<h2>Q4</h2>

<p>Your confusion here is likely due to the poor naming convention of python *compiled files. They are not truly compiled into machine instructions, so they must be executed by a program rather than on the hardware itself. True compilers are programs that translate and optimize code (c, c++, fortran etc..) and spit out actual binary hardware instructions as specified by the manufacturer. </p>

<p>I did my best to guess what you were confused about, but if you have any more questions feel free to ask..</p>

python python-2.7 compilation