cx_Freeze doesn't print to console - python

I have an application that prints a few things to the console upon running. But as a standalone the executable doesn't print anything to the console?
The setup.py script looks like this:
import sys
from cx_Freeze import setup, Executable
setup(
name = "My App",
version = "1.0",
options = {
"build_exe" : {
"include_files": ['MyImgs']
},
},
executables = [Executable("Main.py", base = "Win32GUI")]
)
On the command line I run the following: py setup.py build
I then find the executable and run: Main.exe.
What I am missing for some reason is any print() statements. Is there something I need to include in the setup script for this to happen?

If you use the "Win32GUI" base, then Windows does not make available stdout and stderr. You will need to redirect those yourself to some other location (such as a file). If you use the "Console" base then stdout and stderr are available and print() will work as expected -- but you will see a console created for you if you haven't run it from a console in the first place!

Related

Application fails to start after building using cx_Freeze on OSX

I'm trying to build a simple app on OSX using cx_Freeze. The build using setup.py seems to go fine - it builds a .dmg and .app for my application, along with all of the source files. However, when I try to run the .app, it crashes immediately, saying "My_App quit unexpectedly". Frustratingly, I don't see any decipherable error codes I can track down.
I have stripped down the app into a very simple example. Below are my main python file and my setup file.
my_app.py
from tkinter import *
root = Tk()
root.title("Welcome to My_App")
root.geometry('350x200')
root.mainloop()
setup.py
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": [], "excludes": []}
# base="Win32GUI" should be used only for Windows GUI app
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "My_GUI",
version = "1.0",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("my_app.py", base=base)]
)
I have built an .exe on windows before, and when startup fails on windows, I at least get a traceback error message that I can track down. On OSX, it doesn't appear to do the same thing.
What's going wrong? How can I get more information on why my app is failing to start up? Below are images of my build and the error I'm getting on startup.
Ok turns out if I run the file from terminal, it outputs an actual error code. From there, I was able to track down that I did not have zlib installed. I was getting error
zipimport.ZipImportError: can't decompress data; zlib not available
What ultimately solved it for me was reading through this thread
https://github.com/jiansoung/issues-list/issues/13

msi created in cx_freeze does not install program

I have a python program I've created and turned into an exe with cx_freeze. When just creating the exe with "python setup.py build", everything works great. Now I am trying to create an installer for it by using "python setup.py bdist_msi" and everything seems to work great at first.
The installer pops up and asks me to select a directory. After clicking next, Windows requires me to accept that I'm installing from an unknown publisher. After accepting, the next page immediately jumps to "Completing the (program) installer", and "click finish to exit the installer". After clicking finish, I cannot find my program file anywhere, and the installer appeared to do nothing.
I've tried running this on multiple computers, and nothing seems to work. I think this is an issue with my setup.py script. This does include several qml files and some pictures in my "Resources" folder, and there are several csv's in my "Data-and-executable" folder, plus a exe that is called on by my python script.
from cx_Freeze import setup, Executable
# dependencies
build_exe_options = {
"packages": ['atexit', "os", "sys", "csv", "time", "functools", "PySide2.QtCore", "PySide2.QtWidgets", "PySide2.QtUiTools", "PySide2.QtQuick", "PySide2.QtQml", "PySide2.QtGui", "shiboken2"],
"include_files": ['README.md', r'Resources','inputClass.py', 'selections.py','simulationOutput.py',
'Data-and-executable'],
"include_msvcr": True,
"excludes": ["Tkinter", "Tkconstants", "tcl", ],
"build_exe": "build",
}
bdist_msi_options = {
'add_to_path': False,
'initial_target_dir': r'[ProgramFilesFolder]\%s\%s' % ("Name", "Product"),
}
executable = [
Executable("main.py",
base="Win32GUI",
targetName="product.exe",
icon= r"Resources\logo_icon.ico"
)
]
setup(name = "Product",
version = "0.3",
description = "Simulator",
options={"bdist_msi": bdist_msi_options,
"build_exe": build_exe_options},
executables=executable
)
There are no error messages or anything else to tip me off as to what's going on. Even calling my .msi with cmd does not provide any insight.
Smh. I had all of my files just in the "build" folder, and not the "build/exe.win-amd64-3.6" folder. It works.

