zl程序教程

您现在的位置是:首页 >  后端

当前栏目

ubuntu&windows上打包python程序

2023-09-27 14:26:02 时间

Windows

安装pyinstaller

pip install pyinstaller

测试是否安装成功

pyinstaller -v

打包

pyinstaller --onefile your_program.py
#或者
pyinstaller your_program.py

然后会把当前目录下生成一个dist目录和一个build目录

可执行文件就在dist里面,给其他人用的时候需要把整个dist目录都打包,而不能只给一个exe可执行程序

问题

安装pyinstaller之后使用pyinstaller -v输出 “没有pyinstaller这个程序”

那我们可以找到pyinstaller.exe的位置,把它添加到环境变量 或者 直接在pyinstaller.exe的位置打开cmd。

#找到pyinstaller的位置

D:\Downloads\TicketBot>pip show pyinstaller Name: pyinstaller Version: 5.9.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: c:\users\us\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages Requires: altgraph, pefile, pyinstaller-hooks-contrib, pywin32-ctypes, setuptools Required-by:

可执行文件在:

c:\users\us\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\Scripts

Ubuntu

安装conda

1. 下载anaconda安装包:清华镜像

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.11-Linux-x86_64.sh

可直接从清华镜像网站上下载anaconda安装包, 视情况选择自己的版本,我选择的是2021.11版本。

2.安装包下载完成之后键入

bash Anaconda3-2021.11-Linux-x86_64.sh

期间有ENTER的地方可以直接回车,遇到MORE信息,可以摁Q键跳过,遇到需要输入yes|no的地方输入yes即可。

3.安装完成后在终端修改环境变量,键入

gedit ~/.bashrc

在弹出的文本框最下面键入

export PATH="/home/sxm/anaconda3/bin:$PATH"

sxm是我本机,这里需要根据自己的本机做修改,然后点SAVE保存修改。

4.生效

source ~/.bashrc

打包可执行程序

1.新建一个conda环境,名为pyinstall。这样做也是为了方便与其他环境隔离

conda create -n pyinstall python=3.8

2.激活环境

source activate pyinstall

3.安装pyinstaller包

python -m pip install pyinstaller

4.打包

pyinstaller --onefile --console demo.py
#或者
pyinstaller --console demo.py

可执行文件就在dist里面,给其他人用的时候需要把整个dist目录都打包

参考文章

Ubuntu安装conda

将python程序变成可执行程序