0

I tried most of the solutions in the web however none of them is working.
I have tested pyinstaller, py2exe, cxfreeze none of this works when compiled on windows.

However when I compiles it with pyinstaller on linux.
the size of exe is around 500mb and int includes so may linux os files.
it works fine on windows as well as on wine under linux.
To reduce size I tried virtualenv, but no success.

Then

I tried to compile it on windows system but so far no success.

I am sharing basic loder main.py that needs to be converted in to exe. and a required compiled file (.pyd) named BasicClasses.....*.pyd

when running main.py it should show kivy app with one button.

the required package are imported in the main.py

can any one share the exe of main.py on windows system that can load BasicClasses.pyd

and steps. And if there is some error in my code please point it.

main.py

#!/bin/python3
##import kivy
import cython
import os
if os.name == 'nt':
    from kivy_deps import sdl2, glew
import pyautogui
###import mouse
#import cv2
#import os, sys
#from pathlib import Path
#from kivy.resources import resource_add_path, resource_find
#from kivy.base import runTouchApp
from openpyxl import load_workbook
#from os.path import abspath
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.switch import Switch
from kivy.uix.label import Label
#from kivy.uix.widget import Widget
##from kivy.uix.canvas import Canvas
from kivy.graphics import Rectangle, Color, Line
#from kivy.graphics import *
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.stacklayout import StackLayout
#from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.filechooser import FileChooserListView
from kivy.properties import ObjectProperty, StringProperty, ColorProperty,NumericProperty
from kivy.uix.textinput import TextInput
#from kivy.core.window import Window
#from kivy.clock import Clock
from kivy.metrics import dp
from kivy.effects.dampedscroll import DampedScrollEffect
##from kivy.effects.scrolleffect import ScrollEffect
#from functools import partial
#from kivy.animation import Animation
import webbrowser
from kivy.config import Config
from BasicClasses import *


root = MainWindow()

if __name__=="__main__":
    root.run()

BasicClasses.py


import os
import sys
import cython
import kivy
#import BasicClasses
import pyautogui
import openpyxl
from openpyxl import load_workbook
###import mouse
#import cv2
#import os, sys
#from pathlib import Path
#from kivy.resources import resource_add_path, resource_find
#from kivy.base import runTouchApp
from openpyxl import load_workbook
#from os.path import abspath
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.switch import Switch
from kivy.uix.label import Label
#from kivy.uix.widget import Widget
##from kivy.uix.canvas import Canvas
from kivy.graphics import Rectangle, Color, Line
#from kivy.graphics import *
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.stacklayout import StackLayout
#from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.filechooser import FileChooserListView
from kivy.properties import ObjectProperty, StringProperty, ColorProperty,NumericProperty
from kivy.uix.textinput import TextInput
#from kivy.core.window import Window
#from kivy.clock import Clock
from kivy.metrics import dp
from kivy.effects.dampedscroll import DampedScrollEffect
##from kivy.effects.scrolleffect import ScrollEffect
#from functools import partial
#from kivy.animation import Animation
#import webbrowser
from kivy.config import Config
ICON='./icon.ico'
Config.set('kivy','window_icon',ICON)


if os.name == 'nt':
    from kivy_deps import sdl2, glew

class Main(Button):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.text='HI'


class MainWindow(App):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        global ICON
        self.icon=ICON
        print('MainWindow in Classes')

    def build(self):
        return Main()


if __name__=='__main__':
    MainWindow().run()

BasicClasses.py can be compiled in binary using cython package with setup.py and compile.bat file as

setup.py

from distutils.core import Extension, setup
from Cython.Build import cythonize

import os
import sys
import cython
#import BasicClasses
import pyautogui
import openpyxl
from openpyxl import load_workbook
###import mouse
#import cv2
#import os, sys
#from pathlib import Path
#from kivy.resources import resource_add_path, resource_find
#from kivy.base import runTouchApp
from openpyxl import load_workbook
#from os.path import abspath
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.switch import Switch
from kivy.uix.label import Label
#from kivy.uix.widget import Widget
##from kivy.uix.canvas import Canvas
from kivy.graphics import Rectangle, Color, Line
#from kivy.graphics import *
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.stacklayout import StackLayout
#from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.filechooser import FileChooserListView
from kivy.properties import ObjectProperty, StringProperty, ColorProperty,NumericProperty
from kivy.uix.textinput import TextInput
#from kivy.core.window import Window
#from kivy.clock import Clock
from kivy.metrics import dp
from kivy.effects.dampedscroll import DampedScrollEffect
##from kivy.effects.scrolleffect import ScrollEffect
#from functools import partial
#from kivy.animation import Animation
#import webbrowser
if os.name == 'nt':
    from kivy_deps import sdl2, glew




