#๐Ÿ”’ Kivy to .exe executable errors

197 messages ยท Page 1 of 1 (latest)

ripe parrot
#

Hey. I have been creating this pos application using kivy and python for the past month and it has been working perfectly. However, I tried to convert the app into an .exe executable file for windows, using the --onefile approach, and although it compiles and completes. the result is a black screen instead of showing the application. I have tried using helper functions def resource_path(relative_path) to link all non-python (without .py) files, but it still doesn't work. I have been stuck at this problem for days, and the client expects the project by the end of the week. It feels as if I have wasted an entire month.... Someone please come rescue me. Please!!!

vernal raptorBOT
#

@ripe parrot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

upper dirge
#

i dont know much about how you will fix this in time. but if the client is on windows. i have a solution for you.

  1. use the embedded python edition, download the zip from official python website
  2. install all necessary modules into the embedded python edition (this is a special process)
  3. place your modules/source alongside the embedded python
  4. write a ".bat" script or write a ".py" file that simply executes the embedded python and launches your module. if you choose to use ".py" here, it can be easily converted since it wont use any external modules
kind pilot
#

Show your resource_path helpers?

#

Do you get any error messages?

#

@ripe parrot ^

#

I saw you posted it, but it disappeared

ripe parrot
ripe parrot
# kind pilot I saw you posted it, but it disappeared

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)

Builder.load_file(resource_path('views/dailysales/dailysales.kv'))
class DailySales(BoxLayout):

upper dirge
#

alright, i'll guide you through it?

#

download windows embeddable package 64-bit

ripe parrot
# kind pilot I saw you posted it, but it disappeared

def show_user_details(self, user_info):
# Perform the update in your data storage (e.g., JSON file)
users = []
if os.path.exists(resource_path("users.json")):
with open(resource_path("users.json"), "r") as f:
users = json.load(f)

    # Find and update the specific user
    for u in users:
        if u["user_name"] == self.user_info.get('user_name', 'User'):
            # Update the UI with the new user information
            self.user_name = f"{u['first_name']} {u['last_name']}"
            self.nick_name = u['user_name']

this is how I am using it within the class.

kind pilot
#

I think you want to use importlib.resources not MEIPASS

ripe parrot
upper dirge
#

make a folder, for the sake of cleanliness

#

and extract the zip into that folder

ripe parrot
kind pilot
#

Well it depends where views is stored as a python package

upper dirge
kind pilot
#

Like how would you import views.dailysales

ripe parrot
upper dirge
#

yeah

#

that should do

ripe parrot
upper dirge
#

uh can you show me how you have saved the files? screenshot the folder i asked you to create

ripe parrot
ripe parrot
upper dirge
#

uh make a new folder

#

and copy that folder within it

#

we need to keep the python runtime clean

kind pilot
#

So you want:

import os
import importlib.resources
from views import dailysales

dskv = importlib.resources.files(dailysales) / "dailysales.kv"
with importlib.resources.as_file(dskv) as dskv_fn:
    Builder.load_file(os.fsdecode(dskv_fn))
ripe parrot
upper dirge
#

yeah, nice, that's exactly what i needed. now rename the folder to "python" (This is new folder (Will rename it))

#

also, copy the pip.pyz file from inside that folder to outside that folder (move it to "Embedding Python")

#

remove it from inside

#

also, copy your source code folder to "embedding python"

kind pilot
ripe parrot
upper dirge
#

not a problem, just copy ur source to the embedding python folder

ripe parrot
kind pilot
#

Like FileNotFoundError?

ripe parrot
ripe parrot
upper dirge
#

yeah, just copy the folder

ripe parrot
ripe parrot
upper dirge
upper dirge
#

now you need to open a terminal in that folder

#

or cmd in that folder

#

the embedding python folder

#

this is a sufficiently long process so bear with me

ripe parrot
#

no problem...

upper dirge
#

right

#

n

#

NO

#

wait

#

you need to make modification

#

to the file python/python311._pth

#

open that file in a text editor

ripe parrot
upper dirge
#

can you show me the source structure? the directory tree of "POS" folder

#

showing from within vscode with the tree expanded would help

ripe parrot
#

something like this. they all have three files each. the init.py, folder{eg auth/dailysales}.kv and the folder{eg auth/dailysales}.py

upper dirge
#

alright, the entry point is main.py?

ripe parrot
#

yes

upper dirge
#

