HelloWorld HelloWorld - 24 days ago 8
Python Question

Does a python code with the function exec in it get compiled to an executable?

I am new to python and I have never compiled python code to an executable file before. The languages I am well familiar with are C, C++, and Java and I have never come across a language that lets you modify the code from within itself, like Python which uses has the method

exec
.

For the following code,

a = 500
code = raw_input()
exec (code)


When I give the input as,
print (a)
the program displays the value in
a
. So this means the variable
a
comes within the scope of the code
.

I don't understand what would happen if we try to convert the python code to an executable using a program like
py2exe
. Will the method
exec
still work? If it does work, does
py2exe
bring the entire Python compiler and interpreter with it when the program gets compiled?

Answer

py2exe never compiles Python code into native executables; it bundles up a Python interpreter into an executable, always. This is likewise true of freeze, cx_Freeze and every other tool offering similar functionality while supporting the full Python language rather than a limited subset thereof.

Thus, exec, eval and similar constructs are available without needing additional facilities.