M HYPE SPLASH
// news

Can I compile (protect) a python script? [closed]

By Andrew Adams

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.

Preston

3

2 Answers

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.

0

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.