Reputation: 1
I am in need to generate a standalone portable python application and run on remote systems where even python interpreter is not installed or version is different.
The aim will be to pack the python script as an app, transfer it to the remote systems and run it as a standalone app without any dependencies.
Do we have such a way in the Python world? Has anybody done a similar thing before?
Upvotes: 0
Views: 712
Reputation: 112
Since you want portability even more than what Python already provides as you mentioned that Python interpreter might not be there on remote system, most suitable way for you could be to compile it as Java application. You can follow the steps below.
Make a batch file (or script) in the VisAD-Jython install directory to invoke the jythonc compiler. Below is an example script. let's name it abc.bat
@echo off
set JYTHON_PATH=.
set CLASSPATH=.
%JYTHON_PATH%\jre\bin\java.exe -mx256m "-Dpython.home=%JYTHON_PATH%\\" "-Dpython .path=%JYTHON_PATH%.\\" -cp "%JYTHON_PATH%\\jython.jar;%JYTHON_PATH%\\visad.jar; %JYTHON_PATH%\\;." org.python.util.jython "%JYTHON_PATH%\Tools\jythonc\jythonc.p y " --compiler javac %1 %2 %3 %4 %5 %6 %7 %8 %9
Then to compile your .py script like below:
abc --core --jar new.jar new.py
Now you can port and run it anywhere with Java.
Upvotes: 0
Reputation: 61
Freezing is the name of the game.
Here's an example using PyInstaller
.
A sample script test.py
:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import reduce
my_list = list('1234567890')
my_list = list(map(lambda x: int(x), my_list))
my_sum = reduce(lambda x, y: x + y, my_list)
print("The sum of %s is %d" % (list(my_list), my_sum))
To create a binary from the script, install PyInstaller
and run it on your code:
$ pyinstaller -F test.py
After it finishes, you should find the standalone binary in ./dist
. When you run it, it behaves just like any other program:
$ ./dist/test
The sum of [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] is 45
For more information, take a look here
Upvotes: 2