I have an application in Python compiled with py2exe. I have successfully extracted python scripts using Py2ExeDumper converted to .py using Easy Python Decompiler.

I made some modification to the python code and recompiled .py files to .pyc files.

Question : How can I rebuild the exe file using the new edited .pyc files ?


2 Answers 2


Instead of trying to rebuild the EXE from the .pyc files, you can just use py2exe to build a new EXE from the modified .py files.

Alternatively, if you really want to replace the library in the original EXE, you would need to extract the EXE's PYTHONSCRIPT resource with a resource editor, un-marshal the array of code objects, replace the code object of your choosing in the array, re-marshal the array of code objects, and replace the PYTHONSCRIPT resource data with this new marshaled stream. You can look at _create_script_data in http://sourceforge.net/p/py2exe/svn/HEAD/tree/trunk/py2exe-3/py2exe/runtime.py for more details.

  • This project is very big and have a lot of .pyc files. That's the reason why i am searching to replace the entire library in exe. There is a way to make setup.py for py2exe automatically from directory?
    – itseeder
    Commented Dec 4, 2014 at 17:48
  • This point it's not clear: "replace the code object of your choosing in the array, re-marshal the array of code objects" can you explain more detailed please?
    – itseeder
    Commented Dec 4, 2014 at 22:12
  • Questions related to writing Python code to handle arrays and marshal data should be asked on stackoverflow.com Commented Dec 4, 2014 at 23:17

A py2exe generated executable basically consists of three main parts - PYTHONSCRIPT, the runtime python dll & library.zip.

PYTHONSCRIPT can be modified with a decent resource editor. It is basically a marshalled array holding pyc files. The main script of the application is usually stored here.

In order to make changes you need to unmarshal the code objects, decompile it to py, make changes, recompile back to pyc, marshal it back to generate a new PYTHONSCRIPT and finally update the executable.

Modifying library.zip is easier as it is a standard zip file. It contains other pyc/pyo files. You can decompile them, make changes, recompile it back to pyc/pyo, and zip them up. Next replace the overlay in the executable with your new zip file and you are done.

In order to automate some of the above steps you can use a tool Py2Exe Binary Editor
Note: I am the author of the above tool

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.