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!!!
#๐ Kivy to .exe executable errors
197 messages ยท Page 1 of 1 (latest)
@ripe parrot
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.
Closes after a period of inactivity, or when you send !close.
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.
- use the embedded python edition, download the zip from official python website
- install all necessary modules into the embedded python edition (this is a special process)
- place your modules/source alongside the embedded python
- 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
Show your resource_path helpers?
Do you get any error messages?
@ripe parrot ^
I saw you posted it, but it disappeared
wow... that's sound technical. okay. so I go to the official python website and download... I am trying to figure out the basics as it is, and have no idea how to use embedding python.
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):
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.
I think you want to use importlib.resources not MEIPASS
next step?
do you mind rewriting the function and then giving me the feedback.
Well it depends where views is stored as a python package
after you are done extracting the zip
download this file and save it in the folder
https://bootstrap.pypa.io/pip/pip.pyz
Like how would you import views.dailysales
should i just drug and drop it?
next?
uh can you show me how you have saved the files? screenshot the folder i asked you to create
here is an example: from views.auth.auth import StateManager StateManager is an empty class in the auth folder... all pages are structured that way.
the files are in the embedding python folder.
uh make a new folder
and copy that folder within it
we need to keep the python runtime clean
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))
like this or should the new folder be empty?
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"
What error message did you get from kivy?
dude am sorry, got discconected there for a minute.
not a problem, just copy ur source to the embedding python folder
no errors when am compiling the code. it runs perfectly. like if I run it on my computer, it works without a hitch. but if i use pyinstaller, it completes the compilation with no errors, but displays a black screen.
You don't get a runtime error?
Like FileNotFoundError?
non errors whatsoever. just a black screen on complete.
the entire source code. should i just add it as a folder, or what should i do?
yeah, just copy the folder
Like this?
after that step, what next? do i run it from this new folder?
yes
noop
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
right
n
NO
wait
you need to make modification
to the file python/python311._pth
open that file in a text editor
c
this
can you show me the source structure? the directory tree of "POS" folder
showing from within vscode with the tree expanded would help
something like this. they all have three files each. the init.py, folder{eg auth/dailysales}.kv and the folder{eg auth/dailysales}.py
yes
now from within the command prompt you opened earlier
.\python\python.exe enter the interpreter and exit it (this will create site-packages directory)
what next
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
how do i exit?
finally worked. what was the previous instruction before i rudely interrupted asking about how to exit?
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
can i try to run it from the folder as it is right now to check if everything works?
yeah, what would that be?
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.
oh, so it is having trouble with importing
its using python from interpreter
when i run this:
I get this/
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
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']
not this, you are to do that in ..\python\python.exe (folder python)
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']
this suggests that you forgot to save the changes i asked you to make, are u sure they are saved?
can you please remind me which changes. Come to think of it, i did not edit the python311._pth as you had said.
how do i do this?
Should I delete the # Uncomment to run site.main() automatically highlited in the image?
sure, delete the comment. and uncomment the import site line
this file is like setting up PYTHONPATH for your runtime
Got you. so now what do i need to do now?
right save it
done
and inside the command prompt, run ..\python\python.exe main.py
and let me know if any errors/issues rise up
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?
in that case you need to run ..\python\python.exe ..\pip.pyz install kivy
its freaking working!!! Thanks man. what next?
perfectly
right, for the sake of your client, you would probabbly like to have a POS.exe (a launcher at the Embeded Python folder)?
yeah. assume that they are not tech savy and have no understanding of how to use a terminal.
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
that's outside the POS directory right?
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
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
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
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>
here it is.
hmm, how about replacing the double dot with a single dot
instead of ..\\python change it to .\\python
and try it again
works like a freaking charm.
alright, time to finish it up
do the following one by one
pip install -U pyinstaller
pyinstaller pos.py --onefile
while still in the same directory right?
yes
nothing happened. do i need to create a virtual environment?
uh, did pyinstaller not work?
It just loaded for a bit, then nothing. no errors.. no feedback. it just ran for a second then stopped.
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?
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
wait, did you copy pos.exe from dist to the embedded python folder?
no, i am running it from the dist.
should i cut or just copy it?
if everything works, let me know and we shall proceed to cleanup
cut is also fine
dude. you are a freaking miracle worker. it works
now for the cleanup, please share a screenshot of windows explorer in "embedded python" folder
(ofcourse it does, i recently developed this specific procedure for my own app)
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
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.
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
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.
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.
if you need a testimonial or something, or a comment, or anything, any time man. Any freaking time, just reach out. Thanks bruh.
For now, i dont think it would be possible
i am perfectly fine with this. perfectly fine. I have been stuck here for days on end. days.... thanks man. Thanks.
You are most welcome, no formalities neccessary :P, now to finish this up, type in the command !close in discord to close this thread
!close
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.