r/pythontips • • Jul 17 '21

Module How to encrypt my python script?

I have 1 python script. I have to give this script to 3rd party client but don't want to expose code to them.

how can I do this? want something like .exe files only executables.

33 Upvotes

19 comments sorted by

5

u/big-blue-falafel Jul 17 '21

PyOxidizer uses Rust to package Python code into a target executable: https://github.com/indygreg/PyOxidizer I don’t know if this meets your bar but I have used this a few times and it came in handy. What are your main concerns about this client? I liked the licensing approach someone else said. I found a suggestion in their GitHub issues to “encrypt” which references pyinstaller’s capability for this https://github.com/indygreg/PyOxidizer/issues/265

2

u/prONoOB1004 Jul 18 '21

our concern is we don't want to expose our code to clients.

2

u/big-blue-falafel Jul 18 '21

Fair enough. If you know enough about their execution environment, creating an executable is probably the right balance between obfuscation and convenience. If it's something that doesn't have to run on their systems directly you may consider making a service where the secret sauce runs on your server and client-side code just presents the info.

1

u/prONoOB1004 Jul 19 '21

got it thanks.

43

u/spez_edits_thedonald Jul 17 '21

in general, what you want is not how python works... you could imagine having closed-source C code that you compile and then distribute an executable binary but not the source. (even in this case, advanced users could try and reverse-engineer the code using a decompiler)... whereas python is interpreted so what you distribute is the source code itself.

There might be some hack that is good enough, as other users suggested.

But the "right" way to do closed-source python would be to not send them your program, but put a server (or AWS lambda function etc) in the cloud and have users provide input to get output without ever running the code themselves.

-13

u/[deleted] Jul 17 '21

or just use pyinstaller to create an executable and drop the bullshit

7

u/HomeGrownCoder Jul 17 '21

Way easy to reverse that back to the script source code…

-8

u/[deleted] Jul 17 '21

Then not use python at all, least solution is to use a c wrapper to obfuscate the code but op will probably never even try to use it. Not all problems have solutions

5

u/HomeGrownCoder Jul 17 '21 edited Jul 19 '21

We have given him options, and it is up to him to decide what is best.

Not all solutions are binary so obfuscation would help, but legal and his contract with them or also needed.

9

u/saltyhasp Jul 17 '21

You may want to think more about the licensing model and the legal and contractual relationship instead.

3

u/fjosmjdifjfndush Jul 17 '21

I mean any idiot with 10 mins can read that code, but at least its not plopped in front of then

3

u/testforredditbythe Jul 17 '21

This might be what you are looking for: compile python But I don’t think it’s particularly great for your use case.

3

u/sidro2018 Jul 17 '21

Why you don't compile your code in byecode? how to

3

u/HomeGrownCoder Jul 17 '21

License agreement and contract with your client would be your best bet. You can try to compile it as another later. But that won’t stop anyone who is motivated to steal it.

But legally they could be held liable, which is why the first two are important.

3

u/ayeDaemon Jul 17 '21

I'll give a nuitka package a try. And then, Pyinstaller would be my second choice.

https://github.com/Nuitka/Nuitka

5

u/fjosmjdifjfndush Jul 17 '21

Try an exe builder, but all its doing is writing that file to a location somewhere then executing it

7

u/Mal_Dun Jul 17 '21

There is a tool called py installer: https://www.pyinstaller.org/

I compiles your script into an executable C program. Another way is using cython.

2

u/SloppyPuppy Jul 17 '21

if these are your needs, which are legitimate needs btw, python might not be the best choice of language.

python in its core is built around open source concept