pyinstaller打包数据

使用pyinstaller可以打包数据到exe中,如果是多个文件,则直接创建在文件夹内,如果打包成一个exe,则会自动解压缩到临时目录,等程序执行结束的时候会自动删除该目录,windows是直接解压缩到%temp%目录下,最终都会解压缩到指定的目录下。


I know this is old but to update newer pyinstaller's do not set env variable anymore, now the path gets set as sys._MEIPASS

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

示例代码:

__author__ = 'ds'  import os
import sys
import time


print sys._MEIPASS
time.sleep(60*30)

打包脚本

# -*- mode: python -*-

block_cipher = None


a = Analysis(['pyinstaller_test.py'],
             pathex=['C:\\Users\\ds\\PycharmProjects\\untitled'],
             binaries=None,
              datas=[('test.txt', 'test'), ],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='pyinstaller_test',
          debug=False,
          strip=False,
          upx=True,
          console=True )


输出:

C:\Users\ds\PycharmProjects\untitled\dist>pyinstaller_test.exe
C:\Users\ds\AppData\Local\Temp\_MEI51~1


C:\Users\ds\PycharmProjects\untitled\dist>pyinstaller_test.exe
C:\Users\ds\AppData\Local\Temp\_MEI90~1

你可能感兴趣的:(python,pyinstaller)