1.将配置文件放在新建的文件夹 res内。
2.修改代码内 配置文件的路径:(code内图标得路径也这样修改)
#coding:utf-8
import sys
import os
#生成资源文件目录访问路径
def resource_path(relative_path):
if getattr(sys, 'frozen', False): #是否Bundle Resource
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#访问res文件夹下a.txt的内容
filename = resource_path(os.path.join("res","a.txt"))
print(filename)
with open(filename) as f:
lines = f.readlines()
print(lines)
f.close()
3. 生成spec:
????????pyi-makespec -F main.py
4. 编辑spec文件:
????????
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['C:\\Users\\xxxxxxx'],
binaries=[],
datas=[('res','res')], #########################修改res文件夹
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
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,
upx_exclude=[],
runtime_tmpdir=None,
console=True , icon='icon.ico')
##############需要命令窗口则console为true,否则为False, icon为exe的图标,必须为ico文件。
5. 生成EXE, pyinstaller -F main.spec
6. 生成exe在Dist文件夹中, exe放在其他电脑也可以执行。
|