5

I am wondering if Python code can be compressed/non-human readable like Javascript can. I know indentions are important in Python, and it's core philosophy is readability, but is there, by any chance, a method that allows compressed Python code? Python 2.7, by the way.

Jace Cotton
  • 2,004
  • 1
  • 21
  • 38
  • (Nested) list comprehensions / generator expressions. Here's more: http://c2.com/cgi/wiki?ObfuscatedPython – kren470 Oct 28 '13 at 02:52
  • 1
    What's the purpose of compression? – Leonardo.Z Oct 28 '13 at 02:54
  • possible duplicate of [Python Code Obfuscation](http://stackoverflow.com/questions/576963/python-code-obfuscation) – falsetru Oct 28 '13 at 02:54
  • @Leonardo.Z I want my code to be non-readable. Additionally, I'd like to ship it as an application, and I'd prefer it if I could separate the "development" version (uncompressed, for developers) and the actual shipping version (compressed, for users.) – Jace Cotton Oct 28 '13 at 02:54
  • I'd suggest just shipping the bytecode, `.pyc/.pyo`. also read http://stackoverflow.com/questions/261638/how-do-i-protect-python-code – monkut Oct 28 '13 at 03:09
  • Interesting question, but I too wonder at the purpose of compression. JavaScript compression is common because of download considerations. A few dozen kilobytes matters. AFAIK, Python does not make people mad because of slow download times. If it is obfuscation you are going for, that is a little different than compression. – Paul Draper Oct 28 '13 at 03:54
  • If you want to compress your code,, you use `tar`, or `zip` – Paco Oct 28 '13 at 04:06
  • @monkut that doesn't compress or obfuscate python. It only slightly reduces the size of files and makes reverse-engineering almost trivial. – reem Oct 28 '13 at 04:08

3 Answers3

4

There are a few different approaches you can take here.

Python does in fact offer opportunities for minification in the form of ;. A little known and little used (thankfully) syntactical element in python is that multiple expressions can be written on one line if ; is used.

For instance:

 if x > y:
     x += y
     print y
 else:
     y += x
     print x

Could be compressed to:

 if x>y:x+=y;print y;
 else:y+=x;print x;

There are tons of examples of this, you can also remove comments, and obfuscate simple code further with ternary operators. The ; gets rid of a ton of whitespace, but there are lots of smaller optimizations that can be made.

For some examples of libraries that do this for you, see here, here, or here.

Keep in mind that none of these are going to completely obfuscate your code to the point that it can't be recovered. Any sufficiently motivated person will be able to reverse-engineer your source code, even if undocumented, from this.

This remains true even if you only distribute .pyo or .pyc files. Python bytecode is usually not much smaller than the source code used to generate it, and can for the most part be easily reverse-engineered. Do not make the mistake of thinking that distributing a .pyc will completely secure your source code.

EDIT: If you are looking to package these as standalone binary executables, you might want to take a look at cx_Freeze. It could be closer to what you are looking for and much harder if not impossible to reverse-engineer.

reem
  • 7,186
  • 4
  • 20
  • 30
0

http://doughellmann.com/2009/01/pymotw-compileall.html the delete then source .py files

dartdog
  • 10,432
  • 21
  • 72
  • 121
  • This does not obfuscate code at all (python can be easily reverse-engineered from byte code) and does not save all that much space. For instance, compiling the `Decimal` library from the stdlib only reduces the amount of space it takes by 5%. – reem Oct 28 '13 at 04:06
0

Unlike javascript which is usually executed by browsers and we must provide raw javascript source code.

You don't have to ship python codes, you can ship compiled binary files. There are many tool to do the work e.g. py2exe, Pyinstaller, cx_Freeze. Clients also benefit because they don't need to install Python or even know anything about Python.

Though the compiled Python code are easier to be decompiled comparing to java, c, c++ codes, but better than compressed source code.

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38