Python PyInstaller and include icon file - python

I am building an executable using PyInstaller (v1.5) -- also, I am using GUI2EXE to assist me. My script calls for an icon file located in the working directory:
icon_file = '\pics\myicon.ico'
When I build the executable, i would like to not have to include the "working directory", rather have the icon file included right in my executable. Is there a way to do this?
If this doesn't make since, I want one file (my executable) that includes my icon file.
How do I create one file to include the icon files?
How do I update my script code to access that icon file correctly?
I'm also looking to do this for any image file or even data file...
Thanks!

So rather than the actual icon of the distributed executable, you're looking at including an icon or other image that your python script will open/read from disk at runtime?
Additional data can be bundled in your exe, and pyinstaller provides a method of accessing it at runtime. This question shows how to include it in your .spec, and the answer shows how to access it via your script:
Bundling data files with PyInstaller (--onefile)

I'm not very familiar with GUI2EXE, but you can specify an icon file (scroll down a bit for the Windows/OS X specific options) on the command line while you're building the spec file. That'll set it as the application's icon and you won't have to distribute the icon separately. Looking at the spec file it generates, it adds an icon argument in the exe like so:
exe = EXE(pyz,
a.scripts,
[bunch of other arguments],
icon='\pics\myicon.ico')

Related

pyinstaller move .exe file to Desktop

I made GUI application with PyQt and wrapped it up with pyinstaller.
As you know, make it for onefile (sole exe file) is quite slow.
So, what I want to do is wrap it by using pyinstaller but not in onefile, but i still don't want to execute the exe file in the folder. I want to move the exe file to desktop directory so I can use it like onefile. I tried it but it says there is no .dll on desktop directory.
I assume that there is quite simple solution.
How can I solve this problem?
Keep the file in the folder and create a shortcut on the desktop instead. You can have also shortcuts in the Start menu for example or pin the exe (create shortcut) in TaskBar. If you are going to distribute it a simple installer can automate the creation of the said shortcuts.

How to convert Python package to exe?

