0

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
2
  • 3
    Have you tried to run the command line pyinstaller -F main.py (replacing main.py with your main .py file, of course) ? PyQt5 seems to be fully supported by pyinstaller, so it is expected to work... Commented Aug 10, 2018 at 13:31
  • Sadly it don't works... the exe opens and closes immediatley after running Commented Aug 12, 2018 at 11:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey, first of all: thank you very much for your long answer and the time you spend! But I didn't worked ... I also don't understand your answer 100%. I added all .py files needed for the program in Analysis. But I don't know how "datas=" works (what to put in there). And where do I add PyQt5? I think that's the main problem (PyInstaller doesn't find it)
Does your program depend on external data like a csv file, an image or some other non-python document? if not, then you can forget the datas for the time being. When you say it doesnt work, what is the error message you are getting? Can you also post your spec file as an update? might give a clue on why it is not working? Edit: Open a command prompt, navigate to where the exe is, and run the exe by just typing the program name. It wouldnt exit and will stay there and show you exactly what the error is.
I posted my .spec File. My Program needs a .png and a .db file. I understand what to write as first element int the tuple, but not as a second? Should I delete the folders (except of .spec file) which were made after the first ,,pyinstaller MainFile.py" before building the .exe with the .spec file?
1) I will refer to your code location as the input location, and the dist/projectname folder as the output location. 2) The first argument in the tuple will be the relative location of the data file in the input location. How you want it stored in the output location will be your second argument in the tuple. So if your db file is in Projectfolder\Data\database.db, and you want a similar relative path in the output location , then your datas will be: datas=[('Data\\database.db', 'Data\\database.db')] As I said before, just run the exe in a cmd prompt and check what the error msg is?
After running ,,pyinstaller MainFile.py" I get MANY Warning, which all pretty much look similar: It's every time a ,,lib not found: api-ms-win-crt-......dll" error. But the .exe is build. If I try to run it, the console says this: (new answer because it looks better)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.