2

Consider that I have a package called "A" consisting of several modules and also nested packages. Now, I want to distribute this package to user and I do not want user to see my code at all. I heard that ".pyc" can be de-compiled. So, I am just wondering what could be the other alternatives for this problem.

It would be great if someone gives some ideas in this regard.

vaultah
  • 44,105
  • 12
  • 114
  • 143

2 Answers2

1

You actually have few options. First, you can compile your code into pyc files. However, this can be circumvented with the disassembler library dis, but this requires a lot of technical know-how. You can also use py2exe to package it as an exe file; this converts the pyc file into an exe file. This can still be disassembled but adds an extra layer. You also have a few encryption solutions; for example you can use pyconcrete to encrypt your imports until they are loaded into memory. You can also just encryption the entire application, then ship the decrypter and launcher with it as a C/C++ application (or any other compiled language). Lastly, if you are comfortable with getting python to run custom C/C++ code, you can also put your private code into a DLL or SO and call it directly for the script.

Daniel Davee
  • 756
  • 4
  • 3
-2

Python is an interpreted language. That means that if you want to distribute pyc files you'll have to have them run on the same OS/architecture as yours or you'll run into subtle problems. That, and the fact that most code can be decompiled to some degree, would urge me to rethink your use case.

Can you rethink your package as a service instead?

omu_negru
  • 4,642
  • 4
  • 27
  • 38
  • We thought about service option already, but we are not going with that for now because the required infrastructure is not in place. – PythonProgrammer Jun 14 '17 at 10:54
  • 2
    @omu_negru Could you give a reference to `pyc` files not being cross platform? Pretty sure [that's not correct](https://mail.python.org/pipermail/python-list/2008-November/467417.html). – Peter Wood Jun 16 '17 at 13:37
  • @PeterWood I know for a fact that minor version bytecode compatibility is not guaranteed , so a pyc compiled in 3.5 would fail in 3.6. That does not mean it's missing all togethere, but it's not present in the same way it is for source code files – omu_negru Jun 16 '17 at 16:21
  • It's not OS/architecture dependent though, only VM dependent. – Peter Wood Jun 16 '17 at 16:52
  • Encryption is always a solution. – Daniel Davee Jun 24 '21 at 00:18