1

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?

3
  • 1
    py2exe always embeds a full Python interpreter into the generated binary, 100% of the time. That's how it works. Commented Nov 16, 2016 at 19:11
  • You might want to read something like this to learn about compiling and interpreting... (just don't worry about Java for now, it's complicated). Commented Nov 16, 2016 at 19:13
  • 1
    Nuitka nuitka.net/pages/overview.html is probably closer to creating an executable from Python in the way that you mean. It generates C++ which is then compiled and linked. The source for handling eval is here: pydoc.net/Python/Nuitka/0.5.14.2/nuitka.nodes.ExecEvalNodes Commented Nov 16, 2016 at 19:20

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Does this mean it is possible to obtain the python script from the executable easily?
Yes. If you're using these tools as obfuscators... well, they never claimed to be good for the purpose, and aren't. (Or, rather, you can obtain Python bytecode easily, and Python bytecode dissassembles very cleanly).
Basically it is not possible to compile a python script to a native executable. You could do something like compiling using Cython (cython.org and github.com/cython/cython/wiki/…) but that's not really a beginner path :-/
@HelloWorld, ...really, you can't make reverse engineering impossible; all you can do is make it more difficult. Perhaps you make it difficult enough that you couldn't or wouldn't do it yourself, or that your opposition would rather bribe (or spearphish) one of your employees instead, but none of those are the same thing as "impossible" at all. The place to start is figuring out what your threat model is, how much money/time/resources your opponents are willing to spend and how much money/time/resources it's worth you spending to stop them. Often, opposing RE is simply not worth the effort.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.