CX_Freeze import error on Windows and ZMQ - python

I've a python program that uses ZMQ. I want to Freeze it so everyone can use it as executable. This is my setup.py
import sys
from cx_Freeze import setup, Executable
includes = ["sip", "re", "zmq", "PyQt4.QtCore", "atexit", "zmq.utils.strtypes", "zmq.utils.jsonapi", "encodings.hex_codec"]
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup (
name = "prueba",
version = "0.1",
description = "Esto es una prueba",
options = {"build_exe" : {"includes" : includes }},
executables = [Executable("Cliente.py", base = base)])
When I run this on Linux it works perfect and my program runs OK, but when I do so on Windows I get the Following Error when I execute the .exe file:
from zmq.core import (constants, error, message, context,
File "ExtensionLoader_zmq_core_error.py", line 12, in <module>
ImportError: DLL load failed: The specified module cannot be found
Also, when CX_Freeze is working I can notice the following lines:
Missing modules:
? zmq.core.Context imported from zmq.devices.basedevice
? zmq.core.FORWARDER imported from zmq.devices.monitoredqueuedevice
? zmq.core.QUEUE imported from zmq.devices.monitoredquedevice
? zmq.core.ZMQError imported from zmq.devices.monitoredquedevice
I've been trying to figure out this problem for an hour or two, it seems it may be related with a DLL it should be importing and it isn't. Some DLL that ZMQ needs to work, but I cannot find which one is it.

Fixed by adding:
['zmq','zmq.utils.garbage','zmq.backend.cython']
To the packages, then renaming zmq.libzmq.pyd to libzmq.pyd

You may have to explicitly add one or more modules to your includes. If it IS a DLL issue though, I usually use Dependency Walker to figure it out. You can get it free here: http://www.dependencywalker.com/
It occasionally gave me a false positive, but overall it's almost always helped.

Related

Python cx_Freeze import _tkinter and ImportError: DLL load failed %1 is not a valid win32 application ERROR

cx_Freeze import _tkinter #if this fails your python may not be configurated for Tk.. ImportError: DLL load failed: %1 is not a valid win32 application
Hi! I created a word guessing/memorizing language practice program in tkinter, and i wanted to convert it to an executable app but i get errors again and again... I tried at least 10-15 different ways but it is still same. So i googled all the similar topics and tried every single suggestion but they did not work for me. I tried pyinstaller as well. It does not give an error but also it does not run the executed app either. When i click on the app.exe, black CMD screen appears then disappears and it creates the csv file to save the words. So i changed to cx_Freeze but it does not work either.
What have i tried?
For pyinstaller:
I converted my app.py to app.exe by using pyinstaller -F app.py and pyinstaller app.py, but none of them worked.
For cx_Freeze
1- I tried my old setup.py (which worked to convert my all games to .exe) but it did not work on this, i guess it is about tkinter. Because my games were written with pygame, i did not use tkinter for anything.
2 -I tried at least 10 different setup files shared by you guys on stackoverflow and other websites. But they did not work either.
3- I tried to set environment
4- I tried to specify environment
5- I copied tcl86t.dll and tk86t.dll files from the python dlls to my app directory and added them as included files in the setup.py
6- I deleted 64bit version and i only have 32bit version of python3.7
and more and more...
So this is my current setup.py
import sys,os
from cx_Freeze import setup, Executable
os.environ['TCL_LIBRARY'] = "C:\Python37\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\Python37\tcl\tk8.6"
build_exe_options = {"packages": ["os"],"includes":["tkinter"],"include_files": []}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "App",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("app.py", base=base)]
)
I use Windows7 64bit and Python 3.7.1 32bit and 3.7.0 64bit
So what should i do? I am stuck here. I really gave up and asking a new question on here. Please help me guys.. Thanks in advance.

py2exe "No module named 'site'" from Anaconda

