简体   繁体   中英

How to tidy up a packaged Python app with pyinstaller?

So, let's say I got a simple pyqt app main.py :

import sys
from PyQt5 import QtWidgets


def main():

    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

And then i got a main.spec which packages the app in one folder:

# -*- mode: python -*-

block_cipher = None

import inspect, os
current_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

a = Analysis(['main.py'],
             pathex=[current_path],
             binaries=[],
             datas=[],
             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,
          exclude_binaries=True,
          name='main',
          debug=False,
          strip=False,
          upx=False,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               name='main')

The outcome of doing pyinstaller main.spec will be a working pyqt app which has a mess of files:

在此输入图像描述

So, here's the thing, I don't like the current outcome and I also don't like the option of using --onefile (the idea of extracting files into a temp directory is not my cup of tea).

Now, I've found this interesting article which presents a solution to this problem and I was trying to reproduce it here with this simple mcve but for some reason I've got stuck at certain point. Here's the steps i've followed:

1) I've created a file pyinstaller\\use_lib.py :

import sys
import os
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "lib"))

2) I've replaced runtime_hooks=[] by runtime_hooks=['.\\\\pyinstaller\\\\use_lib.py'] in the above main.spec file

3) I've rerun pyinstaller main.spec , which has generated the bunch of messy files like the above screenshot

4) I've moved manually all dependency files to a lib directory and the results is this:

在此输入图像描述

PROBLEM: When i try to run the app will crash:

在此输入图像描述

Why do you think it's crashing? At which step I've messed up? Could you please explain me how to fix it?

You can't move those dlls away from exe (into another directory). Those dlls are linked statically and should be placed in the same directory as exe.

Anyway. Look into some application folders inside of your C:\\Program Files . There are big bunch of files inside each of those directories. It is just the way it goes. And no one cares because users does not look into these folders.

If you want to distribute your application you should act as all other developers. Folder state after using PyInstaller is not the final form of your application but only an initial form: any C/C++ application will start its way to the users from this exact form.

So if you want to distribute your application to the users you should use one of the installer tools. The best form of installation packet for the Windows platform is msi packet (made for "Windows Installer"). To build your msi packet you may use WiX Installer (the simplest way to create msi packets) or MS Visual Studio . Also there is a plenty amount of installation tools which will generate exe form of installation packets (and usually they are much more simple to use than msi-tools): NSIS , Inno Setup , InstallShield (paid!), etc. Also you may search names of this installers through https://pypi.python.org/pypi database: there are some special Python packets which you can use to manage some of this installation tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM