py2exe generate custom Icon - python

So this is a question that has been asked many times. And I followed all the things found on the interwebs, however. My icon just isn't appearing, and I'm not getting any sort of error message. The rest of my program functions fine, it's just the darn ugly icon.
Here's my setup.py file, please let me know if/what I'm doing wrong? Sorry if there is a dumb error. :(
import os, os.path, sys
import subprocess
from distutils.core import setup
import py2exe
import glob
import numpy
sys.argv.append('py2exe')
target = {
'script' : "MY_PROGRAM.py",
'version' : "1.0",
'company_name' : "MY_COMPANY",
'copyright' : "",
'name' : "PROGRAM_NAME",
'dest_base' : "PROGRAM_NAME",
'icon_resources': [(1, "MY_ICON.ico")]
}
opts = {
'py2exe': { 'includes': ['matplotlib.numerix.random_array', 'dbhash',
'anydbm', 'skimage', 'pymorph', 'register'],
'excludes': ['_gtkagg', '_tkagg'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll'],
'bundle_files': 1
}
}
setup(
data_files = [('Images', glob.glob('Images/*.*'))],
windows = [target],
zipfile = None
)

....
For some reason it works now. I used a different website to convert my png file to a .ico, and voila magic.
:( so much struggles

Related

Adding an icon for my exe file with CX_freeze

I am converting a .py file to a .exe file using cx_freeze. Which is working but I can not seem to change it so that my .exe file has the custom icon I have. This is what I have tried so far:
'''
import sys
import os
from cx_Freeze import setup, Executable
sys.path.append(os.path.abspath("./src/"))
sys.path.append(os.path.abspath("./src/gui/rc/"))
**Dependencies are automatically detected, but it might need fine tuning.**
buildOptions = {
"packages": ["src.gui",
"src.qt_models",
"src.data",
"src.libs",
"src.tguiil",
"src.graphics"
],
"includes": ["scipy.sparse.csgraph._validation",
"scipy.ndimage._ni_support",
"scipy._distributor_init"
],
"include_files": ["database/"],
"excludes": []
}
installOptions = {}
bdistOptions = {}
base = None
** Uncomment for GUI applications to NOT show cmd window while running.**
if sys.platform =='win32':
base = 'Win32GUI'
executables = [
Executable(script = 'src/facile.py', base=base, targetName = 'facile.exe', icon = 'facade_logo.ico')
]
setup(name='Facile',
version = '1.0',
description = 'A platform for generating Python APIs used to control graphical user interfaces.',
options = {
"build_exe": buildOptions,
"install_exe": installOptions,
"bdist_msi": bdistOptions,
},
executables = executables)
'''
I have no idea what could be wrong.
Figured it out! Just needed to add relative path to file and add distutils as a package to handle external dependencies:
'''
buildOptions = {
"packages": [
# Facile sub-packages
"src.gui",
"src.qt_models",
"src.data",
"src.libs",
"src.tguiil",
"src.graphics",
# External dependencies
"distutils"
],
'''

Getting 'tuple' object has no attribute 'split' when using py2exe

I am using Py2exe to create a executable app for my GUI and this is my setup code:
import matplotlib
from distutils.core import setup
import FileDialog
import zmq.libzmq
import py2exe
setup(
data_files=[matplotlib.get_py2exe_datafiles(),(zmq.libzmq.__file__,)],
console = [{'script': 'SVS-virtual-lib2.py'}],
options={
'py2exe': {
'packages': ['FileDialog'],
'includes': ['zmq.backend.cython'],
'excludes': ['zmq.libzmq'],
'dll_excludes': ['libzmq.pyd']
}
}
)
But i get the following error:
File "C:\Users\nzarinabad\AppData\Local\Continuum\Anaconda\lib\distutils\util.py", line 128, in convert_path
paths = string.split(pathname, '/')
File "C:\Users\nzarinabad\AppData\Local\Continuum\Anaconda\lib\string.py", line 294, in split
return s.split(sep, maxsplit)
AttributeError: 'tuple' object has no attribute 'split
Dose anyone who why i get the error and how to fix it?
Thank you
Please see the documentation, if you want to combine matplotlib.get_py2exe_datafiles() with other files, you have to do some manual work:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import os
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
matplotlibdata_files = []
for f in matplotlibdata:
dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
matplotlibdata_files.append((os.path.split(dirname)[0], [f]))
matplotlibdata_files.append(zmq.libzmq.__file__)
# ...
setup(
data_files=matplotlibdata_files,
# rest of your code

Qt platform plugin 'windows' - py2exe

I know there are many posts about this problem (i've read them all).
But i still have a problem with my exe, still cannot be opened.
I've tried to put the qwindows.dll (i tried with 3 different qwindows.dll) in the folder dist with my exe but doesn't change anyhting.
I've tried with libEGL.dll, nothing.
Any suggestions ? Is there a way to avoid having this problem ?
I've had this issue aswell, after a lot of digging I found the following solution:
Copy the following file next to you main .exe:
libEGL.dll
Copy the following file in a folder "platforms" next to you main .exe:
qwindows.dll
Putting the qwindows.dll in the subfolder is the important part I think, hope this helps
Try:
from setuptools import setup
import platform
from glob import glob
from main import __version__, __appname__, __author__, __author_email__
SETUP_DICT = {
'name': __appname__,
'version': __version__,
'description': 'description',
'author': __author__,
'author_email': __author_email__,
'data_files': (
('', glob(r'C:\Windows\SYSTEM32\msvcp100.dll')),
('', glob(r'C:\Windows\SYSTEM32\msvcr100.dll')),
('platforms', glob(r'C:\Python34\Lib\site-packages\PyQt5\plugins\platforms\qwindows.dll')),
('images', ['images\logo.png']),
('images', ['images\shannon.png']),
),
'options': {
'py2exe': {
'bundle_files': 1,
'includes': ['sip', 'PyQt5.QtCore'],
},
}
}
if platform.system() == 'Windows':
import py2exe
SETUP_DICT['windows'] = [{
'script': 'main.py',
'icon_resources': [(0, r'images\logo.ico')]
}]
SETUP_DICT['zipfile'] = None
setup(**SETUP_DICT)
copy the dependency manually is a bad way to do, because py2exe take care of it. With pyqt5, this setup works, BUT if I try in other computer without pyqt install the exe crashes. I migrated to pyqt4 and run in all computers.
For me it was enough to copy qwindows.dll to platforms folder, like #Inktvisje wrote.
And don't repeat my mistake: don't download this dll from Internet! Copy it from your Python libs folder: YourPythonFolder\Lib\site-packages\PyQt5\plugins\platforms.

Bundling GTK3+ with py2exe

Platform is Windows 7 64bit using python 2.7 and GTK3+ installed from http://sourceforge.net/projects/pygobjectwin32/files/?source=navbar
The exe is compiled but fails to run, due to this
The following modules appear to be missing
['gi.repository.Gdk', 'gi.repository.Gtk', 'overrides.registry']
How can i properly include these files?
imports in my .py file
from gi.repository import Gtk, Gdk
my setup file
#!/usr/bin/env python
from distutils.core import setup
import py2exe, sys
sys.path.append("C:\Python27\Lib\site-packages\gnome")
sys.path.append("C:\Python27\Lib\site-packages\repository")#tried including these extra dirs
sys.path.append("C:\Python27\Lib\site-packages\override")#tried including these extra dirs
sys.path.append("C:\Python27\Lib\site-packages\gi") #tried including these extra dirs
setup(
options = {
'py2exe': {
'bundle_files': 1,
#this does not work 'includes': ['Gtk']
}
},
console=["gui.py"],
zipfile=None
)
The executable error when ran:
ImportError: MemoryLoadLibrary failed loading gi\_gi.pyd
Thanks
You need to add "gi" to "packages".
'options': {
'py2exe': {
'packages': 'gi',
}
}
I haven't tested it on 64bit but this is the setup.py I've used to build with cx_freeze, py2exe looks like is not maintained for a long time.
from cx_Freeze import setup, Executable
import os, site, sys
## Get the site-package folder, not everybody will install
## Python into C:\PythonXX
site_dir = site.getsitepackages()[1]
include_dll_path = os.path.join(site_dir, "gtk")
## Collect the list of missing dll when cx_freeze builds the app
missing_dll = ['libgtk-3-0.dll',
'libgdk-3-0.dll',
'libatk-1.0-0.dll',
'libcairo-gobject-2.dll',
'libgdk_pixbuf-2.0-0.dll',
'libjpeg-8.dll',
'libpango-1.0-0.dll',
'libpangocairo-1.0-0.dll',
'libpangoft2-1.0-0.dll',
'libpangowin32-1.0-0.dll',
'libgnutls-26.dll',
'libgcrypt-11.dll',
'libp11-kit-0.dll'
]
## We also need to add the glade folder, cx_freeze will walk
## into it and copy all the necessary files
glade_folder = 'glade'
## We need to add all the libraries too (for themes, etc..)
gtk_libs = ['etc', 'lib', 'share']
## Create the list of includes as cx_freeze likes
include_files = []
for dll in missing_dll:
include_files.append((os.path.join(include_dll_path, dll), dll))
## Let's add glade folder and files
include_files.append((glade_folder, glade_folder))
## Let's add gtk libraries folders and files
for lib in gtk_libs:
include_files.append((os.path.join(include_dll_path, lib), lib))
base = None
## Lets not open the console while running the app
if sys.platform == "win32":
base = "Win32GUI"
executables = [
Executable("main.py",
base=base
)
]
buildOptions = dict(
compressed = False,
includes = ["gi"],
packages = ["gi"],
include_files = include_files
)
setup(
name = "test_gtk3_app",
author = "Gian Mario Tagliaretti",
version = "1.0",
description = "GTK 3 test",
options = dict(build_exe = buildOptions),
executables = executables
)
Depending on the libraries you have used you might have to add some missing dll, look at the output of cx_freeze.
I've posted the same some time ago on gnome's wiki:
https://wiki.gnome.org/Projects/PyGObject#Building_on_Win32_with_cx_freeze

Replace string in Python and open browser with result

I am trying to build a plugin for the program Sublimetext2.
It uses plugins coded with Python. I have no Python knowledge at all but from looking at existing plugins and my PHP knowledge here is what I need help with...
this is the start of the Python file so far
import sublime, sublime_plugin
import webbrowser
settings = sublime.load_settings('openonserver.sublime-settings')
settings.get('file_path_prefix')
settings.get('server_url')
class OpenonServerCommand(sublime_plugin.TextCommand):
def run(self,edit):
file_path = self.view.file_name()
What I need to do though take the value of the settings
file_path will be the path to the file I am running this on so lets say...
E:\Server\htdocs\mytest_project_\some\folder_\test.php
The settings
file_path_prefix will be E:\Server\htdocs\ and
server_url will be http://localhost/
I need to see if file_path_prefix exist in file_path if it does,
I need to replace the E:\Server\htdocs\ with the http://localhost/ and replace all \ to / and then store this new path in a variable
so...
E:\Server\htdocs\mytest_project_\some\folder_\test.php would become
http://localhost/mytest_project_/some/folder_/test.php
I then need to send this to the browser.
Any help is greatly appreciated
Use
os.system("path_to_browser url")
To run any external program. I also recomend to take a look at this comment
Ok after many hours (I hate Python now) my solution (i'm very not impressed) but it partially works
#Context.sublime-menu
[
{ "command": "openserver", "caption": "Open on Server" }
]
#Default (Windows).sublime-keymap
[
{ "keys": ["ctrl+shift+b"], "command": "openserver" }
]
#Main.sublime-menu
[
{
"caption": "Tools",
"mnemonic": "T",
"id": "tools",
"children":
[
{ "command": "openserver", "caption": "Open on Server" }
]
}
]
#Openserver.sublime-commands
[
{
"caption": "Open file on Server in Browser",
"command": "openserver"
}
]
#Openserver.sublime-settings
{
"file_path_prefix": "E:/Server/htdocs",
"url_prefix": "http://localhost"
}
Main file
#openserver.py
import sublime, sublime_plugin
import os
import webbrowser
import re
import os2emxpath
import logging
import sys
class OpenserverCommand(sublime_plugin.TextCommand):
def run(self,edit):
file_path = self.view.file_name()
settings = sublime.load_settings('Openserver.sublime-settings')
file = os2emxpath.normpath(file_path)
url = re.sub(settings.get('file_path_prefix'), settings.get('url_prefix'), file)
#logging.warning(url)
#webbrowser.open_new(url)
if sys.platform=='win32':
os.startfile(url)
elif sys.platform=='darwin':
subprocess.Popen(['open', url])
else:
try:
subprocess.Popen(['xdg-open', url])
except OSError:
logging.warning(url)
Now when I say it works but it partially doesn't, it does take the file name, replaces my path and server URL from a settings file and then does launch a browser with the proper URL
Except, in Sublimetext2 when you run this on a .py file or any file that you do not have set to be able to open in a web browser, then instead of opening the file in the web browser it will give the windows popup asking to set a default program to open the file, very annoying!

Categories