相关代码
main.py
import sys import os #生成资源文件目录访问路径 #说明: pyinstaller工具打包的可执行文件,运行时sys。frozen会被设置成True # 因此可以通过sys.frozen的值区分是开发环境还是打包后的生成环境 # # 打包后的生产环境,资源文件都放在sys._MEIPASS目录下 # 修改main.spec中的datas, # 如datas=[(‘res‘, ‘res‘)],意思是当前目录下的res目录加入目标exe中,在运行时放在零时文件的根目录下,名称为res def resource_path(relative_path): if getattr(sys, ‘frozen‘, False): base_path = sys._MEIPASS else: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) filename = resource_path(os.path.join("res","a.txt")) print(filename) with open(filename) as f: lines = f.readlines() print(lines) f.close()
res/a.txt
1 2 3 4 5 6
打包流程
- 生成main.spec文件:pyi-makespec -F test.py
- 修改mian.spec文件,如下
- pyinstaller -F main.spec
# -*- mode: python -*- block_cipher = None a = Analysis([‘main.py‘], pathex=[‘C:\\Users\\xxx\\PycharmProjects\\Test‘], binaries=[], datas=[(‘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, runtime_tmpdir=None, console=True )
pyinstall遇到的问题
描述:
Traceback (most recent call last): File "C:\Python\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "C:\Python\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Users\10223394\PycharmProjects\Test\venv\Scripts\pyinstaller.exe\__main__.py", line 9, in <module> File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\__main__.py", line 111, in run run_build(pyi_config, spec_file, **vars(args)) File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\building\build_main.py", line 838, in main build(specfile, kw.get(‘distpath‘), kw.get(‘workpath‘), kw.get(‘clean_build‘)) File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\building\build_main.py", line 784, in build exec(text, spec_namespace) File "<string>", line 32, in <module> File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\building\api.py", line 424, in __init__ strip_binaries=self.strip, upx_binaries=self.upx, File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\building\api.py", line 196, in __init__ self.__postinit__() File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__ self.assemble() File "c:\users\10223394\pycharmprojects\test\venv\lib\site-packages\PyInstaller\building\api.py", line 273, in assemble pylib_name = os.path.basename(bindepend.get_python_library_path()) File "C:\Python\lib\ntpath.py", line 214, in basename return split(p)[1] File "C:\Python\lib\ntpath.py", line 183, in split p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not NoneType
解决办法:
这是个已知的故障,官方已经修改,但是没有正式的版本支持。下载https://github.com/Loran425/pyinstaller/tree/14b6e65642e4b07a4358bab278019a48dedf7460中PyInstaller里的bindepend.py文件,然后替换掉本地安装包里的bindepend.py文件即可。
原文地址:https://www.cnblogs.com/chusiyong/p/12052930.html
时间: 2024-11-05 15:55:49