1

Assume this is the code. It accepts a CSV file, does its thing, and gives some output. Called this way:

python test.py input.csv

import numpy as np
from sklearn.svm import SVC
import sys

filename = sys.argv[-1]

val = np.genfromtxt(filename,delimiter=',')
target = val[:, 4]
features = val[:, [0,1,2,3]]

clf = SVC()
clf.fit(features, target)

print clf.predict([[6.3,2.9,5.6,1.8]])

Now I want to create a standalone executable which can run on any machine without any of the prerequisites installed (Python 2.7, numpy, sklearn, etc).

I used PyInstaller on Ubuntu 16.04 to create an executable:

pyinstaller --onefile --hidden-import sklearn.neighbors.typedefs test.py

I can use the generated executable (size ~ 55 MB) on any Ubuntu machine without any prerequisites installed:

./test input.csv

It gives identical output. So part of the problem is solved. However the documentation: https://pythonhosted.org/PyInstaller/operating-mode.html#hiding-the-source-code

states that the source code cannot be protected and suggests Cython as a remedy. I came across this StackOverflow question Cython standalone executable on ubuntu

which delineates the usage of --embed option to include the Python interpreter. But how do I embed sklearn, numpy, etc with Cython to create a truly standalone application?

The execution should be simply:

./test input.csv

and the source code shouldn't be compromised. I need help with expanding on this Cython standalone executable on ubuntu question to embed external libraries (sklearn, numpy, etc) as well.

1
  • Is it vital for you to have the complete program in one script file or is it possible to split the script into a part that you want to hide and a simple python script calling that hidden code? To work with pyinstaller, we have to have some python entry point, whereas cython transpiles python code into c code pyinstaller can start nothing with. The compiled program will still be embedded into a single file you can call from command line. Commented Oct 5, 2017 at 18:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.