Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to offer a single executable file without showing the code in 2020

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)

like image 415
Federico Dorato Avatar asked Mar 31 '20 08:03

Federico Dorato


2 Answers

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.

like image 80
Keyur Khant Avatar answered Sep 22 '22 13:09

Keyur Khant


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.

like image 28
Muhammad Moiz Ahmed Avatar answered Sep 20 '22 13:09

Muhammad Moiz Ahmed