python - 如何在 Ubuntu 中将 kivy 和 python 打包为可执行文件?

标签 python linux ubuntu kivy pyinstaller

我有一个由许多 Python 和 KV 文件组成的 Kivy 应用程序。我想创建一个可执行文件,以便最终用户可以在 Ubuntu 中运行它。
https://kivy.org/doc/stable/guide/packaging.html文档中提到了如何在 OS X 和 Windows 中打包,但没有提到 Linux 发行版。
任何帮助,将不胜感激。

最佳答案

这是我使用的一个方案。这个脚本创建了一个 .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 安装并运行 main.py如果安装了 python 和 kivy。这些脚本可以根据需要针对不同的应用程序进行修改。

关于python - 如何在 Ubuntu 中将 kivy 和 python 打包为可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68839174/

相关文章:

python - 我如何使用 Py2Exe 将多个 .py 文件组合成一个 .exe

python - 如何从 CSV 文件中提取目标行、前一行和后行?

.net - 仅使用 .NET Core 2.0 运行时的 Linux 上缺少运行时存储错误

Python:在 linux 终端中获取最后一个命令行

python - 在 Python 2 下运行脚本时如何忽略 Python 3 语法?

python - 如何使用预训练权重在 tensorflow 中训练卷积神经网络?

linux - 在 Bash 条件语句中使用 Conky execpi 变量

linux - git push 两步验证失败的解决方法(linux)

linux - ubuntu - 禁用从特定进程/rsyslog 进程记录到 syslog

python - hashlib 找不到ripemd160