目录
使用pyinstaller可以把.py文件打包为.exe可执行文件
具体命令(参数上网查很详细 这里不细说):
pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME] [--add-data <SRC;DEST or SRC:DEST>]
[--add-binary <SRC;DEST or SRC:DEST>] [-p DIR] [--hidden-import MODULENAME]
[--additional-hooks-dir HOOKSPATH] [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
[--key KEY] [-d {all,imports,bootloader,noarchive}] [-s] [--noupx] [--upx-exclude FILE] [-c] [-w]
[-i <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">] [--version-file FILE] [-m <FILE or XML>]
[-r RESOURCE] [--uac-admin] [--uac-uiaccess] [--win-private-assemblies] [--win-no-prefer-redirects]
[--osx-bundle-identifier BUNDLE_IDENTIFIER] [--runtime-tmpdir PATH] [--bootloader-ignore-signals]
[--distpath DIR] [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
scriptname [scriptname ...]
-D | 打包后有两个文件夹[dist]和[build],可执行文件在dist文件夹里面,.exe文件不可单独使用 |
---|---|
-F | 只有一个.exe文件在dist文件夹. |
import sys import os
1 sys.path[0] 目录 2 sys.argv[0] 程序目录+文件名 3 os.path.dirname(os.path.realpath(sys.executable)) 父目录 4 os.path.dirname(os.path.realpath(sys.argv[0])) 同(3)
import os,sys
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)