python + cx_freeze 'cannot get zipimporter instance' - python

I saw one question relating to this, however it did not really answer my question.
I have written a program intended to be able to be used without needing an installation of python; I used cx_freeze to achieve this. I wrote a setup.py, and ran it in cmd. This all works fine, although I do get some 'missing module' warnings, it assures me that this may not be a problem:
Missing modules:
? _dummy_threading imported from dummy_threading
? ce imported from os
? doctest imported from heapq
? getopt imported from base64, quopri
? grp imported from shutil, tarfile
? org.python.core imported from copy
? os.path imported from os, py_compile, shutil
? posix imported from os
? pwd imported from posixpath, shutil, tarfile
? subprocess imported from os
This is not necessarily a problem - the modules may not be needed on this platform.
(I am running Windows 8, never got round to updating to 8.1)
After exporting, I am presented with two files; 'python34.dll' and
'T-Backup.exe'. When I run T-backup.exe I get the above error.
Here's my setup.py in case it's neeeded:
import sys
from cx_Freeze import setup, Executable
base = None
if (sys.platform == "win32"):
base = "Win32GUI"
exe = Executable(
script = "<path>\\T-Backup.py",
icon = "<path>\\Icon.ico",
targetName = "<path>\\exe\\T-Backup.exe",
base = base
)
includefiles = ["<path>\\Icon.ico","<path>\\backupfrom.tbk","<path>\\backupto.tbk"]
setup(
name = "T-Backup",
version = "0.1",
description = "Backs up Terraria Worlds and Players.",
author = "Sam Poirier (darthmorf)",
options = {'build_exe': {'include_files':includefiles}},
executables = [exe]
)
Thanks for your help.
-darthmorf

I cannot comment, so I am answering:
I think you are on a 64bit system and you will never get a base other than 'None'. Try changing
base = None
if (sys.platform == "win32"):
base = "Win32GUI"
To
base = "Win32GUI"
This fixed a lot of my issues, that were similar to what you described...
If you could show what modules you actually imported then I could probably be of more use.

Related

cx_Freeze missing module

