I compiled my Python GUI with Pyinstaller on Windows 10 but it seems like it cannot find my other script even though I provided the hard-coded absolute path to it (with r'"C:\Program Files...script path..."'). I even tried os.isfile (script path) but it returns False. The python script was compiled with pyinstaller --onefile --windowed --icon=iconimage.ico myscript.py from the command prompt. I use this same command on Ubuntu and the binary works just fine. I read something about Pyinstaller creating a temporary directory which I found, but I don't think it matters where it's running from as long as I give it the full path, so I'm thinking maybe I need more options when compiling? The GUI opens just fine. It's when it needs to call the script that it doesn't do anything. There are no errors when I run it from the command prompt. Please help!
Solved by adding --onedir which will put everything needed to run the program in one directory in the dist folder.
Related
I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation:
"failed to execute script new-app"
new-app is my python GUI program. When I run pyinstaller using this command:
pyinstaller.exe --onedir --hidden-import FileDialog --windowed --noupx new-app.py
It does work smoothly. In addition, when I execute the command line to run the gui program, it works perfectly and the GUI is generated using this command:
.\dist\new-app\new-app.exe
But when I go to that file hopefully to be able to click the app to get the GUI, it gives me the error said above. Why is that?
I am using python2.7 and the OS is Windows 7 Enterprise.
Any inputs will be appreciated and thanks a lot in advance.
Well I guess I have found the solution for my own question, here is how I did it:
Eventhough I was being able to successfully run the program using normal python command as well as successfully run pyinstaller and be able to execute the app "new_app.exe" using the command line mentioned in the question which in both cases display the GUI with no problem at all. However, only when I click the application it won't allow to display the GUI and no error is generated.
So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error which made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.
The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!
So The Simple answer is to place all the media files or folders which were used by code in the directory where exe file is there.
Second method is to add "--add-data <path to file/folder>"(this can be used multiple times to add different files) option in pyinstaller command this will automatically put the given file or folder into the exe folder.
In my case i have a main.py that have dependencies with other files. After I build that app with py installer using this command:
pyinstaller --onefile --windowed main.py
I got the main.exe inside dist folder. I double clicked on this file, and I raised the error mentioned above.
To fix this, I just copy the main.exe from dist directory to previous directory, which is the root directory of my main.py and the dependency files, and I got no error after run the main.exe.
Add this function at the beginning of your script :
import sys, os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Refer to your data files by calling the function resource_path(), like this:
resource_path('myimage.gif')
Then use this command:
pyinstaller --onefile --windowed --add-data todo.ico;. script.py
For more information visit this documentation page.
In case anyone doesn't get results from the other answers, I fixed a similar problem by:
adding --hidden-import flags as needed for any missing modules
cleaning up the associated folders and spec files:
rmdir /s /q dist
rmdir /s /q build
del /s /q my_service.spec
Running the commands for installation as Administrator
I was getting this error for a different reason than those listed here, and could not find the solution easily, so I figured I would post here.
Hopefully this is helpful to someone.
My issue was with referencing files in the program. It was not able to find the file listed, because when I was coding it I had the file I wanted to reference in the top level directory and just called
"my_file.png"
when I was calling the files.
pyinstaller did not like this, because even when I was running it from the same folder, it was expecting a full path:
"C:\Files\my_file.png"
Once I changed all of my paths, to the full version of their path, it fixed this issue.
I got the same error and figured out that i wrote my script using Anaconda but pyinstaller tries to pack script on pure python. So, modules not exist in pythons library folder cause this problem.
That error is due to missing of modules in pyinstaller. You can find the missing modules by running script in executable command line, i.e., by removing '-w' from the command. Once you created the command line executable file then in command line it will show the missing modules. By finding those missing modules you can add this to your command :
" --hidden-import = missingmodule "
I solved my problem through this.
I had a similar problem, this was due to the fact that I am using anaconda and not installing the dependencies in pip but in anaconda. What helped me was to install the dependencies in pip.
I found a similar issue but none of the answers up above helped. I found a solution to my problem activating the base environment. Trying once more what I was doing without base I got my GUI.exe executed.
As stated by #Shyrtle, given that once solved my initial problem I wanted to add a background image, I had to pass the entire path of the image even if the file.py and the image itself were in the same directory.
In my case (level noob) I forgot to install library "matplotlib". Program worked in Pycharm, but not when I tried open from terminal. After installed library in Main directory all was ok.
I have created a simple Python script test.py that is supposed to check if a directory exists, and, if it does not, to create it.
The code is as follows:
import os
def crete_dir(dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
crete_dir("test_dir")
I am on Mac and use PyCharm to write and test my code.
The script works as expected so long as I run it in PyCharm, but as soon as I turn it into an executable (using pyinstaller: pyinstaller --onefile --clean --windowed test.py) and run it, it does not work (does not create the directory, even though it does not exist). The terminal window, that opens alongside the executable, does not return any file-access-permission or other error.
What should I please do to make the executable work normally?
Many thanks.
I've been stuck on the very last step of creating my program.
I am trying to pack it in to an executable file using pyinstaller, but there is this weird bug (?), I've come across.
I tried packaging it using
pyinstaller --icon=64x64.ico --noconsole file.py
Which results into build folder, spec file and dist folder, but the dist folder does not contain exe file. If, however, I remove the --noconsole part, everything works fine, though, with the console window in the background when running. The code does not interact with the console at all, and I even tried using it with the console open, to see it if displays anything, and the results were negative.
Any ideas what to look for?
EDIT:
Managed to solve the problem, but further investigating #VictorS's suggestion with pyw extension. Tried running pyinstaller kalkulators_v2.0.pyw in CMD , which managed to create all the files in dist folder and also surpress the console
So I'm using pyinstaller with python27, and my exe works great so long as it's in the same directory as the build folder. I need it to be a completely standalone exe, without any dependencies, is there a way to bundle the important things from the build folder into one file? Neither -F nor --onefile seems to do this.
Edit: as I explain in my answer below, I thought pyinstaller was the problem because the exe would only run in the dist folder, so I assumed it had dependencies there, but in reality, it was running and then instantly crashing due to a bug that only triggered when the exe was on the desktop.
I figured out that the reason it wasn't working had nothing to do with pyinstaller or dlls. The exe was opening, and and trying to input powershell commands via python like it was supposed to. Unfortunately I had a line of code that said this:
subprocess.check_output('schtasks /create /sc minute /mo ' + str(time) + ' /tn "test_process_to_run_every_'+str(time)+'_min" /tr //'+sys.argv[0],shell=True)
#set this exe to run every X minutes in windows scheduled tasks
the problem was that sys.argv[0] changed when I put the exe on the desktop, and ended up being a path that looked like C://Users/John Smith/Desktop. The space in between John and Smith made powershell mad and crashed the program, so I escaped it using this line of code:
path = sys.argv[0].replace(" ","^")
and then I replaced sys.argv[0] with my new path variable. Hope this helps anyone in the future trying to do the same thing.
after pyinstaller has converted your script into .exe, than you need to add the executable to path, otherwise you have to open the command line in the directory that the file is in. pyinstaller just puts your script and py interpretor into a single file. same goes for linux.
for dependency side, look here.
there are other options you can try to bbFreeze, py2exe, cx_Freeze
to use pyinstaller in a simple way:
pyinstaller --onefile your_file.py
now you should see couple of files build, dist(exe in here).
NOTE: that --onefile flag doesn't necessarily get rid of the need for it to have link with certain libraries, it will still need those in order to run.
prepare for distribution, first need to get a spec file:
to get a spec file:
pyinstaller --noconsole your_file.py
than you can get the exe file for distribution like so:
pyinstaller your_file.spec
for more info and tutorial look here
see if nuitka works for you, it might sound scary but it is not. it compiles your code to executable binary format. Be aware that under the hood first it converts to c++ API calls.
if you dont like that for closed source program use Cython, and for no dependency use py2exe
I want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
it's been a while since i tried on linux, but i believe it should be fairly simple, firstly you need to put a shebang at the top of the script so your shell knows which executable to use:
#!/usr/bin/python
or if you want a specific version you can expand this to:
#!/usr/bin/python3.2
using whichever version you want (only works for first 2 digits)
then you need to mark it as executable:
chmod 711 myfile.py
for more info on this see this page
then when you double click it, on the rpi (last i used linux) it asks if you want to execute it, or execute it in the terminal.
if you choose to execute it without the terminal, you should only see the tkinter GUI
You can use pyinstaller to create the executables for the different platforms you want.
For example,
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
pyinstaller