打包exe文件需要使用windows环境
安装
pip install pyinstaller
python打包
mac 需要使用sudo执行
# 打包为文件夹的形式
sudo pyinstaller hello.py
#打包为单个文件的形式
sudo pyinstaller -F hello.py
-i 指定icon
-n 指定打包后程序的名称
# 指定打包 spec文件 打包
pyinstaller -F xx.spec
# py文件要读取 文本内容
#不能用相对路径或 os.path.dirname(__file__ ) 获取当前的路径
## 方法1
import sys,os
# sys.argv 脚本传递的所有参数参数,0 是当前脚本的 名称(非绝对路径)
# os.path.realpath(sys.argv[0]) # os.path.realpath('文件名'),获取文件的绝对路径
# os.path.realpath() 和os.path.abspath是基本是一样的,具体参考:https://blog.csdn.net/rainshine1190/article/details/85165059
path=os.path.dirname(os.path.realpath(sys.argv[0])) # 这样获取文件路径
## 方法2
import sys,os
if getattr(sys,'frozen',Fales):
path=os.path.dirname(sys.executable)
else:
path=os.path.dirname(os.path.abspath(__file__))
python解释器位置声明
cat hello.py
#!/usr/bin/python3.8 # 声明解释器的绝对路径
if __name__ == '__main__':
print('hello')
cat hello.py
#!/usr/bin/env python3.8 # 在环境变量里里找python3.8
if __name__ == '__main__':
print('hello')