I'm using python 3.7 and cx_Freeze 5.1.1 , I'm, trying to convert my python script into an executable but I am getting thrown a missing module error and I am stumped.
I have tried putting the modules in the package and includes of the setup script but nothing is changing.
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
# build_exe_options = {"packages": ["os", "win32api", "win32con", "pywintypes", "easyguy", "ntsecuritycon"
# , "win32security", "errno", "shutil", "ctypes"], "excludes": ["tkinter"],
# "includes" = ['easy_gui']}
build_exe_options = {'packages': ['sys', "os", "win32api", "win32con",
"pywintypes", "easygui", "ntsecuritycon",
"errno", "shutil", "ctypes", "win32security",
"errno", "shutil", "ctypes"],
'excludes': ['tkinter'],
'includes': ["os", "win32api", "win32con", "pywintypes",
"easygui", "ntsecuritycon",
"errno", "shutil", "ctypes", "win32security",
"errno", "shutil", "ctypes"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name="Automated Installer", # this will set the name of the created executable to "Automated Installer.exe"
version="0.1",
description="My GUI application!",
options={"build_exe": build_exe_options},
executables=[Executable("Automated Installer.py", base=base)]) # this tells cx_Freeze to freeze the script "Automated Installer.py"
I expect a executable to be created, instead I am thrown this error\
ImportError: No module named 'win32api'
EDIT 2: Reflecting Steps taken by the answer posted below.
I upgraded back to Python 3.7 and I applied the fix to freezer.py as recommended. I took the exact same easygui script written and the same setup.py script also written below. The executable builds, but does not run. I am thrown an error shown below. I am able to run the example easygui script just fine, so that leads me to believe that easygui is installed correctly.
I'm not too sure what you mean by the full stack trace but here is some notable output from the command prompt that I received
Missing modules:
? __main__ imported from bdb, pdb
? _frozen_importlib imported from importlib, importlib.abc
? _frozen_importlib_external imported from importlib, importlib._bootstrap,
importlib.abc
? _posixsubprocess imported from subprocess
? _winreg imported from platform
? easygui imported from hello world__main__
? grp imported from shutil, tarfile
? java.lang imported from platform
? org.python.core imported from copy, pickle
? os.path imported from os, pkgutil, py_compile, tracemalloc, unittest,
unittest.util
? posix imported from os
? pwd imported from http.server, posixpath, shutil, tarfile, webbrowser
? termios imported from tty
? vms_lib imported from platform
This is not necessarily a problem - the modules may not be needed on this
platform.
running build
running build_exe
copying C:\Users\Billy\AppData\Local\Programs\Python\Python37\lib\site-
packages\cx_Freeze\bases\Win32GUI.exe -> build\exe.win-amd64-3.7\hello
world.exe
copying
C:\Users\Billy\AppData\Local\Programs\Python\Python37\python37.dll ->
build\exe.win-amd64-3.7\python37.dll
copying
C:\Users\Billy\AppData\Local\Programs\Python\Python37\VCRUNTIME140.dll ->
build\exe.win-amd64-3.7\VCRUNTIME140.dll
copying C:\Program Files\TortoiseGit\bin\api-ms-win-crt-runtime-l1-1-0.dll -
>
build\exe.win-amd64-3.7\api-ms-win-crt-runtime-l1-1-0.dll
copying C:\Program Files\TortoiseGit\bin\api-ms-win-crt-stdio-l1-1-0.dll ->
build\exe.win-amd64-3.7\api-ms-win-crt-stdio-l1-1-0.dll
copying C:\Program Files\TortoiseGit\bin\api-ms-win-crt-math-l1-1-0.dll ->
build\exe.win-amd64-3.7\api-ms-win-crt-math-l1-1-0.dll
copying C:\Program Files\TortoiseGit\bin\api-ms-win-crt-locale-l1-1-0.dll ->
build\exe.win-amd64-3.7\api-ms-win-crt-locale-l1-1-0.dll
copying C:\Program Files\TortoiseGit\bin\api-ms-win-crt-heap-l1-1-0.dll ->
build\exe.win-amd64-3.7\api-ms-win-crt-heap-l1-1-0.dll
*** WARNING *** unable to create version resource
install pywin32 extensions first
writing zip file build\exe.win-amd64-3.7\lib\library.zip
cx_Freeze does not yet support Python 3.7, it has a bug. A bugfix exists but has not yet been released, however you can apply it manually, see What could be the reason for fatal python error:initfsencoding:unable to load the file system codec? and Cx_freeze crashing Python3.7.0. Or you can rollback to Python 3.6 if this is an option for you.
EDIT:
Check that easygui is correctly installed. You should for example be able to run the following hello.py example script from the easygui documentation:
from easygui import *
import sys
# A nice welcome message
ret_val = msgbox("Hello, World!")
if ret_val is None: # User closed msgbox
sys.exit(0)
msg = "What is your favorite flavor?\nOr Press <cancel> to exit."
title = "Ice Cream Survey"
choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"]
while 1:
choice = choicebox(msg, title, choices)
if choice is None:
sys.exit(0)
msgbox("You chose: {}".format(choice), "Survey Result")
Try to freeze this example script. easygui depends on tkinter, which requires some additional tuning to be frozen with cx_Freeze 5.1.1, see tkinter program compiles with cx_Freeze but program will not launch. You should be able to freeze the example using the following setup script:
from cx_Freeze import setup, Executable
import os
import sys
PYTHON_INSTALL_DIR = os.path.dirname(sys.executable)
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
build_exe_options = {'include_files': [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join('lib', 'tk86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
os.path.join('lib', 'tcl86t.dll'))]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
setup(name='hello',
version='0.1',
description='Sample cx_Freeze EasyGUI script',
executables=[Executable('hello.py', base=base)],
options={'build_exe': build_exe_options})

When using cx_Freeze and tkinter I get: "DLL load failed: The specified module could not be found." (Python 3.5.3)

When using cx_Freeze and Tkinter, I am given the message:
File "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.
Some things to note:
I want to use Python 3+ (Currently using 3.5.3, 32-bit). Don't really care about a specific version, whatever works.
My project has multiple files I need to compile. As far as I can tell, that leaves me with cx_Freeze or Nuitka. Nuitka had problems of its own.
I am using Windows 10 Home Edition, 64-bit
Here is my current setup.py:
from cx_Freeze import setup, Executable
import sys
build_exe_options = {"packages": ["files", "tools"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name="Name",
version="1.0",
description="Description",
options={"build_exe": build_exe_options},
executables=[Executable("main.py", base=base)],
package_dir={'': ''},
)
I have tried many solutions from all corners of the internet. Including but not limited to:
Multiple versions of python (and the corresponding cx_Freeze/Tkinter versions)
Both 32-bit and 64-bit versions
Replacing Tkinter with easygui (apparently easygui needs Tkinter to work)
Checking the PATH variables
Restarting my computer (Don't know what I expected)
Uninstalling other versions of python and repairing the correct version
Placing the following in my compile bat file (Definetly the correct paths):
set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\tcl\tcl8.6
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\tcl\tk8.6
Placing the following in my setup.py:
options={"build_exe": {"includes": ["tkinter"]}}
Along with:
include_files = [r"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\DLLs\tcl86t.dll",\
r"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\DLLs\tk86t.dll"]
(And yes, those were included in setup() in one way or another)
Thanks for any help, it's greatly appreciated. And yes, I have looked at just about every solution to this problem on this site. Hoping someone could help me find yet another solution since my problem seems to be persistent.
Found a solution!
I had to copy the tk86t.dll and tcl86t.dll files from my python directory's DLLs folder into the build folder with the main.py I was trying to compile.
This, in conjunction with having
set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35\tcl\tcl8.6
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35\tcl\tk8.6
at the top of my compile.bat, and including
"include_files": ["tcl86t.dll", "tk86t.dll"]
in my build_exe_options in setup.py, seems to have done the trick.
Here is my current setup.py:
from cx_Freeze import setup, Executable
import sys
build_exe_options = {"packages": ["files", "tools"], "include_files": ["tcl86t.dll", "tk86t.dll"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name="Name",
version="1.0",
description="Description",
options={"build_exe": build_exe_options},
executables=[Executable("main.py", base=base)],
package_dir={'': ''},
)
And here is my compile.bat (updated to show all steps):
#echo off
set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6
RD /S /Q "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin"
mkdir "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin"
xcopy /s "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\DLLs\tcl86t.dll" "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin\tcl86t.dll"
xcopy /s "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\DLLs\tk86t.dll" "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin\tk86t.dll"
cd "C:\Users\VergilTheHuragok\Desktop\PythonProject\"
cxfreeze main.py --target-dir "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin" --target-name "launch.exe"
pause
I found this solution here.
to solve this problem just copy the files
1.tcl86t.dll
2.tk86t.dll
from this path C:\Users\h280126\AppData\Local\Programs\Python\Python36-32\DLLs
and placed in our .exe path
C:\Users\h280126\PycharmProjects\my_tool\build\exe.win32-3.6
it is working fine :)
After fixing these issues cx_freeze was still unable to import the dependencies of pandas (namely numpy). To fix this I literally copied and pasted the entire folders into the directory of the .py file I was trying to compile. The executable needs to be in the same directory (so it isn't necessarily stand-alone) but it runs with pandas and numpy.

.exe created with cxfreeze-quickstart does nothing - cx_freeze 5.0

I am using python 3.5.1 and the unofficial cx_freeze 5.0 build available from here. I am trying to create an executable version of a python project using tkinter and sympy that I've been working on. I used cxfreeze-quickstart to create a setup.py file for the program, and in terms of building what at least seems to be a valid executable, it works without throwing any errors. However, when I try to run the executable, nothing happens. I know similar questions have been asked on here and I've looked at and tried to understand every one I've found, but none of the solutions have worked for me. I don't understand what's going on, and any help would be appreciated. Code below:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('c:\\users\\joe\\pycharmprojects\\physics2-0\\physics2-0.py', base=base,
targetName = 'c:\\users\\joe\\pycharmprojects\\physics2-0\\physics.exe')
]
setup(name='physics solver',
version = '0.1',
description = 'alpha physics solver',
options = dict(build_exe = buildOptions),
executables = executables)
Thanks in advance.
UPDATE: I am now attempting to write the setup.py script myself according to the template provided in the docs, although any help would still be greatly appreciated.
UPDATE 2: I wrote my own setup.py according to the template provided in the docs, and put it in the same folder as the script I want to freeze, something I hadn't realized I needed to do. I ran python setup.py build in command line, and it created the build subdirectory with the exe and DLLs. However, now when I try and run the exe, an error message pops up that says ImportError: DLL load failed. The specified module could not be found. in reference to tkinter. The code for the 2nd setup.py is below.
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["tkinter", "sympy", "_tkinter"], "excludes": []}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == 'win32':
base = "Win32GUI"
setup( name = "physics solver",
version = "0.1",
description = "a basic physics solver",
options = {"build_exe": build_exe_options},
executables = [Executable("Physics2-0.py", base=base)])
Below are the first 4 lines of physics2-0.py. The line brought into question by the error message is line 1.
from tkinter import *
from tkinter import ttk
from sympy import Symbol
from sympy.solvers import solve
UPDATE 3: Someone please help me out here. I can't figure this out. I've even done a clean re-install of python at this point, just to be sure I didn't accidentally mess something up at some point, and it's still giving me the same error message.

cx_freeze not importing external modules

I choose to try using cx_freeze which converts my simple python 3.x keylogger to an exe. I choose cx_freeze because py2exe is only python 2.x I am compiling my code using this setup.py script.
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
base = 'Console'
executables = [
Executable('logger.py', base=base, targetName = 'logger.exe')
]
setup(name='PyLogger',
version = '0.1',
description = 'A Simple Keylogger',
options = dict(build_exe = buildOptions),
executables = executables)
and I when I compile my code which is
try:
import pythoncom
except ImportError:
input("Import Error, pywin32 is not installed")
try:
import pyHook
except ImportError:
input("Import Error, pyHook is not installed")
I get the import error saying both pywin32 and pyHook is not installed. How do you import external modules into cx_freeze.
EDIT - I have tried changing the setup.py to add the includes option but it made no difference.
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = ['pyHook','pythoncom'],includes = ['pyHook','pythoncom'], excludes = [])
base = 'Console'
executables = [
Executable('logger.py', base=base, targetName = 'logger.exe')
]
setup(name='PyLogger',
version = '0.1',
description = 'A Simple Keylogger',
options = dict(build_exe = buildOptions),
executables = executables)
Find the .pyd file of the external module. Copy and paste that into the build file. So, for example, if it was looking for _cpyHook (I had the same problem as you and it said that module was missing), go to C:\Python33\Lib\site-packages\pyHook and copy and paste the file into C:\Python33\build\exe.win-amd64-3.3.
Try listing the missing packages explicitly in the build options like so:
buildOptions = dict(packages = ['pyHook', 'pywin32'], excludes = [])
And see the accepted answer to this question if you need to include other (non-Python) files in your build.
EDIT: I finally had time to look at this a little more, and it seems to be a tricky problem. I'll keeping poking at it as time permits, but I thought I'd post my findings in case they're useful to the OP. I suspect that the pyHook module doesn't play nice when 'frozen', i.e., when it's included in a zip file. If I use this setup.py:
from cx_Freeze import setup, Executable
buildOptions = dict(
includes=['pythoncom'],
packages=['pyHook']
)
executables = [
Executable('logger.py', base='Console', targetName = 'logger.exe')
]
setup(
name='PyLogger',
version = '0.1',
description = 'A Simple Keylogger',
options = dict(build_exe = buildOptions),
executables = executables
)
the generated logger.exe does not—initially, at least—run correctly, and generates the error:
Import Error, pyHook is not installed
However, if I run the following command from the directory containing the EXE:
unzip library.zip
and re-run logger.exe, then everything seems to work fine. It's just not able to load pyHook from the library.zip file that cx_Freeze generates. I've seen this kind of problem before in the past, and worked around it by munging sys.path in my top-level script prior to loading any modules. I'll see if I can dig up one of those examples. In the meantime, perhaps this advice will help the OP: try unzipping the zip file and see if it makes a difference. A couple things to note:
I'm not having any problems importing pywin32, only pyHook
I did try setting create_shared_zip=False and include_in_shared_zip=False in the build options, but this just resulted in a file named logger.zip instead of library.zip. (Weird. I can't believe that's not a bug.)

cx_Freeze help. ImportError

I'm a newbie to cx_Freeze and I need some help. I'm writing an application with python 3.3, pyqt4 and some more libraries (scipy, numpy, matplotlib, dxfwrite). Now I'm trying to freeze the application with cx_Freeze under windows7. I'm using cx_Freeze-4.3.2.win-amd64-py3.3 and the following setup.py for cx_Freeze:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"] }
setup(
name = "Barrel Cam Editor",
version = "0.2.0",
description = "An editor for Barrel Cams",
options = {"build_exe": build_exe_options},
executables = [Executable("barrelcameditor.py", base = "Win32GUI")])
I run the command python setup.py build and it seems to work but when I launch the obtained exe I get an Importerror: DLL load failed.
I really do not know how to solve this problem. any help?
Tnx
PS:
Thank you for your help, it was a problem with scipy.linalg. I switched to numpy.linalg and it seems to work. Now I've another little problem: I was importing a custom package:
from barrelcam import camdata, camdlg, camwidget
but in this way it is not working.
I found a workaround: I moved the files to the barrelcameditor folder and it seems to work, changing the import to
import camdata, camdlg, camwidget
There is a way to keep the original position of files?
Thank you

Categories

Resources