Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to protect Python source code?

Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?

like image 354
Nathan Osman Avatar asked Jan 13 '10 08:01

Nathan Osman


2 Answers

I did it by creating .py library and simple .py program that uses that library. Then I compiled library to .pyc and distributed: program as .py source and library as compiled .pyc.

like image 57
Michał Niklas Avatar answered Sep 27 '22 16:09

Michał Niklas


Use the freeze tool, which is included in the Python source tree as Tools/freeze. It converts Python byte code to C arrays; with a C compiler you can embed all your modules into a new program, which is then linked with the standard Python modules.

Note that freeze requires a C compiler.

Other utilities:

1- PyInstaller

2- Py2Exe

3- Squeeze

4- cx_freeze

more info on effbot.org

like image 25
Michel Gokan Khan Avatar answered Sep 27 '22 17:09

Michel Gokan Khan