# define an extension that will be cythonized and compiled
ext = Extension(name="BasicClasses", sources=["BasicClasses.py"])
setup(ext_modules=cythonize(ext))

##run as 
##python setup.py build_ext --inplace
##python3 setup.py build_ext --inplace

compile.bat

python setup.py build_ext --inplace
python3 setup.py build_ext --inplace
gfdsweds
  • 345
  • 3
  • 11
  • "however when i compiles it with pyinstaller on linux. the size of exe is around 500mb and int includes so may linux os files" - that's probably just what all the dependencies you are using add up to. – DavidW Sep 18 '21 at 14:58
  • Did you had a look a the docs ? https://kivy.org/doc/stable/guide/packaging.html – Maurice Meyer Sep 21 '21 at 12:59
  • Not enough information. What is your `pyinstaller` version? What is the build log? – gfdsweds Dec 10 '21 at 10:20

2 Answers2

1

this guy's answer is really helpful: Can't pyinstall a Kivy python program

If you want to start from the beginning, do pyi-makespec name.py from this source: https://pyinstaller.readthedocs.io/en/stable/spec-files.html

Then you edit the spec file to add kivy dependencies. You need to import sdl2, glew, etc. Look at this spec file for an example: How to get an windows executable from my kivy app (Pyinstaller)?

Now you said you're at the point where kivy runs BUT there is no window -- just a black screen. That is because when you make an app using --onefile instead of --onedir, pyinstaller extracts all the files to a _MEIxxxxxx folder. So kivy is looking at the wrong folder to load your kv file. So how you fix that with relative paths is detailed here: Bundling data files with PyInstaller (--onefile)

There are two messy/easy solutions and the third best solution was the previous link.

#1 is to just load your kv file as a string and put that in your main py folder. Very messy

#2 is to just append sys._MEIPASS to ALL your resource files. For example, if you use os.getcwd()+"\\Resources\\ICON.ico" then the kivy executable will not look in the MEIPASS folder. To fix just do sys._MEIPASS + "\\Resources\\ICON.ico" the kivy window is black because it cannot find your kv file when you make a --onefile with pyinstaller, but in --onedir your relative paths should work to find the kv file. The problem with this is you'll end up making a 2nd copy of your main py and things will get messy.

#3 is to use relative paths as per Bundling data files with PyInstaller (--onefile)

0

To convert .py to .exe:

Go to your python installed directories then using Ctrl + Shift and right click in empty area and then select Open command menu. in command window type "pip install pyinstaller module

then create folder and paste your .py file inside folder then open command prompt using control shift and right click then type "pyinstaller --onefile -w then Your your python script name and click enter. it will take time to generate file for you then in same directory folder called Dist go to and you will see you converted file into exe. double click and run its portable so it will take less than a minute and work properly.

For more details visit this link: https://youtu.be/5AA5i2OlsmQ

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ehsan Rahi
  • 61
  • 7
  • 2
    `"I tried most of the solutions in the web however none of them is working. I have tested pyinstaller, py2exe, cxfreeze none of this works when compiled on windows."` My guy you didn't even read the question and you already answered – Weebify Sep 18 '21 at 14:51
  • I really don't get you. Is it that you're trying to create an executable file with Pyinstaller but it's crashing on startup? – CCCC Sep 18 '21 at 17:09
  • with pyinstaller it gives error with sdl2 so i get it fixed by modifying the spec file and recompulation, after installation for some extra kivy dependencies, however it do not launch or git any error now. no error even in command terminal of windows, The size of onefile generated main.exe is 22mb. – Prateek Raj Gautam Sep 18 '21 at 20:37