PyInstaller does not know how to use the "--onedir" option

Asked 2 years ago, Updated 2 years ago, 88 views

I would like to create an exe file with Python+Kivy+Pyinstaller.
I was able to generate the exe file with the pyinstaller--onedir option without any problems with my previous PC, but after I replaced the PC and re-build the environment, the previous command stopped working.

The usage environment is as follows:
·Anaconda Navigator 2.2.0
·Pyinstaller 5.1
·Python 3.912
·Windows 11 64bit

Below is the Python file used.
python main.py launches a simple app in Kivy.

main.py

#-*-coding:utf-8-*-

from kivy.lang import Builder
Builder.load_string("""
<TextWidget>:
    BoxLayout:
        orientation:'vertical'
        size —root.size

        TextInput:
            text —root.text

        Button:
            id —Button1
            text: "OK"
            font_size:48
            on_press —root.buttonClicked()
""")

from kivy.app import App
from kivy.uix.widget importWidget

from kivy.properties import StringProperty 

class TextWidget (Widget):
    text=StringProperty()

    def__init__(self,**kwargs):
        super(TextWidget,self).__init__(**kwargs)
        self.text='"

    def buttonClicked(self):
        for i in range (5):
            self.text+=str(i)+'\n'

Class TestApp (App):
    def__init__(self,**kwargs):
        super(TestApp,self).__init__(**kwargs)
        self.title = 'greeting'

    def build (self):
        return TextWidget()

if__name__=='__main__':
    TestApp().run()

Compare the --onefile and --onedir options

The article on the Internet only showed examples of using the --onefile option, so I'm having trouble knowing how to use the --onedir option.

The --onedir option is faster to create an exe file than the --onedir option, so I want to use the --onedir option.

1.--If you use the onefile option

If you use the --onefile option, it works fine.
First execute the following command:

pyinstaller main.py --onefile

Then, we edited the generated main.spec file as follows:

main.spec

with the -onefile option
#-*-mode:python;coding:utf-8-*-

from kivy_deps import sdl2,glew
block_cipher=None


a = Analysis (['main.py'],
             pathx=[],
             binaries=[],
             datas=[],
             hiddenimports = ['win32file', 'win32timezone',
             hookspath=[],
             hookconfig={},
             runtime_hooks=[],
             exclude=[],
             win_no_prefer_redirects=False,
             win_private_assembly=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,  
          * Tree(p) for pin(sdl2.dep_bins+glew.dep_bins),
          name = 'main',
          debug = False,
          bootloader_ignore_signals=False,
          strip=False,
          upx = True,
          upx_exclude=[],
          runtime_tmpdir = None,
          console=True,
          disable_windowed_traceback=False,
          target_arch = None,
          codedesign_identity=None,
          entries_file=None)
col=COLLECT(exe, Tree('.'),
               a. binaries,
               a.zipfiles,
               a.datas,
               * Tree(p) for pin(sdl2.dep_bins+glew.dep_bins),
               strip=False,
               upx = True,
               name = 'main')

The following command created the exe file:

pyinstaller main.spec

Then, I was able to double-click the dist/main/main.exe file without any problems.
However, at this point, I think it's a little strange, but if you use the -onefile option, you'll find main.exe directly under the dist, and dist/main/main.exe also has an exe file.

Originally, I remember that the dist/main folder could not be created without using the --onedir option, but has the specification changed?

Also, even if you take out the dist/main/main.exe generated by the --onefile option and move it to another folder, it will start by itself.

Enter a description of the image here

2.--Onedir

I didn't want to put the files together, so I ran it with the --onedir option as follows:Before running, the build or dist folders generated by the --onefile option have been deleted.

pyinstaller main.py --onedir

I edited the generated main.spec and did the following:

main.spec

with the -onedir option
#-*-mode:python;coding:utf-8-*-
from kivy_deps import sdl2,glew

block_cipher=None


a = Analysis (['main.py'],
             pathx=[],
             binaries=[],
             datas=[],
             hiddenimports = ['win32file', 'win32timezone',
             hookspath=[],
             hookconfig={},
             runtime_hooks=[],
             exclude=[],
             win_no_prefer_redirects=False,
             win_private_assembly=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe=EXE(pyz,
          a.scripts, 
          * Tree(p) for pin(sdl2.dep_bins+glew.dep_bins),
          exclude_binaries = True,
          name = 'main',
          debug = False,
          bootloader_ignore_signals=False,
          strip=False,
          upx = True,
          console=True,
          disable_windowed_traceback=False,
          target_arch = None,
          codedesign_identity=None,
          entries_file=None)
col=COLLECT(exe, Tree('.'),
               a. binaries,
               a.zipfiles,
               a.datas,
               * Tree(p) for pin(sdl2.dep_bins+glew.dep_bins),
               strip=False,
               upx = True,
               name = 'main')

Then we executed the following command:

pyinstaller main.spec

dist/main/main.exe does not start the software.
main.specIs it wrong to edit the file?

[Additional note]
The main.exe generated by the --onedir option and the main.exe generated by the --onefile option could not be run as it is, so I edited the main.spec file and ran it.

For the --onefile option, editing main.spec worked, but for the --onedir option, editing main.spec did not work.

--onefileThe following article explains how to edit the main.spec option.
How to use Pyinstaller to convert an application created with kivy into an EXE file

The --onedir option was edited using the same contents as the --onefile option, which may be the reason why it doesn't work.

Your previous Windows 10 machine could generate a main.exe file that would work even if you ran the command without using the spec file, so there is a possibility that the error depends on your current environment.

python pyinstaller kivy

2022-09-30 20:19

1 Answers

1.--If you use the onefile option

For the --onefile option, there is main.exe directly under dist, and there is also an exe file in dist/main/main.exe.

When you edit main.spec with the --onefile option, you may have added the coll=COLLECT(...) section.

In other words, it is not an option, but a problem with the .spec file editing.

This part of the document will be affected.
Spec File Operation

  • An instance of COLLECT creates the output folder from all the other parts.

In one-file mode, there is no call to COLLECT, and the EXE instance receipts all of the scripts, modules and binaries.

  • The COLLECT instance creates an output folder from all other parts

1 In file mode, there are no COLLECT calls, and the EXE instance receives all scripts, modules, and binaries.

Comparing the contents of the pre-edit .spec file that is automatically created when you create an executable file using each option, you can see if there is a coll=COLLECT(...) section.

2.--Onedir

dist/main/main.exe does not start the software.
main.specIs it wrong to edit the file?

Probably so.
You can create an executable once in the pre-edit .spec file that is automatically created when you specify the --onedir option, and then edit it.

The executable created from the source code provided in the question worked without editing the .spec file.
Your actual program will need to be edited accordingly, but the details will probably not appear in the source code provided, so you will need to look it up yourself.

According to the comment, after uninstalling Anaconda and building an environment based on WinPython, it worked with the default usage.
Something in the Anaconda environment has affected the problem.

Note:
[WinPython] Usage and configuration summary
WinPython
For Windows+WinPython+pip - [Python] How to install Kivy (pip)


2022-09-30 20:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.