27
loading...
This website collects cookies to deliver better user experience
pip install pyinstaller
main.py
- which is going to contain the main code.main.kv
- which is going to contain the code written in kivy langmain.py
, enter the following code:import os, sys
from kivy.resources import resource_add_path, resource_find
from kivymd.app import MDApp
class MainApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepPurple"
if __name__ == '__main__':
try:
if hasattr(sys, '_MEIPASS'):
resource_add_path(os.path.join(sys._MEIPASS))
app = MainApp()
app.run()
except Exception as e:
print(e)
input("Press enter.")
input("Press enter.")
stops the console from closing before we want it to.main.kv
:MDRectangleFlatButton:
text: "Hello World"
pos_hint: {'center_x': .5, 'center_y': .5}
pyinstaller --onefile main.py
in command prompt and press enter.main.py
; dist
and build
and a file named main.spec
.dist
folder. Inside it, there is an executable file, main.exe
. If you double click it, a terminal will open and close very quickly.main.spec
:from kivy_deps import sdl2, glew
from kivymd import hooks_path as kivymd_hooks_path
hookspath
as shown shortly below.Analysis
the lines:a = Analysis(
#...
datas=[('main.kv', '.')],
hookspath=[kivymd_hooks_path],
#...
)
datas
is made up of a list of tuples where the first item in the tuple is the file name and the second item is the directory that is going to be used to store the file. If for example, you had a folder named images in the same directory as main.py
, you would add them as follows:datas=[('main.kv', '.'), ('./images/*.png', 'images')],
EXE
:exe = EXE(
#...
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
#....
)
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
from kivy_deps import sdl2, glew
from kivymd import hooks_path as kivymd_hooks_path
a = Analysis(['main.py'],
pathex=['C:\\Users\\path\\to\\file'],
binaries=[],
datas=[('main.kv', '.')],
hiddenimports=[],
hookspath=[kivymd_hooks_path],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=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 p in (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 )
pyinstaller main.spec
in the terminal.main.exe
again.pip install pyenchant
pip install opencv-python
pyinstaller main.spec
in your terminal and try executing the main.exe
again.