一、在线安装
1、安装libffi-devel
如果不先安装这个依赖,运行pyinstaller --version的时候会出现错误
问题:ModuleNotFoundError: No module named ‘_ctypes’
另外:这个依赖必须在python前进行安装,否则需要把python卸载之后再安装
yum install libffi-devel
2、安装3.8+版本的python
whereis python
which python
ls -l /usr/bin/python*
tar -zxvf Python-3.8.0.tgz
cd Python-3.8.0
./configure --prefix=/usr/local/python3.8 --enable-shared(使用这个参数可能会出现so文件找不到)
make
make instal
ln -s -f /usr/local/python3.8/bin/python3 /usr/bin/python3
ln -s -f /usr/local/python3.8/bin/pip3 /usr/bin/pip3
3、安装pyinstaller
pip3 install pyinstaller
可能需要升级pip:
pip3 install --upgrade pip
安装之后如果出现报错问题:-bash: pyinstaller: 未找到命令,需要进行环境配置
find / -name pyinstaller
vi ~/.bash_profile
export PATH="$PATH:/usr/local/python3.8/bin"
source ~/.bash_profile
二、离线安装
1、在一个联网的Linux服务器上下载pyinstaller安装文件
需要注意联网的和离线的Linux的python版本需要一致
pip3 download pyinstaller
2、安装python
3、将下载的文件上传到离线服务器上,进入目录,并执行以下命令安装
pip3 install --no-index --find-links=. pyinstaller
三、验证是否安装完成
# 方式一:
pyinstaller --version
6.12.0
# 方式二:
pip3 show pyinstaller
Name: pyinstaller
Version: 6.12.0
Summary: PyInstaller bundles a Python application and all its dependencies into a single package.
Home-page: https://www.pyinstaller.org/
Author: Hartmut Goebel, Giovanni Bajo, David Vierra, David Cortesi, Martin Zibricky
Author-email:
License: GPLv2-or-later with a special exception which allows to use PyInstaller to build and distribute non-free programs (including commercial ones)
Location: /usr/local/python3.8/lib/python3.8/site-packages
Requires: altgraph, importlib-metadata, packaging, pyinstaller-hooks-contrib, setuptools
Required-by:
四、打包py文件
# 打包
pyinstaller --onefile main.py
# --onefile 生成一个单一的可执行文件,这个文件包含脚本和所需要的依赖,在目录dist中
# 授权
chmod a+x runsh
# 后台运行
nohup ./runsh &
nohup python3 -u ./runsh.py 9527 > output.log 2>&1 &
PYTHONUNBUFFERED=1 nohup python3 ./runsh.py 9527 > output.log 2>&1 &
在 Python 中,你可以通过设置环境变量 PYTHONUNBUFFERED 来禁用输出缓冲,或者使用 -u 选项来运行 Python 解释器。
这两条命令都会确保 Python 脚本的输出是实时写入 output.log 文件的
PYTHONUNBUFFERED=1:设置环境变量 PYTHONUNBUFFERED 为 1,告诉 Python 解释器不要缓冲输出。
-u:这是 Python 解释器的一个选项,用于强制标准流(stdin、stdout、stderr)使用不带缓冲的模式。
如果python打包成了执行文件,就不适用了,还是需要引入日志工具
# 查找进程
ps -ef | grep runsh
# 停止程序
pkill runsh
或者
kill 2399
遇到问题
PyInstaller.exceptions.PythonLibraryNotFoundError: Python library not found: libpython3.8.so, libpython3.8.so.1.0
安装python的时候增加参数 --enable-shared
安装完之后将so文件复制到
find / -name libpython3.8.so.1.0
cp /usr/local/python3.8/lib/libpython3.8.so.1.0 /usr/lib64/
2325

被折叠的 条评论
为什么被折叠?



