Pyinstaller打包示例

it2024-07-05  53

win10、py3.7、64位 vs2015

目录结构

demo     |–my_lib.py     |–read.txt     |–run.py     |–to_pyd.py

文件内容

my_lib.py

import pandas as pd def fun1(): print('\nthis is my_lib.fun1') data = {'a': [1, 2, 3], 'b': ['a', 'b', 'c']} df = pd.DataFrame(data) print(df) with open('read.txt')as f: a = f.readlines() print('read file:', a) return True

read.txt

a b c

run.py

from my_lib import fun1 fun1() input('按任意键退出!')

to_pyd.py

from distutils.core import setup from Cython.Build import cythonize # name随便写,py文件为要编译的文件 setup(name='api_sign', ext_modules=cythonize('my_lib.py', compiler_directives={'language_level': 3}))

安装依赖包

pip install pyinstaller==3.6 # 支持py2的最后版本 pip install cython pip install pandas pip install pywin32 # 打包时报错,ModuleNotFoundError: No module named 'win32com' pip install setuptools==44.0.0 # 执行exe报错,no module named 'pkg_resources.py2_warn'

编译打包

# 编译,pyd文件在当前目录下 python to_pyd.py build_ext --inplace # 报错Unable to find vcvarsall.bat,安装py3.7对应的VS2015 # 打包,exe可执行文件在dist目录下 pyinstaller -F --hidden-import=pandas -clean run.py

–clean:在本次编译开始时,清空上一次编译生成的各种文件。

执行

复制read.txt文件到dist目录下双击运行exe
最新回复(0)