Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to minify python code like javascript?

Tags:

python

minify

Python is a scripting language. It is hard to protect python code from being copied. No 100% protection is required but at least slow down those who have bad intentions. Is it possible to minify/uglify python code the way javascript front-end code is being done today?

EDIT: The python code will be used in Raspberry Pi, not server. On raspberry pi, anyone can take out the SDcard and gain access to the python code.

like image 455
guagay_wk Avatar asked Sep 08 '18 01:09

guagay_wk


3 Answers

python is executed server-side. while sometimes it's fun to intentionally obfuscate code (look into perl obfuscation ;), it should never be necessary for server-side code.

if you're trying to hide your python from someone but they already have access to the directories and files it is stored in, you have bigger problems than code obfuscation.

like image 180
derelict Avatar answered Sep 25 '22 22:09

derelict


  1. What about starting off with only distributing the pyc files? These are files created by Python interpreter for performance reasons--their load times are faster than .pys--but to the casual user they are difficult to decipher.
python -m compileall .
  1. Ramp up the security by using Cython to compile your python src. To "cythonize" your code, run Cython + GCC on each module. The init.py files must be left intact to keep module imports working. A silly Hello world example:
$ cython helloworld.py -o helloworld.c
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python3.7 -o helloworld.so helloworld.c

YMMV using this approach; I've run into various gotchas using different modules.

like image 22
gregory Avatar answered Sep 26 '22 22:09

gregory


I will answer my own question.

I found the following software tools that can do the job. I have not tried them, so I cannot comment on how effective they are. Comments are welcomed on their effectiveness.

https://liftoff.github.io/pyminifier/

https://mnfy.readthedocs.io/en/latest/

like image 25
guagay_wk Avatar answered Sep 24 '22 22:09

guagay_wk