Python: PYInstaller, CX_Freeze: Cannot create installer - python

I built a python app, and I'm using Ubuntu 16.04 and python 2.7.
I'm trying to compile the python script as .EXE to work on windows but I cannot find a way to make it start. I'm also having Selenium and Tkinter in my script.
Here's what my python file looks like:
import ast
import random
import thread
import time
import tkMessageBox
from Tkinter import *
import os
from selenium import webdriver
from selenium.webdriver import ActionChains
global geckodriver
if os.name == 'nt':
geckodriver = "./geckodriver.exe"
else:
geckodriver = "./geckodriver"
I'm also using a PNG file as tkinter icon and displayed picture in Tkinter:
photo = PhotoImage(file="smallblack.png")
root.tk.call('wm', 'iconphoto', root._w, photo)
w = Label(root, image=photo)
w.photo = photo
w.pack(side=TOP, pady=15)
and also using two text files for the script to read later:
def countlinks():
lines = 0
for line in open("checklinks.txt"):
lines += 1
return lines
but I thought this can be added later next to the generated .exe.
Now, here is my setup.py used by cx_freeze:
#setup.py
from cx_Freeze import setup, Executable
setup(
name = "MainExe",
version = "1.0.0",
options = {"build_exe": {
'packages': ["ast","random","thread","time", "tkMessageBox", "Tkinter", "os", "selenium"],
'include_files': ['smallblack.png','geckodriver','geckodriver.exe'],
'include_msvcr': True,
}},
executables = [Executable("main.py)]
)
What am I doing wrong? When building the .exe with pyinstaller (pyinstaller main.py --onefile), the .exe builds but it doesn't open in Windows (opens a console window for 1 second and closes it), and if I'm trying to run it in CMD it says the program is too big.
When trying to run cx_freeze (python setup.py build) it creates a .exe that will only open a console and nothing else happens.
I've been googling this for the last 5 hours with no success.
What am I doing wrong?

In the example of CX_Freeze, this is happening most likely because your build is missing one of the packages it requires. The reason it is opening the console window is because you didn't set the base to "Win32GUI". Once you do this, you'll get a dialog box which shows the error message and you can include that package in your "include" list.
Try:
#setup.py
from cx_Freeze import setup, Executable
setup(
name = "MainExe",
version = "1.0.0",
options = {"build_exe": {
'packages': ["ast","random","thread","time", "tkMessageBox", "Tkinter","os", "selenium"],
'include_files': ['smallblack.png','geckodriver','geckodriver.exe'],
'include_msvcr': True,
}},
executables = [Executable("main.py", base="Win32GUI")]
)

Related

Mac cxFreeze app quits unexpectedly when trying to open

I am trying to convert a python script to a mac app so I can distribute it. I'm using cxFreeze to do this. After creating the app, I try to open it but it says the app quit unexpectedly and shows some report.
(code signature invalid (errno=1)
usr/local/lib/Python (no such file)
---
my script at.py:
import tkinter as tk
from tkinter import font
window = tk.Tk()
width=1
window.title('test')
window.geometry("425x500")
label_speed = tk.Label(
text="Speed"
)
label_speed.grid(row=1, column=1, columnspan = 5, stick="w")
window.mainloop()
And then my setup.py
from cx_Freeze import Executable, setup
base = None
if sys.platform == "win32":
base = "Win32GUI"
executables = [Executable("at.py", base=base)]
setup(
name="test",
version="0.1",
description="just for testing",
executables=executables,
)
I used the following commands to make the mac bundle or app.
python3 setup.py build then
python3 setup.py bdist_dmg
I had to use python3 instead of python because it wasn't working for me.
Thanks in advance for any tips and answers
There might be two different things going on. The first thing I know I have run into with cx_freeze is that it tried to map to where it thinks the python 2.x folder should be even if I specify to run on python 3.x. The other thing might be it was downloaded on to a different path. type $ where python to see where the file path should be. If you do $ open $FILEPATH and you see that its using python3 it might be worth reaching out to the maintainer of cx_freeze and see if they have any advice.

Python Error using Tkinter tix when create exe or msi with cx_Freeze

I'm trying to make with Python an EXE/MSI of my script, but in my script I use the library tkinter.tix i use that to create a Balloon tooltip. If i execute the script with the IDLE runs very well, but if i try to make an EXE(auto-py-to-exe) or MSI(cx_Freeze), shows me an error.
I import the module like this:
from tkinter.tix import *
I attach the error in picture.
I appreciate you can help me!!! Thanks...
You need to copy the tix folder in the Python installed folder into the distributed folder as well.
Below is a sample setup.py when cx_Freeze is used:
from cx_Freeze import setup, Executable
# change to the correct path for the tix folder in your system
include_files = [(r'C:\Python38\tcl\tix8.4.3', r'lib\tkinter\tix8.4.3')]
build_exe_options = {
'include_files': include_files,
}
bdist_msi_options = {}
setup(
name='Demo',
version='0.1',
options = {
'build_exe': build_exe_options,
'bdist_msi': bdist_msi_options,
},
executables=[Executable('tix-demo.py', base='Win32GUI')],
)
Then build the MSI by executing the following command:
python3 setup.py bdist_msi

Cx_freeze is not working on Win 10 with Python 3.6.2

I am starting to learn a bit about Python. I am trying to convert Python Tkinter app into exe file. Converting to exe works fine when Tkinter is not involved. I tryed sample file of setup.py and Tkinter app that you can find on official website of cx_Freeze [http://cx-freeze.readthedocs.io/en/latest/index.html] but still geting a lot of errors [like: KeyError: 'TCL_LIBRARY'] in CMD when runing build command. On official website is stated that Python 3.6 is supported.
Here is official example of setup.py:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [
Executable('app.py', base=base)
]
setup(name='simple_Tkinter',
version='0.1',
description='Sample cx_Freeze Tkinter script',
executables=executables
)
And here is official example of test Tkinter app:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
from tkinter import Tk, Label, Button, BOTTOM
except ImportError:
from Tkinter import Tk, Label, Button, BOTTOM
root = Tk()
root.title('Button')
Label(text='I am a button').pack(pady=15)
Button(text='Button').pack(side=BOTTOM)
root.mainloop()
Instead of cx_freeze you could try and use Pyinstaller it will do the exact same job you are trying to accomplish.
From pip go ahead and type
pip install pyinstaller and then in your programs directory run pyinstaller yourprogram.py

Python crashes when trying to compile using cx_Freeze

I'm trying to compile my python scripts using cx_Freeze, here is my setup file:
import cx_Freeze
import sys
import matplotlib
import os
base = None
if sys.platform == 'win32':
base = "Win32GUI"
os.environ['TCL_LIBRARY'] = r'C:\\Python35\\tcl\\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\\Python35\\tcl\\tk8.6'
executables = [cx_Freeze.Executable("HomeScreen.py", base=base,
icon="icon.png")]
cx_Freeze.setup(
name = "LeagueBoost",
options = {"build_exe":{"packages": ["sqlite3","requests","time","sys","os","statistics","matplotlib","random","collections"],
"include_files": ["Assets", "LeagueBoost_v1.py","LBRun.py","graphSetup.py","profilepage.py","Assets_rc.py"]}},
version = "1",
executables = executables
)
But when I give the cmd command C:/python35/python.exe, it gets to copying C:\python35\python35.dll -> build\exe.win-amd64-3.5\python35.dll it pops up "python has stopped working"
This is crazy
after hitting my head against the wall for the weird reason python crashes when I tried to build executable with cx_Freeze,
what solved my problem is using ico format for icon file.
Your icon file should be icon type not png, may be because png is not supported by cx_Freeze.
In your setup.py change
icon="icon.png" to icon="icon.ico",
please note the icon file must be in ico format, don't act smart and just change the extension.
If it still doesn't work you can give it a trial without writing this option at all icon="icon.png" and see if it works.

Py2exe compiles properly but the built application doesn't work

I am using Python 2.7 to build my application. Within it, I used few packages which are numpy, scipy, csv, sys, xlwt, time, wxpython and operator.
All the above packages are in 64-bit, and I am using python 2.7(64-bit version) in Aptana Studio 3(64-bit version) in Windows 7 Professional (64-bit version).
At last, I'd like to compile my project to an application using following code, the file name is py2exeTest.py:
from distutils.core import setup
import numpy # numpy is imported to deal with missing .dll file
import py2exe
setup(console=["Graphical_Interface.py"])
Then in cmd, I switched to the directory of the project and used following line to compile it:
python py2exeTest.py py2exe
Everything goes well, it generates an application under dist directory, and the application name is Graphical_Interface.exe.
I double clicked it, but there is a cmd window appears, and a python output windows flashes, then both of them disappeared. I tried to run the application as an administrator, the same outcome I've had.
May I know how to work this out?
Thanks!
EDIT:
I've managed to catch the error information that flashes on the screen. The error info I had is:
Traceback (most recent call last):
File "Graphical_Interface.py", line 397, in <module>
File "Graphical_Interface.py", line 136, in __init__
File "wx\_core.pyc", line 3369, in ConvertToBitmap
wx._core.PyAssertionError: C++ assertion "image.Ok()" failed at ..\..\src\msw\bitmap.cpp(802) in wxBitmap::CreateFromImage(): invalid image
I used one PNG image in the project, the code is like follows:
self.workflow = wx.Image("Work Flow.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self.panel_settings, -1, self.workflow, (330,270), (self.workflow.GetWidth(), self.workflow.GetHeight()))
I tried to comment the above chunk out from the project, and the application works properly. However, I need the image to show up in the application.
May I know how to deal with it?
Thanks.
Hiding console window of Python GUI app with py2exe
When compiling graphical applications you can not create them as a console application because reasons (honestly can't explain the specifics out of my head), but try this:
from distutils.core import setup
import numpy
import py2exe
import wxpython
setup(window=['Graphical_Interface.py'],
options={"py2exe" { 'boundle_files' : 1}})
Also consider changing to:
http://cx-freeze.sourceforge.net/
It works with Python3 and supports multiple platforms.
A cx_freeze script would look something like:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
includefiles = ['/folder/image.png']
setup( name = "GUIprog",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options, 'include_files' : includefiles},
executables = [Executable("Graphical_Interface.py", base=base)])
No worries, I've got the solution.
It turns out that the image is in the project folder rather than in the dist folder. So I have two solutions:
Copy the image into dist folder
Include the full path of the image in the code.
Thanks for your help.

Categories