I'm using pyinstaller to produce an exectuable for a python program. But, whenever I try to use a generated executable, I get a message window that reads "Failed to execute script ...".
The command I'm running is pyinstaller -w run.py. But, when run with the --debug option a different error is made apparent. "Failed to execute script pyiboot-1_bootstrap". After this error it kills the program.
I tried with the -F option and that did not change the outcome. All the files I created for the project are in the same directory. I did get warnings that dll's weren't being included but I resolved that.
Pyinstaller doesn't like the built in quit() function I was using.
Instead use sys.exit() to close the program.
See this question to see why sys.exit() is better practice for production code.
What I found useful was to make an exe of my .py script in console mode and then run it. For a very short period of time, I saw an error message in the console window. I had to screenshot it to read completely. So one of the imports was missing from the folder in which I had my script. I opened cmd from the location bar of that folder (by typing cmd there) and imported modules using pip install <module_name>.
Sorry for the long answer, I am a newbie.
Related
I'm running windows
I've got a python script that works as expected when I run it from my interpreter (Anaconda, i think that's an interpreter...), but when I run it from my file manager by double clicking the script ("script.py") I see a quick black screen flash, but nothing else. I'm using the input() function, so that's not the issue.
When I try the same thing with a simple test script, which is just the print() function and the input function, the command line or terminal screen, whatever the black screen is called, displays my printed string and closes only after I press enter.
I believe my issue has to do with file paths and working directories, but I'm a noob and I don't know how to solve this. My script declares this variable:
inv_folder=r"C:\Users\domin\OneDrive\Desktop\Test Folder" #folder to look for completed recipe files
which I believe is an absolute filepath. I then loop through the files in that folder and open and read them
for filename in os.listdir(inv_folder):
Main goal is to send my script to someone else's computer, and allow them to run it simply by double clicking on the file. Trying to do that on my computer and failing
Doubleclicking the .py file in Windows will generally run the file. In your case, if all your script is doing is looking for a directory and looping through files in it, it is quite possible that the script is working correctly, but that it is closing immediately upon completing the script.
E.g. if you have the following python file hello.py with the single line of code.
print("hello world")
If you doubleclick hello.py, you will only see a black window flash for a second, as the program runs and exits.
To check whether your code is working correctly or not, you are best to navigate to that folder in your command prompt and run the program within the command prompt. E.g. typing "hello.py" in the right folder will then show the correct output in the console.
Try running it in the console first, seeing if the code is giving any errors, and then working from there. But the behaviour you are describing is basically standard Windows behaviour and not necessarily an error.
If you need to pause the window after running, please see the following StackOverflow question and answers about "Python Equivalent to System('PAUSE')"
Have you considered to compile it?
You can compile it easily with pyinstaller and this command:
C:\<path_to_python>\Scripts\pyinstaller.exe --onefile -n <your script name> <your script path>
Of course replace the <> with the relevant paths for you.
I tried to execute a try except command on the app to get log of the error but it still crash so I think it is a problem of PATH ? library ?
Why does it work with pyinstaller and not after I created an installer.nsi with HM NSIS edit ?
I'm sorry but I have no idea of how to debug it !
It is a "simple" project. tkinter app, excel creation, 1 thread...
I don't know from where it comes from (I'm not good in system and OS).
PS: even stranger, when I install the app with Install.exe, if I decide to launch the app directly it works !!!
But it never work a second time when I use the shortcut or the .exe in C:\Program Files (x86)\MYDIRECTORY.
This a permission problem, I tried to write on a protected file.
Solution to debug:
do not use --noconsole when you run pyinstaller, it will let you the console and with windows+G, you can keep track of the error even if the terminal stop immediatly after the crash of the programm.
(I tried to register the error in a .json in a try except but that the thing that made my program crash !)
(before start, i'm not good at eng.)
i'm making a python application.
everything was completed.
but, pyinstaller don't work well.
the file's name is "shojo.py".
i used this code.
pyinstaller -w -F shojo.py
and if execute the file,
this window appears.
error image
this time, i used this code.
pyinstaller -w -F -p D:\python\Lib\site-packages\PyQt5\Qt\bin shojo.py
but the result was same.
this is the log.
https://github.com/shojoinfo/shojoinfo/blob/master/shojo/source/log%2Ctxt
and i found the file "warn-shojo.txt"
this is the file's contents.
https://github.com/shojoinfo/shojoinfo/blob/master/shojo/source/warn-shojo.txt
what should i do?
Not sure if this is the cause, but from the logs I see you need to give path to install those missing modules.
See here:
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
I have a technical questions on python script.
I have developed code on my laptop and I want to move it in another one.
I installed python there, put when I try to double click on the .py file it is not working. Like the cmd screen goes away in a second.
Do you know why?
(if I open python through cmd it is working, so it is in path and it works)
What is probably happening is that there is an error being thrown on your new computer which causes command prompt to just instantly close.
The best way to run a python script is from an open command prompt/terminal. To do this open a command prompt and move into the directory of your python file. For example, if the file you are trying to run is located at C:\Users\Davide\PythonScripts, then open a command prompt and type
cd C:\Users\Davide\PythonScripts
Now your command window is in the folder that you want to run files out of.
Next you want to tell Python to run your script. This can be done by typing "python ". For example, if your script is name my_script.py, you would type
python my_script.py
What this is doing is telling your computer "open an instance of Python where you are running my_script.py." If there are any errors thrown, the command window will stay open after python closes and you can see what is going on.
Most likely, there is a package you are trying to import which it cannot find because it was not installed on your other computer. If at the top of your file you have "import xxx" or "from xxx import yyy" lines, your other computer might not be finding those modules and just throwing an error that instantly closes command prompt when you just double click the .py file.
I'm trying to make my python program run on other systems without python installation. I made use of tkinter to create a gui, after creating an exe file with pyinstaller it throws a fatal error "Failed to run script". I've checked my code several times and it works well. I don't know what's wrong.
Create the file using the -F switch only and run from your command line. You should see the actual error.
build: pyinstaller -F your_script.py
run: C:\some_dir\dist\your_script.exe
I have run into issues like this when a module is in a directory that is lowercase and pyinstaller is looking for an uppercase first character. e.g. C:\Python27\Lib\site-packages\queue vs C:\Python27\Lib\site-packages\Queue
The error is very generic and at this point, it may not be possible to find the actual error what preventing the execution.
In order to get actual error please exclude '--windowed' or '--noconsole' parameters during installation and then execute with 'pyinstaller -filename.py --onefile'. Then on execution it will show exact error instead of 'Fatal error! Failed to execute script'. Then you can proceed accordingly.