Solved

Compile Python Code

Posted on 2004-08-02
19
32,976 Views
Last Modified: 2011-09-20
I have been searching around for a way to compile my python scripts beyon .pyc or .pyo files mainly because my company would like to distribute some applications however those byte code files are EASILY decompiled by using the disutils module and by any other methods... Also above the fact that a lot of it is still readable in a text editor.

At any rate, I don't want an answer like say it in the EULA or something dumb like that. I'm looking specifically for something to do code obfuscation or compile it to bit code if that is at all possible.

Thanks
0
Comment
Question by:e-b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +7
19 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 11700948
How about py2exe?
http://starship.python.net/crew/theller/py2exe/

Regards
Friedrich
0
 

Author Comment

by:e-b
ID: 11700990
Well I am actually making a bundle for Mac but I believe the scripts for windows are still left in .py/.pyc format someone in the dlls or exe am I correct?
0
 
LVL 3

Expert Comment

by:pYrania
ID: 11701161
As you already mentioned, I'd obfuscate the code, just for the fun of it. :)


fibonacci = lambda x:map(lambda o:(map(lambda c:map(lambda l:
o.__setslice__(l[0],l[1],l[2]),([o[2]+3,o[2]+4,[o[0]]],[0,3,[o[1],
reduce(lambda x,o:x+o,o[:2]),o[2]+1]])),range(x)),o)[1],[[1,1,0]+
range(x)])[0][3:]

print fibonacci(20)
0
Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

 

Author Comment

by:e-b
ID: 11701181
pYrania: Did you have to write that yourself or is there a program to obfuscate your code out there somewhere?
0
 
LVL 3

Expert Comment

by:pYrania
ID: 11701191
you need to write it yourself, else you wouldn't have fun while doing so, right?
0
 

Author Comment

by:e-b
ID: 11701216
writting that above code looks hardly any fun. and if you can write it then why can't someone else read it right?
0
 
LVL 3

Expert Comment

by:pYrania
ID: 11701245
but it's much more pain to fiddle with the obfuscated code than with plain python.
0
 
LVL 5

Accepted Solution

by:
EOL earned 32 total points
ID: 11701464
How about pyrex ( http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ ) ?

It will code-changes, but then you can compile your python source to C-code. compile it with a c-compiler, and import it as a module back into python. Should be much harder to read, and on top of that it's much faster then native python.
0
 
LVL 4

Expert Comment

by:szacks
ID: 11703236
If you need it compiled and translated into machine code like a traditional computer language, I think you should stay away from Python.

Does your code contain trade secrets or special algorithms developed by your company? We discovered that it did not hurt our interests at all actually giving the source code to the clients. They are really afraid to touch it anyways, and if it got out they would get sued. They appreciate having it, though in case our company somehow disappeared they could hire someone to maintain their system.
0
 
LVL 5

Expert Comment

by:EOL
ID: 11703428
Hm, there's recent effords to port python to .Net. As far as I know IronPython is a python-interpreter written in C# that can compile python to .Net bytecode, and makes some .Net libraries accessible. Except math nothing from the python standard library is ported tough. Also you could decide to use jython, and compile your java-app to java-bytecode ( which is as nice to read as python bytecode, but ohwell... )
0
 

Author Comment

by:e-b
ID: 11708179
IronPython is still Alpha
0
 
LVL 5

Expert Comment

by:EOL
ID: 11708593
That's true, and I don't think that's at the moment particulary good to use, but hopefully this will change pretty soon.
0
 
LVL 2

Assisted Solution

by:astrand
astrand earned 31 total points
ID: 11877026
pyobfuscate is written exactly for this purpose. Check out http://www.lysator.liu.se/~astrand/projects/pyobfuscate/.
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 31 total points
ID: 12005733
Hi,

If you really need to compile to complete native code, the following should work:

Using jython (http://www.jython.org/) (a java Python implementation) you can generate standard Java Class files for the python.
Using GCJ (http://gcc.gnu.org/java/) (the GCC Java Native compiler) you can compile Java Class files to native object/executables.

If you set up the two steps above correctly, it should be possible to get 100% compiled native applications from Python, although it may require 1) the GCJ library(s), and also possibly compilation of the jython Jar file(s) (GCJ applications can load normal classpaths as any normal java application running under JVM, but this will be slower than just running the Python code as Python... so I'd suggest looking at compilation of Jython libraries to native library for inclusion in compilation/linkage)
0
 
LVL 14

Assisted Solution

by:RichieHindle
RichieHindle earned 31 total points
ID: 12275051
Here's an idea that would make decompilation a lot harder.  Make your own build of Python (which is very easy) and swap around some of the opcodes in include/opcode.h.  For instance, change this:

#define LOAD_CONST      100      /* Index in const list */
#define LOAD_NAME      101      /* Index in name list */

to this:

#define LOAD_CONST      101      /* Index in const list */
#define LOAD_NAME      100      /* Index in name list */

and make the equivalent changes to lib/opcode.py

Now bundle your interpreter up with your software as normal, and although the pyo's will be included, they won't be decompilable.
0
 

Expert Comment

by:bergel
ID: 13931629

The McMillan Installer will produce a single .exe file in windows. This is probably the easiest solution to the issue of distribution and hiding the code.
0
 
LVL 1

Expert Comment

by:codecaine
ID: 24732759
There is a program called Jython. You can compile programs to java jar file. You can also code in python and java :).
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Use of python .format to prefill  form. 5 74
merging 2 mysql databases with java 7 74
What is Python programming? 3 137
Python Script to return all instances in all regions 4 100
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question