【问题标题】:How to package kivy and python to executable file in Ubuntu?如何在 Ubuntu 中将 kivy 和 python 打包成可执行文件?
【发布时间】:2021-10-20 15:36:35
【问题描述】:

我有一个由许多 Python 和 KV 文件组成的 Kivy 应用程序。我想创建一个可执行文件,以便最终用户可以在 Ubuntu 中运行它。

https://kivy.org/doc/stable/guide/packaging.html 文档提到了如何在 OS X 和 Windows 中打包,但没有提到 Linux 发行版。

任何帮助将不胜感激。

【问题讨论】:

    标签: python linux ubuntu kivy pyinstaller


    【解决方案1】:

    这是我使用的一个方案。此脚本创建一个.sh 文件来运行python/kivy 应用程序:

    #!/usr/bin/tcsh
    
    set APP_NAME="HandAndFoot"
    
    # setup output
    set OUTPUT="${APP_NAME}.sh"
    if $#argv > 0 then
        set OUTPUT=$argv[1]
    endif
    echo "output will be to $OUTPUT"
    
    # copy initial part of shell script into output file
    cp ${APP_NAME}.basis $OUTPUT
    
    # create a tar file of the Hand And Foot app
    rm -f tmp.tar
    tar cf app.tar *.py *.kv dropbox-trusted-certs.crt dialogs/*.py dialogs/*.kv dialogs/*.png resources/*.png resources/*.ico resources/default/*.png
    
    # add the uuencoded version of the app to the end of the output file
    uuencode app.tar app.tar >> $OUTPUT
    chmod 755 $OUTPUT
    
    # How does a file named "0" get created?? Don't know, but this gets rid of it
    rm -f 0
    

    上述脚本创建了一个HandAndFoot.sh,其中包含应用程序的tar。在这种情况下,应用程序称为HandAndFoot。当HandAndFoot.sh 运行时,它会执行应用程序的main.py 脚​​本。

    除了上述脚本之外,还需要另一个文件(在本例中名为 HandAndFoot.basis):

    #!/bin/bash
    
    APP_NAME="HandAndFoot"
    PYTHON="NONE"
    # check if python3 is installed
    pyvers=$(python3 -V 2>&1)
    echo $pyvers
    regex="Python 3.*"
    if [[ $pyvers =~ $regex ]]
    then
        PYTHON="python3"
    else
        # check if python is installed
        pyvers=$(python -V 2>&1)
        if [[ $pyvers =~ $regex ]]
        then
            PYTHON="python"
        fi
    fi
    if [[ $PYTHON != "NONE" ]]
    then
        kivyimport=$(
        $PYTHON 2>&1  <<EOF
    import kivy
    EOF
        )
        kivyregex=".*Kivy .*"
        if ! [[ $kivyimport =~ $kivyregex ]]
        then
            echo "Python is installed, but Kivy is also required"
            echo "Use you package Manager or 'apt-get' to install Kivy"
            exit
        fi
    else
        echo "Python and Kivy are not installed, but are required for the $APP_NAME app"
        echo "Use your Package Manager or 'apt-get' to install Python and Kivy"
        exit
    fi
    
    match=$(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1)
    payload_start=$((match + 1))
    tail -n +$payload_start $0 | uudecode
    rm -rf /tmp/$APP_NAME
    mkdir /tmp/$APP_NAME
    mv app.tar /tmp/$APP_NAME
    cd /tmp/$APP_NAME
    tar xvf app.tar
    $PYTHON main.py &
    exit
    
    
    
    PAYLOAD:
    

    最后的脚本检查 Python 和 Kivy 的安装,如果安装了 python 和 Kivy,则运行main.py。这些脚本可以根据需要针对不同的应用进行修改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多