I'm trying to use py2exe to build an executable on 64-bit Windows 7 with Anaconda (Python 3.4) for a project of mine that depends on a lot of libraries. Some of the more complex ones include vispy (pyopengl), PyQt4, numba, and scipy. I've been stepping through various errors to try to get a working executable, but have hit a road block with no clear way forward. Currently, the py2exe command completes, but I get the following error when running the exe:
...
from numba import jit
File "C:\Anaconda3\envs\sift_py2exe\lib\site-packages\numba\__init__.py", line
13, in <module>
from .pycc.decorators import export, exportmany
File "C:\Anaconda3\envs\sift_py2exe\lib\site-packages\numba\pycc\__init__.py",
line 12, in <module>
from .cc import CC
File "C:\Anaconda3\envs\sift_py2exe\lib\site-packages\numba\pycc\cc.py", line
4, in <module>
from distutils.command import build_ext
File "C:\Anaconda3\envs\sift_py2exe\lib\distutils\command\build_ext.py", line
17, in <module>
from site import USER_BASE
ImportError: No module named 'site'
I was able to do a small workaround by adding the C:\Anaconda3\envs\sift_py2exe\Lib directory to sys.path in my main script, but I doubt that's going to help me much later. Not to mention I had more scipy DLL issues after that.
Here are the relevant parts of my setup.py:
try:
import py2exe
from llvmlite.binding.ffi import _lib_dir, _lib_name
kwargs["data_files"] = [('.', [os.path.join(_lib_dir, _lib_name), os.path.join(_lib_dir, "MSVCP120.dll"), os.path.join(_lib_dir, "MSVCR120.dll")])]
kwargs["console"] = [{
'script': 'cspov/__main__.py',
'dest_base': "SIFT",
}]
kwargs["options"] = {'py2exe': {"includes": ["vispy.app.backends._pyqt4", "PyQt4.QtNetwork"]}}
except ImportError:
print("'py2exe' and/or 'llvmlite' not available")
I've tried adding the "Lib" directory in setup.py and then including "site", but it doesn't find the module. Any ideas? Thanks.
Side note: I'm using the Microsoft DLLs from llvmlite as a quick workaround because I couldn't get it to work in any of the normal ways.
This isn't the answer I was hoping for, but I was able to get a working executable when I switched to pyinstaller. All of the other SO questions I saw relevant to my problem had similar "solutions".

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.

Cannot find module "cx_freeze__init__" when freezing Python script

So I am trying to freeze a Python script as an .exe. I installed cx_freeze to do so as I am using 3.4. I created the setup.py file successfully and also ran the build which appears in the build folder and .exe appears. However, when I open the .exe it displays the following error:
zipimport.ZipImportError: can't find module 'cx_Freeze__init__'
Fatal python error: unable to locate initialization module
Current thread 0x00001d58 <most recent call first>:
I believe I did everything else correctly. There is indeed a cx_Freeze_init_.pyc file in the library.zip, however it cannot find it for some reason. The only other error I had was that certain modules were missing when during the cmd build:
Missing modules:
? _dummy_threading imported from dummy_threading
? ce imported from os
? doctest imported from heapq
? getopt imported from base64, quopri
? org.python.core imported from copy
? os.path imported from os
? posix imported from os
? pwd imported from posixpath
? subprocess imported from os
This is not necessarily a problem - the modules may not be needed on this platfo
rm.
This is the setup.py I used:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
base = 'Console'
executables = [
Executable('guess.py', base=base)
]
setup(name='Guess',
version = '1.0',
description = 'test',
options = dict(build_exe = buildOptions),
executables = executables)
Thanks in advance!

Making exe using py2exe + sqlalchemy + mssql

I have a problem with making exe using py2exe. In my project i'm using sqlalchemy with mssql module.
My setup.py script looks like:
from distutils.core import setup
import py2exe
setup(
windows=[{"script" : "pyrmsutil.py"}],
options={"pyrmsutil" : {
"includes": ["sqlalchemy.dialects.mssql", "sqlalchemy"],
"packages": ["sqlalchemy.databases.mssql", "sqlalchemy.cresultproxy"]
}})
But when i'm starting procedure like:
python.exe setup.py py2exe
I'm receiving build log with following errors:
The following modules appear to be missing
['_scproxy', 'pkg_resources', 'sqlalchemy.cprocessors', 'sqlalchemy.cresultproxy']
And in "dist" folder i see my pyrmsutil.exe file, but when i'm running it nothing happens. I mean that executable file starts, but do nothing and ends immediately without any pyrmsutil.exe.log. It's very strange.
Can anybody help me with this error?
I know it's no an answer per se but have you tries pyInstaller? I used to use py2exe and found it tricky to get something truly distributable. pyInstaller requires a little more setup but the docs are good and the result seems better.
For solving this issue you could try searching for the mentioned dlls and placing them in the folder with the exe, or where you build it.
Looks like py2exe can't find sqlalchemy c extensions.
Why not just include the egg in the distribution, put sqlachemy in py2exe's excludes and load the egg on start?
I use this in the start script:
import sys
import path
import pkg_resources
APP_HOME = path.path(sys.executable).parent
SUPPORT = APP_HOME / 'support'
eggs = [egg for egg in SUPPORT.files('*.egg')]
reqs, errs = pkg_resources.working_set.find_plugins(
pkg_resources.Environment(eggs)
)
map(pkg_resources.working_set.add, reqs)
sys.path.extend(SUPPORT.files('*.egg'))
i use Jason Orendorff's path module (http://pypi.python.org/pypi/path.py) but you can easily wipe it out if you want.

Categories