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!
Related
I want to make a .exe file from my python project, I have made a GUI in tkinter. This project has multiple files and uses a variety of libraries.
I tried to use auto-py-to-exe but it gave a variety of errors concerning the use of tkinter, saying it can not find tkinter. I do not understand this error since tkinter is automatically installed with python? Are there better ways to use auto-py-to-exe or better programs to convert a hole project to .exe? I also tried pyinstaller, but when opening the program it immediately closes again. The program does run properly in pycharm.
The error is I\output\main_init_.py", line 1, in <module> import tkinter ModuleNotFoundError: No module named 'tkinter'
I personally use CX-Freeze to compile my executables. I have probably used it over 100 or so updates of my tools and typically the problem I run into is either related to missing file that need to be identified in the setup.py file or the fact that when it compiles the Tkinter folder it uses a capital T instead of a lower case t so after I compiling an app I have to manually update the folder to be lowercase T.
Here is an example of the setup file.
As you can see below when compiling tkinter you need to ID the TK and TCL library folders in order for it to compile the listed DLL files properly.
from cx_Freeze import setup, Executable
import os
base = "Win32GUI"
os.environ['TCL_LIBRARY'] = r'C:\Users\user\Desktop\Python381\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\user\Desktop\Python381\tcl\tk8.6'
build_exe_options = {'packages': ['os',
'json',
'http',
'email',
'pyodbc',
'openpyxl',
'calendar',
'threading',
'datetime',
'tkinter',
'tkinter.ttk',
'tkinter.messagebox'],
'excludes': ['PyQt5',
'PIL',
'numpy',
'pandas'], # 'urllib', # 'encodings', # 'numpy'
'include_files': [r'excel_temp.xlsx',
r'opt_3_excel_temp.xlsx',
r'tcoms_excel_temp.xlsx',
r'main_config.json',
r"C:\Users\user\Desktop\Python381\DLLs\tcl86t.dll",
r"C:\Users\user\Desktop\Python381\DLLs\tk86t.dll"]}
setup(
name='<GIT>',
options={'build_exe': build_exe_options},
version='0.57',
description='<GIT - Global Inventory Tool!>',
executables=[Executable(r'C:\Users\user\PycharmProjects\Py381_GIT\MAIN.py', base=base)]
)
After you run the compiler you will often get an error that looks like this.
The error NoduleNotFoundError: No module named 'tkinter' is due to the odd behavior of the compiler giving the tkinter folder a Capital T like the below image in the lib folder.
In this case you would update the library to be a lowercase t.
Use this package :
python -m pip install auto-py-to-exe
Run this command :
auto-py-to-exe
I am trying to generate aa .exe using py2exe for a python script that generates an excel. Here is just a sample code. I am writing value 100 to a cell and saving the excel to Users Desktop using openpyxl. This works perfectly fine when I run it directly.
import openpyxl
import getpass
wb = openpyxl.Workbook()
ws = wb.create_sheet('test')
ws.cell(row=1, column=1, value=100)
username = getpass.getuser()
wb.save('C:\\Users\\{}\\create_exe\\gen.xlsx'.format(username))
print 'Done'
And when I compile it using py2exe it compiles also just fine.
The problem arises when I run the generated .exe file. I get a return saying
ImportError: No module named jdcal
setup.py file is as follows
import py2exe
from distutils.core import setup
packages = ["openpyxl", "openpyxl.workbook", "xml.etree", "xml"]
excludes = []
setup(console=['test_program.py'],
options={"py2exe": {"excludes": excludes,
"packages": packages}}
)
Thisngs I have already tried
I have searched and few people said Install openpyxl using pip. I
have done that and pip says its alöready installed.
I have also tried to install jdcal using pip and pip says it is installed.
I have uninstalled jdcal and installed it using pip and manually, and
still the same error.
I have included jdcal in the packages and still no change in the outcome.
I hope someone can help me with it.
Thanks in advance
EDIT :
Generated Filed in dist folder are as follows (openpyxl cannot be seen here, I don't know why)
tcl (Folder)
_ctypes.pyd
_elementtree.pyd
_hashlib.pyd
_multiprocessing.pyd
_socket.pyd
_ssl.pyd
_tkinter.pyd
bz2.pyd
pyexpat.pyd
select.pyd
unicodedata.pyd
win32ui.pyd
numpy.core._dummy.pyd
numpy.core.multiarray.pyd
numpy.core.multiarray_tests.pyd
numpy.core.operand_flag_tests.pyd
numpy.core.struct_ufunc_test.pyd
numpy.core.test_rational.pyd
numpy.core.umath.pyd
numpy.core.umath_tests.pyd
numpy.fft.fftpack_lite.pyd
numpy.linalg._umath_linalg.pyd
numpy.linalg.lapack_lite.pyd
numpy.random.mtrand.pyd
_win32sysloader.pyd
win32api.pyd
win32pdh.pyd
win32pipe.pyd
tk85.dll
tcl85.dll
libiomp5md.dll
pywintypes27.dll
python27.dll
w9xpopen.exe
pythoncom27.dll
library.zip
test_program.exe (Executable File)
Try to manually include it in setup.py in packages = ["openpyxl", "openpyxl.workbook", "xml.etree", "xml"]
so it would be:
packages = ["openpyxl", "openpyxl.workbook", "xml.etree", "xml", "jdcal"]
I personally have had good luck letting py2exe detect the modules that are needed. I've never tried to specify every module necessary.
try this:
from distutils.core import setup
import py2exe
setup(console=['test_program.py'])
this should be run from command line as
python setup.py py2exe
py2exe outputs .dll files in the dist directory, these have to be in the directory that you run your .exe file from. if you just want one .exe file and no .dll files try this:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
console = [{'script': "test_program.py"}],
zipfile = None,
)
this should be run from command line as
python setup.py
I use cx_freeze and never had any issues.
Here's the setup.py for a cx_freeze file
from cx_Freeze import setup, Executable
build_exe_options = {"excludes": ["html5lib"],"optimize":2}
setup(name = "App Name" ,
version = "1.0.0.0" ,
options = {"build_exe": build_exe_options},
description = "" ,
executables = [Executable("FooBar.py")])
I'm trying to build a program in Python 3.6, but I've been struggling to get a functional .exe out. The packing script looks like it completes, but the resulting program instantly crashes. I've had similar issues with py2exe and pyinstaller even when using earlier versions of python 3, but I was able to get the following error out of cx_freeze 5.0.1's executable:
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000019a0 (most recent call first):
Searching around this has been mentioned before in relation to unix systems, but I'm doing this on Windows 10?
Here's the packaging script:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = "C:\\Program Files (x86)\\Python36-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Program Files (x86)\\Python36-32\\tcl\\tk8.6"
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
import sys
base = None
#'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('WorkingScript.py', base=base, targetName = 'WorkingScript.exe', icon='IconSml.ico')]]
includefiles=["Logo3.png", "Image4.png", "IconSml.ico"]
setup(name='WorkingScript',
version = '0.9',
description = 'This time around',
options = dict(build_exe = buildOptions),
executables = executables)
The only modules my program imports are tkinter, PIL, os, threading, csv and xml.dom.
I'm on Windows 7, as are all potential users of my program. I packaged a Python program I wrote into an executable file using cx_Freeze, with the following command:
python setup.py build
This generates a build directory that contains my_program.exe. The executable works flawlessly on my computer, but on a coworker's machine, it throws an exception:
ImportError: No module named 'zipfile'
Here's my setup.py, where zipfile is explicitly included (and it's definitely in library.zip):
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name='Z-Wizard',
version='0.1',
description='Z1/Z2 data extraction tool',
author='Liz Rosa',
author_email='me#url',
options = {
'build_exe': {
'packages': ['zipfile']
}
},
executables = [Executable('my_program.py', base=base)]
)
The traceback is quite long; there's a screenshot at the URL below. It apparently involves a series of functions in _bootstrap.py. I'm not quite sure what's going on here. Also, "C:\Users\lizr..." is my home directory, not hers. Why does it appear in the traceback on her computer? In case it's not obvious, I don't know much about the freezing process.
http://i.imgur.com/cAQKWxq.png
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.