now from within the command prompt you opened earlier

#

.\python\python.exe enter the interpreter and exit it (this will create site-packages directory)

ripe parrot
#

what next

upper dirge
#

yea, exit it

#

now you have to run the command .\python\python.exe pip.pyz install -r POS/requirements.txt

#

this should install ur requirements

ripe parrot
#

how do i exit?

upper dirge
#

exit()

#

wait what?

#

lol

#
import sys
sys.exit()
ripe parrot
#

finally worked. what was the previous instruction before i rudely interrupted asking about how to exit?

upper dirge
#

now you have to run the command .\python\python.exe pip.pyz install -r POS/requirements.txt

#

this shall install all your project requirements into the runtime/embedded python

ripe parrot
#

can i try to run it from the folder as it is right now to check if everything works?

upper dirge
#

for that cd POS

#

and run ..\python\python.exe main.py

#

does it work?

ripe parrot
upper dirge
#

yeah, what would that be?

ripe parrot
#

now am not sure if python main.py is using the global (not sure if that's the word) interpreter or if its using the python from the folder.

upper dirge
#

oh, so it is having trouble with importing

upper dirge
ripe parrot
#

when i run this:

upper dirge
#

run ..\python\python.exe

#

to get into the interpreter

ripe parrot
#

I get this/

upper dirge
#

and inside the interpreter

import sys
print(sys.path)
#

the issue isn't anything major that you are facing

#

it simply means that the path is incorrect

#

we can fix that

ripe parrot
#

C:\Users\dell\Desktop\Embedding Python\POS>python
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import sys

print(sys.path)
['', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\python312.zip', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\DLLs', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib', 'C:\Users\dell\AppData\Local\Programs\Python\Python312', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\site-packages', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\site-packages\win32', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\site-packages\win32\lib', 'C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\site-packages\Pythonwin']

upper dirge
#

not this, you are to do that in ..\python\python.exe (folder python)

ripe parrot
#

got you.

#

C:\Users\dell\Desktop\Embedding Python\POS>..\python\python.exe
Python 3.11.8 (tags/v3.11.8:db85d51, Feb 6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)] on win32

import sys

print(sys.path)
['C:\Users\dell\Desktop\Embedding Python\python\python311.zip', 'C:\Users\dell\Desktop\Embedding Python\python']

upper dirge
# upper dirge this

this suggests that you forgot to save the changes i asked you to make, are u sure they are saved?

ripe parrot
#

can you please remind me which changes. Come to think of it, i did not edit the python311._pth as you had said.

ripe parrot
upper dirge
#

ah, replace the file content of python311._pth

#

with that shown in the given image

ripe parrot
#

Should I delete the # Uncomment to run site.main() automatically highlited in the image?

upper dirge
#

sure, delete the comment. and uncomment the import site line

#

this file is like setting up PYTHONPATH for your runtime

ripe parrot
#

Got you. so now what do i need to do now?

upper dirge
#

right save it

ripe parrot
#

done

upper dirge
#

and inside the command prompt, run ..\python\python.exe main.py

#

and let me know if any errors/issues rise up

ripe parrot
#

C:\Users\dell\Desktop\Embedding Python\POS>..\python\python.exe main.py
Traceback (most recent call last):
File "C:\Users\dell\Desktop\Embedding Python\POS\main.py", line 16, in <module>
from app import MainApp
File "C:\Users\dell\Desktop\Embedding Python\POS\app_init_.py", line 14, in <module>
from kivy.app import App
ModuleNotFoundError: No module named 'kivy'

C:\Users\dell\Desktop\Embedding Python\POS>

I have had this error before and I just download kivy. should i do that here?

upper dirge
#

how do you download kivy? exactly?

#

using pip?

ripe parrot
#

pip install kivy

#

yeah

upper dirge
#

in that case you need to run ..\python\python.exe ..\pip.pyz install kivy

ripe parrot
#

its freaking working!!! Thanks man. what next?

upper dirge
#

so now your app works?

#

right?

ripe parrot
#

perfectly

upper dirge
#

right, for the sake of your client, you would probabbly like to have a POS.exe (a launcher at the Embeded Python folder)?

ripe parrot
#

yeah. assume that they are not tech savy and have no understanding of how to use a terminal.

upper dirge
#

i figured

#

in that case let's do a very simple file.

#

at the root of this all, inside Embedded Python folder, create a file POS.py

ripe parrot
#