cx_Freeze with python packages (not just one module)

All of the cx_Freeze examples are for one file (module). I need to make an executable for an entire python package. Why is that hard to make?
Here's my directory:
test1/
__init__
__main__
The way I run this from the command line is by using the following cmd
python -m test1
__init__ is empty and __main__ just have a simple print statement.
I am using python 3.5.1 but I can switch to python 3.4 if that would fix the problem
Here is my setup.py for win64
from cx_Freeze import setup, Executable
import sys
build_exe_options = {"packages": ['test1'],
"include_files": []
}
executables = [
Executable("__main__")
]
setup(
name = "Foo",
version = "0.1",
description = "Help please!",
author = "me",
options = {"build_exe": build_exe_options},
executables = executables
)
Update:
1- see the comment below for solution for this approach
2- switch to pyinstaller because it can produce one exe file not a folder
Freezing a whole package doesn' t make sense because to create an executable binary you will want a Python script that can be run standalone from the command line. A package normally isn't started out-of-the-box but would get imported by another module.
However you can always import a package in your script so when you freeze it the package gets included in your distribution.
So do something like this:
test1/
__init__
__main__
run_test.py
run_test.py now imports test1 and starts your function that does whatever you want.
import test1
run_the_whole_thing()
Note: You will need to change your Executable in the setup.py to run_test.py.

Python script has been built to exe, but how to make it not show the command prompt?

I have converted the python script into the exe version. But every time i executed it, it will show the command prompt. Is it possible to change the script to make it not show the command prompt?
Thanks in advance.
Change your .py file extension to .pyw
And then add Win32GUI to your script.
from cx_Freeze import setup, Executable
import sys
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = u"XXX",
version = "1.0",
description = u"XXX",
options = {'build_exe': {'include_files':includefiles}},
executables = [Executable("XXXX.pyw" ,base = base)])

How can I hide the console window when freezing wxPython applications with cxFreeze?

I'm developing a Python application using wxPython and freezing it using cxFreeze. All seems to be going fine apart from this following bit:
When I run the executable created by cxFreeze, a blank console window pops up. I don't want to show it. Is there any way I could hide it?
It doesn't seem to be documented on the cxFreeze site and Googling didn't turn up much apart from some similar sorta problems with Py2Exe.
Thanks.
For Windows:
You have to use a line like this (use file folders and names as appropriate)
C:/Python/Scripts/cxfreeze C:/Python/Code/yourprogram.py --base-name=Win32GUI --target-dir C:/Python/Dist
By adding the --base-name=Win32GUI option, the console window will not appear.
This worked to some extent but it has issues. My program runs in both a console mode and a GUI mode. When run from the console with a --console argument it runs in a console mode. When I followed the procedure below, this doesn't work anymore and my program is only a GUI app then.
The following source code comes from a sample file in the \Python\Lib\site-packages\cx_Freeze\samples\PyQt4\setup.py. Lesson of the day. Read the README.
# A simple setup script to create an executable using PyQt4. This also
# demonstrates the method for creating a Windows executable that does not have
# an associated console.
#
# PyQt4app.py is a very simple type of PyQt4 application
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
# subdirectory that contains the files needed to run the application
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "simple_PyQt4",
version = "0.1",
description = "Sample cx_Freeze PyQt4 script",
executables = [Executable("PyQt4app.py", base = base)])
If you're using Windows, you could rename your "main" script's extension (that launches the app) to .pyw
Option 1) Use gui2exe to muck with various options.
Option 2) Modify your setup.py with 'base' parameter as such.
GUI2Exe_Target_1 = Executable(
# what to build
script = "rf_spi.py",
initScript = None,
base = 'Win32GUI', # <-- add this
targetDir = r"dist",
targetName = "rf_spi.exe",
compress = True,
copyDependentFiles = False,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = r"wireless.ico"
)

Categories

Resources