Portable python app to run on remote systems

三世轮回 提交于 2019-12-08 12:35:00

问题


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?


回答1:


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




回答2:


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.

  1. install JDK and VisAD-Jython from jython.org(a python to jar compiler)
  2. 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
    
  3. 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.



来源:https://stackoverflow.com/questions/47009498/portable-python-app-to-run-on-remote-systems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!