that's outside the POS directory right?

upper dirge
#

yes

#

i'll write down the code you need inside that

#
import subprocess
import os

subprocess.run([".\\python\\python.exe", "POS\\main.py"], cwd=os.getcwd()+"\\POS")
#

this should do

#

once you are done saving it
try running this in the cmd after "cd .." so that you are in the root

python POS.py
#

let me know if any errors arise before we proceed to final step

ripe parrot
#

C:\Users\dell\Desktop\Embedding Python\POS>python POS.py
python: can't open file 'C:\Users\dell\Desktop\Embedding Python\POS\POS.py': [Errno 2] No such file or directory

upper dirge
#

you forgot to cd ..

#

you need to run it from the root (embedded python folder)

ripe parrot
upper dirge
#

uh righttt, sorry, error in my code indeed. let me write a fix

#
import subprocess
import os

subprocess.run(["..\\python\\python.exe", "main.py"], cwd=os.getcwd()+"\\POS")
#

this

#

after saving, now retry python POS.py

ripe parrot
#

C:\Users\dell\Desktop\Embedding Python>python pos.py
Traceback (most recent call last):
File "C:\Users\dell\Desktop\Embedding Python\pos.py", line 4, in <module>
subprocess.run(["..\python\python.exe", "main.py"], cwd=os.getcwd()+"\POS")
File "C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\dell\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

C:\Users\dell\Desktop\Embedding Python>

upper dirge
#

can you screenshot me the code?

#

of POS.py

ripe parrot
#

here it is.

upper dirge
#

hmm, how about replacing the double dot with a single dot

#

instead of ..\\python change it to .\\python

#

and try it again

ripe parrot
#

works like a freaking charm.

upper dirge
#

alright, time to finish it up

#

do the following one by one

pip install -U pyinstaller
pyinstaller pos.py --onefile
ripe parrot
#

while still in the same directory right?

upper dirge
#

yes

ripe parrot
#

nothing happened. do i need to create a virtual environment?

upper dirge
#

uh, did pyinstaller not work?

ripe parrot
#

It just loaded for a bit, then nothing. no errors.. no feedback. it just ran for a second then stopped.

upper dirge
#

this is indeed strange

#

if you can, please proceed to do the afformentioned steps in a venv

#

unfortunately, i do not know the ins and outs of venv so that is up to you

#

so, any progress?

ripe parrot
#

am getting this error in the finished version:

Traceback (most recent call last):
File "pos.py", line 4, in <module>
File "subprocess.py", line 548, in run
File "subprocess.py", line 1026, in init
File "subprocess.py", line 1538, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified

upper dirge
#

wait, did you copy pos.exe from dist to the embedded python folder?

ripe parrot
#

no, i am running it from the dist.

upper dirge
#

copy it to the embedded python folder

#

and then run it

ripe parrot
#

should i cut or just copy it?

upper dirge
#

if everything works, let me know and we shall proceed to cleanup

upper dirge
ripe parrot
#

dude. you are a freaking miracle worker. it works

upper dirge
#

now for the cleanup, please share a screenshot of windows explorer in "embedded python" folder

upper dirge
ripe parrot
upper dirge
#

alright

#

delete the folder named build and dist and new_venv

#

and then delete the file pip.pyz pos.py POS.spec

#

so finally you should have something like this:

POS
python
POS.exe
#

try running POS.exe one final time

ripe parrot
#

it freaking works. i was almost not sure about deleting the pos.py file, but it works without it. dude. you are a freaking genius. I have no idea how to thank you.

upper dirge
#

now, you can zip this folder and send it to your client.

#

no thanks necessary, i will use this post of yours as a reference for my own projects

ripe parrot
#

is there anyway to create like a shortcut that can be used on the desktop for easier access or is that a little bit too much to ask after all that.

upper dirge
#

that is indeed possible, but it would require tons more of code. i do intend to develop a tool in the very near future to automate this process.

ripe parrot
#

if you need a testimonial or something, or a comment, or anything, any time man. Any freaking time, just reach out. Thanks bruh.

upper dirge
#

For now, i dont think it would be possible

ripe parrot
#

i am perfectly fine with this. perfectly fine. I have been stuck here for days on end. days.... thanks man. Thanks.

upper dirge
#

You are most welcome, no formalities neccessary :P, now to finish this up, type in the command !close in discord to close this thread

ripe parrot
#

!close

vernal raptorBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.