I have a python script. It starts with #!/usr/bin/env python Is there a way I can compile it, and protect it's contents? I've used shc for bash scripts, and before anyone chimes in with a moan, I know it's not a secure way to protect the contents of the script.

I was hoping there was a secure way to compile a python script and completely stop anyone from seeing the original code.

If not, then maybe something like shc would do, to stop the ill informed from snooping.

Hope that makes sense.


closed as off-topic by Byte Commander, user68186, mikewhatever, karel, muru Jan 28 '18 at 1:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Byte Commander, user68186, mikewhatever, karel, muru
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    This doesn't seem to be about Ubuntu, but a general programming question. As such, it is off-topic here and should instead rather be asked on e.g. Stack Overflow. However, I think there is already a post that fully covers your question: stackoverflow.com/q/261638/4464570 – Byte Commander Jan 27 '18 at 21:33
  • :( Sorry, will do that – Preston Cole Jan 27 '18 at 21:37
  • Keep in mind almost anything compiled and be decompiled. Also when compiled with debug / trace options it is even easier to decipher when decompiled. – WinEunuuchs2Unix Jan 28 '18 at 0:08

I haven't try it myself, but I know there's a thing called cx_Freeze. From its description:

cx_Freeze is a set of scripts and modules for freezing Python scripts into executables

I'm not sure how easy or hard it is to unpack executable and actually see the script source in it, so security of this approach is debatable.


I have used one Python compiler during initramfs to speed up boot time for EnhanceIO SSD to HDD block device cacher. It worked well back in 2016 but I don't know how well it compiles today in current Python language.

The compiler used is called Pyinstaller and it:

is a program that freezes (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX. Its main advantages over similar tools are that PyInstaller works with Python 2.7 and 3.3—3.6, it builds smaller executables thanks to transparent compression, it is fully multi-platform, and use the OS support to load the dynamic libraries, thus ensuring full compatibility.

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