如何将Python项目打包成独立可执行文件?

2026-06-09 16:403阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计773个文字,预计阅读时间需要4分钟。

如何将Python项目打包成独立可执行文件?

Linux环境下打包Python工程,将程序交付到生产环境(甲方),不想进行环境维护或甲方查看源代码,因此需要将源代码打包成可执行文件。可以使用PyInstaller工具实现这一目标。具体步骤如下:

1. 安装PyInstaller: pip install pyinstaller

2. 使用PyInstaller打包Python程序: pyinstaller --onefile your_script.py

这条命令会将`your_script.py`打包成一个独立的可执行文件。

3. 打包完成后,可执行文件通常位于`dist`目录下。将这个目录中的文件交付给甲方即可。

更多信息和详细步骤,请访问PyInstaller官方文档:[https://pyinstaller.readthedocs.io/en/stable/requirements.](https://pyinstaller.readthedocs.io/en/stable/requirements.)。

一、Linux环境打包python工程

  将程序交付到生产环境(甲方),不想要环境维护者或甲方看到源代码,所以需要将源代码打包成可执行文件

  ​​pyinstaller.readthedocs.io/en/stable/requirements.html​​  

一)安装打包环境(PyInstaller)

  1、安装依赖包

yum install -y python-setuptools python-dev build-essential

  2、下载并安装pyinstaller

  在网址下载pyisntaller的包,地址:​​github.com/pyinstaller/pyinstaller/releases​​ ,下载对应的tar包

如何将Python项目打包成独立可执行文件?

cd ${BASE_DIR}
#下载所需的release版本

tar -xvf pyinstaller-4.10.tar.gz
cd pyinstaller-4.10
pip3 install wheel
python3 setup.py install

如果中间没有报错的话,pyinstaller就安装完成了
验证pyinstaller
pyinstaller --version
4.10

  3、打包python项目源码

  PyInstaller 工具的命令语法如下:

pyinstaller 选项 Python 源文件(程序主文件)

  举例

## 示例代码
]# cat main.py
print("hello,world!")


pyinstaller -F main.py

  编译完成后,会将可执行文件保存到dist目录下

]# tree -L 1 dist
dist
└── main

  执行可执行文件

]# ./dist/main
hello,world!

  4、打包复杂环境,需修改配置文件然后重新编译

  因为" pyinstaller -F 程序主文件.py ”这个打包的方法它只会打包当前目录下的所有py文件,而不会打包config和database两个文件夹,所以此时的可执行文件打包的并不完整,此时该怎么做

  这种情况需要修改程序main.spec

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['main.py'],
pathex=['/app/test'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)

dict_database = Tree('/app/test/database',prefix='database')
a.datas += dict_database
dict_config = Tree('/app/test/config',prefix='config')
a.datas += dict_config

pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )

  重新编译

pyinstaller mian.spec


二、Windows环境打包Python工程






本文共计773个文字,预计阅读时间需要4分钟。

如何将Python项目打包成独立可执行文件?

Linux环境下打包Python工程,将程序交付到生产环境(甲方),不想进行环境维护或甲方查看源代码,因此需要将源代码打包成可执行文件。可以使用PyInstaller工具实现这一目标。具体步骤如下:

1. 安装PyInstaller: pip install pyinstaller

2. 使用PyInstaller打包Python程序: pyinstaller --onefile your_script.py

这条命令会将`your_script.py`打包成一个独立的可执行文件。

3. 打包完成后,可执行文件通常位于`dist`目录下。将这个目录中的文件交付给甲方即可。

更多信息和详细步骤,请访问PyInstaller官方文档:[https://pyinstaller.readthedocs.io/en/stable/requirements.](https://pyinstaller.readthedocs.io/en/stable/requirements.)。

一、Linux环境打包python工程

  将程序交付到生产环境(甲方),不想要环境维护者或甲方看到源代码,所以需要将源代码打包成可执行文件

  ​​pyinstaller.readthedocs.io/en/stable/requirements.html​​  

一)安装打包环境(PyInstaller)

  1、安装依赖包

yum install -y python-setuptools python-dev build-essential

  2、下载并安装pyinstaller

  在网址下载pyisntaller的包,地址:​​github.com/pyinstaller/pyinstaller/releases​​ ,下载对应的tar包

如何将Python项目打包成独立可执行文件?

cd ${BASE_DIR}
#下载所需的release版本

tar -xvf pyinstaller-4.10.tar.gz
cd pyinstaller-4.10
pip3 install wheel
python3 setup.py install

如果中间没有报错的话,pyinstaller就安装完成了
验证pyinstaller
pyinstaller --version
4.10

  3、打包python项目源码

  PyInstaller 工具的命令语法如下:

pyinstaller 选项 Python 源文件(程序主文件)

  举例

## 示例代码
]# cat main.py
print("hello,world!")


pyinstaller -F main.py

  编译完成后,会将可执行文件保存到dist目录下

]# tree -L 1 dist
dist
└── main

  执行可执行文件

]# ./dist/main
hello,world!

  4、打包复杂环境,需修改配置文件然后重新编译

  因为" pyinstaller -F 程序主文件.py ”这个打包的方法它只会打包当前目录下的所有py文件,而不会打包config和database两个文件夹,所以此时的可执行文件打包的并不完整,此时该怎么做

  这种情况需要修改程序main.spec

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['main.py'],
pathex=['/app/test'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)

dict_database = Tree('/app/test/database',prefix='database')
a.datas += dict_database
dict_config = Tree('/app/test/config',prefix='config')
a.datas += dict_config

pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )

  重新编译

pyinstaller mian.spec


二、Windows环境打包Python工程