6

Stack Overflow has many questions about

  • How to give someone else a python script protecting the source code
  • How to compile python files
  • How to create packages and deploy the code

But I could not find the answer to my problem:

I want to give someone else my python script, without giving him the source code. My current attempt is compiling the file and giving away the .pyc file.

This is for sure not the best solution. Moreover, my code is made by different files. To offer a single executable pyc file, I put the code all together in a single file before compiling it: a true hell for a developer

How can I obtain my goal in a cleaner way?

Side-notes

I know .pyc files are not going to hide so much, but it is for sure better compared to giving .py files

Still, .pyc files can be incredibly problematic (as they can be system-dependant)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Federico Dorato
  • 710
  • 9
  • 27
  • 2
    The good question is *why do you want to hide the source code*? There are very few valid reasons. If you really have one, then you should use a tool that compiles everything into a true executable file (yes is includes the Python interpretor inside the executable). You lose all compatibility but the good point is that the end user is not forced to install Python (nor anything else than your product) to run you program. Common ones are `pyinstaller` or `cx_freeze`. More détails in https://stackoverflow.com/q/41570359/3545273 – Serge Ballesta Mar 31 '20 at 08:47
  • 1
    To answer "Why do you want to hide the source code" we should start a discussion on meta :P Still you are right. My end user already has Python installed, do we have other options in this case? – Federico Dorato Mar 31 '20 at 09:24
  • @SergeBallesta let the OP ask what he wants. whether or not this is the greatest idea is another question, but frankly, your objection's been done to death and this is a valid *technical* question regarding quick and dirty information hiding. even compiled languages have decompilers and you don't see people clamoring this. and I say this as someone very fond of open source. – JL Peyret Mar 31 '20 at 15:01
  • @JLPeyret: What I mean here is that many times the obfuscation techniques cost more (in time and sometimes in money) than their benefit. And that is the reason why I said that in a comment. And I have even given all the info that I knew about that... But my *opinion* is still that Python is probably not the best language if you want to hide your source code... – Serge Ballesta Mar 31 '20 at 15:20
  • @SergeBallesta against a determined, technically savvy, attacker, sure. against a casual snooper? probably does the job quite well. a reminder of the very real limitations of obfuscation would not be remiss. but that's an entirely different statement than *there are very few valid reasons* however. – JL Peyret Mar 31 '20 at 15:52
  • Answering to both of them, I've already seen posts stating the same opinion. In my case, the customer is not someone with an IT background. So I am not worried about python being not the best language for hiding source code or whatever – Federico Dorato Apr 01 '20 at 12:49

3 Answers3

1

using "pyinstaller sroucecode.py --onefile" command will generate an executable file on Windows. This can be a way should it be desired to ship the functionality but hide the code.

Muhammad Moiz Ahmed
  • 1,799
  • 1
  • 11
  • 9
1

You can create a .exe file using pyinstaller.

pip install pyinstaller

then, open terminal in your source code directory and use the command:

pyinstaller --onefile source.py

If you have database connection with python file then it can be added using:

pyinstaller --onefile --add-data 'database.db:.' source.py

Here, :. shows database.db is a source data file and it will copy on the top level of your python application.

-1

There is tool, which might help you to achieve described outcome, but only if destination machine is able to run .exe files.