Our Programs is not always a Python source file !.
The Python may be associated with a file or an image,
that is located inside the source of the application to the file path inside the package.
For example, when setting the application icon in Tkinter Or Include image for app background
root.iconbitmap('favicon.ico')
PhotoImage(file = 'python_logo.gif')
In Pyinstaller only the source file expires in the exe format.
If the program contains a file or image path inside the source, then the program will not be executed , In that event,The source of the application and files are in a package (on a route).
Please provide a solution to this problem, So as possible convert a package containing source and file into exe
1-Install cx_Freeze, (open your command prompt and type pip install cx_Freeze
2-Install idna, (open your command prompt and type pip install idna.
3-Write a .py program named myprogram.py
4-Create a new python file named setup.py on the current directory of your script.
5-In the setup.py, code below and save it.
6-With shift pressed right click on the same directory, so you are able to open a command prompt window.
7-In the prompt, type python setup.py build
8-If your script is error free, then there will be no problem on creating application.
9-Check the newly created folder build. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
this works on python 3.5 and above!
Add heres the open source project to convert to exe
https://github.com/brentvollebregt/auto-py-to-exe
I have not used Pyinstaller myself but in cx_Freeze I know you can include none project files like images in the setup file. With Pyinstaller you want to look into the spec file. Here is a link to the docs where it talks about added files to project. https://pythonhosted.org/PyInstaller/spec-files.html#adding-files-to-the-bundle I hope thisis of some help.

Windows- Pyinstaller unless folder contains file appear error “failed to execute script"

I have a problem to over come the following error, when i am running my program packed using pyinstallet into standalone executable.
In the program i am using an icon file to set iconbitmap. The file is located in the same folder as the python script.
master = Tk()
master.wm_title("P&P Util ")
master.iconbitmap('logo.ico')
In addition i am adding and icon to the executable in the packing process.
pyinstaller -F -w -i "C:\temp\Util\logo.ico" main.py
Now after some testing i located that when i add file itself to the packed executable, the program is running without any issues. I understand that i need to change the way i am using the file but i don't know how.
Your advice will be very much appreciated.
It looks like it is crashing because you have not added the "logo.ico" file to the package, so when master.iconbitmap('logo.ico') looks for the icon, it is not there. With the command -i 'C:\temp\Util\logo.ico' you are telling pyinstaller to set that icon file as the windows icon, but this is not the same as including it in the package. To do that try including this in your pyinstaller command:
--add-data "C:\temp\Util\logo.ico;."
The "." after the semi-colon tells pyinstaller where to put the file. In this case it will put it into the same folder as the .exe file.

Compiling Python into a windows application

Currently I am working on a fork of someone else's project, that is written in the Python programming language.
I have access to all the source code I need, with all the changes I wanted to make, and everything 'set' to how I need it.
My current step is trying to somehow compile it, so it runs as windows in a stand-alone application. I know this is possible because this is how the source-application runs. Currently I have access to Visual Studio with the python extension module, WinPython, the kivy framework itself that the GUI was built with, etc.
But I cannot seem to figure out how to do this. My cursory research has suggested a program called py2exe but that does not work with what I need, based on what I can tell.
copied from http://kivy.org/docs/guide/packaging-windows.html
Create the spec file
For this example, we’ll package the touchtracer example and embed a custom icon. The touchtracer example is the kivy\examples\demo\touchtracer directory and the main file is named main.py.
Double click on the Kivy.bat and a console will open.
Go to the pyinstaller 2.1 directory and create the initial spec:
cd pyinstaller-2.1
python pyinstaller.py --name touchtracer ..\kivy\examples\demo\touchtracer\main.py
You can also add an icon.ico file to the application folder in order to create an icon for the executable. If you don’t have a .ico file available, you can convert your icon.png file to ico using the web app ConvertICO. Save the icon.ico in the touchtracer directory and type:
python pyinstaller.py --name touchtracer --icon ..\kivy\examples\demo\touchtracer\icon.ico ..\kivy\examples\demo\touchtracer\main.py
For more options, please consult the PyInstaller 2 Manual.
The spec file will be touchtracer.spec located in inside the pyinstaller + touchtracer directory. Now we need to edit the spec file to add kivy hooks to correctly build the exe. Open the spec file with your favorite editor and add theses lines at the beginning of the spec:
from kivy.tools.packaging.pyinstaller_hooks import install_hooks
install_hooks(globals())
In the Analysis() function, remove the hookspath=None parameter. If you don’t do this, the kivy package hook will not be used at all.
Then you need to change the COLLECT() call to add the data for touchtracer (touchtracer.kv, particle.png, ...). Change the line to add a Tree() object. This Tree will search and add every file found in the touchtracer directory to your final package:
coll = COLLECT( exe, Tree('../kivy/examples/demo/touchtracer/'),
a.binaries,
#...
)
We are done. Your spec is ready to be executed!
Build the spec
Double click on Kivy.bat
Go to the pyinstaller directory, and build the spec:
cd pyinstaller-2.1
python pyinstaller.py touchtracer\touchtracer.spec
The package will be in the touchtracer\dist\touchtracer directory.

A .py file which compiled from .qrc file( using pyside-rcc ) does not work

I am working on python project and I have a problem with my .py file which complied from .qrc file. First, let I explain briefly about my project.
I created my project GUI in QtDesigner and also use the image in the GUI. Then, I generate .py from .ui file using pyside-uic and generate .py file from .qrc file using pyside-rcc. The problem is when I use the .py file (an image file), images does not show in my GUI.
Is anybody knows how to solve this problem?
Thank you for all your answer. :)
Ps. I use PySide as my GUI language.
Have you actually added the resource to your GUI project?
Starting from scratch, here's how to do it:
In Qt Designer, select View > Resource Browser. Then, in the Resource Browser, click the Edit Resources button. From there, you can either create a new resouce file, or open an existing one. The important thing is that the resource file must be able to access the resources (e.g. image files) using relative paths. So that means they must be either in the same directory as the resource file, or one of its sub-directories.
Once you've created the resource file, add a prefix (e.g. "images") using the buttons below the right-hand pane, then add your images (or whatever), and finally click Ok.
Now when you want to add a pixmap to a label, just make sure you select the image from your new resource, rather than the file on disk.
The final steps are to re-generate your GUI module using pyside-uic and generate the resources module using pyside-rcc. When you do this, make sure the resources module is saved as "resource_rc.py" in the same directory as the GUI module:
pyside-uic -o widget.py widget.ui
pyside-rcc -o resource_rc.py resource.qrc
(PS: if your are using packages in your project, you should use the --from-imports option with pyside-uic to get relative imports).

Categories