简体   繁体   中英

Convert multiple Python files using PyQt5 to exe

I really need help. I have programmed a GUI in Python, using PyQt5. Now I want to convert my files/file into an .exe file, so you are able to use it without installing Python first.

I'm searching on the Internet for almost three and a half hours now, meanwhile trying to solve my problem, but nothing works. I don't understand the PyInstaller Documentation and no other answered question on the Internet helped me.

I have seven Python files (programmed object orientated). A main program and the modules. How do I manage to make this program work without installing Python (respectively, how do I convert them to .exe?)

I'm using Windows 10, have Python 3.5, PyQt5 and PyInstaller 3.3.1 installed.

Thank you very much for any help!

My .spec file looks like this at the moment:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['MainFile.py', 'module1.py', 'module2.py', 'module3.py', 'module4.py', 'module5.py', 'module6.py'],
             pathex=['C:\\Users\\MyName\\Documents\\ProgramFolder'],
             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='MainFile',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='MainFile')

My Error after running:

Traceback (most recent call last):
    File "MainFile.py", line 11, in <module>
    File "C:\Users\MyName\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
      exec(bytecode, module.__dict__)
    File "module2.py", line 2, in <module>
    File "C:\Users\MyName\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
      module = loader.load_module(fullname)
ImportError: DLL load failed: The specified procedure was not found
[11868] Failed to execute script MainFile

Creating a pyinstaller exe can be a bit intimidating for the first time if you are using a lot of libraries and files. I followed the steps as I have mentioned below and it worked for me.

1) Create a spec file using the following command:

pyinstaller filename.py

This will try to create an exe from the py file, but will most probably fail if there are dependencies. That doesnt matter. What it also does is create a spec file that you can use henceforth to create an exe. You will now have a filename.spec in the same directory.

2) The spec file is actually python code that the pyinstaller runs to create your exe. Think of it as your config file needed for creating an exe. Open the spec file using any text editor and edit it as mentioned in the following steps.

2a) Insert all the py files needed for your code to run in the first list within Analysis

eg: Analysis(['file1.py', 'file2.py', 'file3.py'],

2b) Insert all the data files needed in the datas list (within Analysis) in the spec file. Each entry will be a tuple. First element in the tuple will be the path to the resource, and the second entry will be the folder name in the output.

Eg: datas=[('csv\\\\', 'csv'), ('plotly-latest.min.js', '.')],

This will copy the contents of the csv folder in the input and create a csv folder within the output root folder and paste it there. It will also copy the js file and paste it in the root folder of the output.

The final spec file will look something like this:

# -*- mode: python -*-
import sys
sys.setrecursionlimit(5000)

block_cipher = None


a = Analysis(['file1.py', 'file2.py', 'file3.py'],
             pathex=['C:\\Users\\Username\\PycharmProjects\\myproject'],
             binaries=[],
             datas=[('csv\\', 'csv'), ('plotly-latest.min.js', '.')],
             hiddenimports=['scipy._lib.messagestream', 'cython', 'sklearn', 'sklearn.ensemble', 'sklearn.neighbors.typedefs', 'sklearn.neighbors.quad_tree', 'sklearn.tree._utils', 'ipykernel.datapub'],
             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='myproject',
          debug=False,
          strip=False,
          upx=True,
          console=True,
          icon='icons\\appicon.ico')
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='myproject')

Once the spec is ready, you can now create an exe by passing the spec file to the pyinstaller command:

pyinstaller filename.spec

This will create an exe. If you manage to create an exe, but if the exe just opens a console and closes, it means there was an error in the process. You can open the exe in command prompt, read the error and debug further.

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