can't find module 'cx_Freeze__init__' - python

I'm trying to convert my Python project to a standalone executable, in order to run it on other servers that don't have Python installed.
Command used:
python setup.py build > build.log
When I try to run the resulting exe, it always spits out the following error message:
zipimport.ZipImportError: can't find module 'cx_Freeze__init__'
Fatal Python error: unable to locate initialization module
Current thread 0x00000b8c (most recent call first):
I've tried to define all the libraries I'm using throughout my project in the setup.py module, though this has made no difference.
I have also added the DLL files to include (described in the post cx-freeze doesn't find all dependencies).
The project consists of the following libraries (output of pip list):
cx-Freeze (4.3.2)
docopt (0.6.1)
pip (1.5.4)
psutil (2.0.0)
pywin32 (218)
requests (2.2.1)
setuptools (2.2)
virtualenv (1.11.4)
WMI (1.4.9)
Contents of setup.py:
include_files=[
(r'C:\Python34\Lib\site-packages\pywin32_system32\pywintypes34.dll', 'pywintypes34.dll'),
(r'C:\Python34\Lib\site-packages\pywin32_system32\pythoncom34.dll', 'pythoncom34.dll'),]
build_exe_options = dict(
packages=['os', 'concurrent.futures', 'datetime', 'docopt', 'email.mime.text', 'configparser', 'enum',
'json', 'logging', 'psutil', 'requests', 'smtplib', 'socket', 'subprocess', 'sys', 'threading', 'time',
'wmi', 'pythoncom'],
excludes=[],
include_files=include_files)
executable = Executable(
script = 'pyWatch.py',
copyDependentFiles = True,
base = 'Console')
setup( name= "pyWatch",
version= "0.1",
options= {"build_exe": build_exe_options},
executables= [executable])
cx_freeze's output (too large to paste here): http://pastebin.com/2c4hUSeD
All help would be greatly appreciated!

instead of cx_freeze which is last version updated on 2014
there is a module called pyinstaller which is last version update on 2016
pyinstaller
also easy to use just pyinstaller myscript.py and bam

Related

Cannot load mkl_intel_thread.dll on python executable

I'm trying to create an executable python program that runs on windows without python being installed, for this I'm using cx_Freeze. But I get the following error: "Cannot load mkl_intel_thread.dll"
On my PC, which has python installed (miniconda3), I built the executable using cx_Freeze, and when I ran the executable I also would get "Cannot load mkl_intel_thread.dll". I fixed this by going to my python folder, Library\bin, and copied the mkl_intel_thread.dll file to where the executable is placed. The problem is, when moving the whole folder to another PC (without python installed), this error reappears, even though the mkl_intel_thread.dll is in the folder.
File that I want to distribute (plot.py):
import matplotlib.pyplot as plt
a = [0, 1, 2]
b = [0, 2, 0]
plt.fill(a, b, 'b')
plt.show()
cx_Freeze setup file (setup.py):
import cx_Freeze
import sys
import matplotlib
import numpy
import os
os.environ['TCL_LIBRARY'] = "C:\\Miniconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Miniconda3\\tcl\\tk8.6"
executables = [cx_Freeze.Executable("plot.py")]
build_exe_options = {"includes":['numpy.core._methods',
'numpy.lib.format', 'matplotlib.backends.backend_tkagg']}
cx_Freeze.setup(
name = "script",
options = {"build_exe": build_exe_options},
version = "0.0",
description = "A basic example",
executables = executables)
EDIT:
Try to copy all files starting with mkl you find under Library\bin or numpy\core into the build folder, as well as libiomp5md.dll, see Python Pyinstaller 3.1 Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll and cx_freeze converted GUI-app (tkinter) crashes after pressing plot-Button.
Once you have found out which file(s) need(s) to be manually copied, you can let cx_Freeze include the necessary file(s) by using the include_files list of the build_exe options (see code snippet below). If necessary, you can use a tuple (source, destination) as item in the include_files list to let cx_Freeze copy a file from source to a specific destination into the build directory, see the cx_Freezedocumentation.
I see further potential problems in the setup script you've posted in your question:
include the whole numpy packages using the packages list of the build_exe options, it is easier and maybe safer
it is safer to dynamically find out the location of the TCL/TK DLLs
for cx_Freeze 5.1.1, the TCL/TK DLLs need to be included in a lib subdirectory of the build directory
In summary, try t o use
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
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 = {'packages': ['numpy'],
'includes': ['matplotlib.backends.backend_tkagg'],
'include_files': [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
os.path.join('lib', 'tcl86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join('lib', 'tk86t.dll'))
# add here further files which need to be included as described in 1.
]}
in your setup script.
A similar issue affects cx_Freeze 6.1 or 6.2: the executable does not launch, either without error message or with
INTEL MKL ERROR: The specified module could not be found. mkl_intel_thread.dll.
Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll.
Configuration:
Windows 10
Python 3.8.5 installed from https://www.python.org/
numpy 1.19.1+mkl installed with pip using wheel from https://www.lfd.uci.edu/~gohlke/pythonlibs/
various python modules installed using pip
This is also observed with Python 3.6.8 or earlier versions of numpy such as e.g. 1.18.4+mkl or 1.19.0+mkl.
I've observed that cx_Freeze includes 3 DLLs mkl_rt.dll, python38.dll, and vcruntime140.dll in the subdirectory lib\numpy\core of the build directory, whereas the original installation does not contain any DLL in the subdirectory site-packages\numpy\core (all DLLs are in site-packages\numpy\DLLs). If I manually remove mkl_rt.dll from the subdirectory lib\numpy\core of the build directory after building the application with cx_Freeze, the issue disappears and the application works.
This solution can be implemented by adding the following code at the end of the setup.py script:
numpy_core_dir = os.path.join(dist_dir, 'lib', 'numpy', 'core')
for file_name in os.listdir(numpy_core_dir):
if file_name.lower().endswith('.dll'):
file_path = os.path.join(numpy_core_dir, file_name)
os.remove(file_path)
where dist_dir is the build directory generated by cx_Freeze (passed to the build_exe option).
Just copy these four files in cx_freeze generated build folder
mkl_core.dll
mkl_def.dll
mkl_intel_thread.dll
mkl_mc3.dll
Manage to find a solution to this by downgrading numpy==1.18.2 from numpy==1.19.1 when using cx_Freeze==6.5.3.

cx_freeze executable - Py_Initialize: Unable to load the file system codec

I'm using cx_freeze to pack my Python script as a standalone executable.
The exe is running fine on the machine it was packed (with python 3.5 and all the relevant packages).
But when I copied the folder cx_freeze created to another machine the I got this error:
My cx_freeze script:
import sys
import numpy
import os.path
from cx_Freeze import setup, Executable
os.environ['TCL_LIBRARY'] = r'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'
setup(
name = "DocSum",
version = "1.0",
options = {"build_exe": {"packages":["idna","asyncio", "encodings","numpy", "jinja2.ext"]}},
description = "DocSumRESTfulServer",
executables = [Executable("DocSumRESTfulServer.py", base = None)]
)
Any idea what could be the reason? I thought that the exe should be a standalone (run on machines without python). Am I wrong?
It seems that not all dependencies were compiled successfully.
If you want to have a standalone executable, I recommend pyinstaller.
Just pip install it then:
pyinstaller.exe --onefile yourFile.py
The --onefile flag is used to package everything into a single executable. Your executable file would be found on the dist folder.
You could also try this site.
I had the same problem. At the end I discovered that I need to copy also my python37.dll and the lib directory.
If the exe, dll and the directory are on the same directory, it works.
I would like to have a single exe too.

Crosscompile Python bdist_wininst executeables

I'm using a 64bit Windows machine with 64bit python3. I need to build a installable package for a windows 32bit machine and stumbled upon the cross compile feature of the bdist feature: https://docs.python.org/3/distutils/builtdist.html
I'm using a setup.py like this:
from ez_setup import use_setuptools
use_setuptools()
from setuptools import setup, find_packages
setup(name='mypackage',
version='1.0',
description='Some Description',
install_requires=['requests'],
package_dir={'': 'src'},
packages=[''],
entry_points = {'console_scripts': ['somescript = foobar:main']},
)
And build the install packages like so:
python setup.py build --plat-name=win32 bdist_wininst --user-access-control auto
python setup.py build --plat-name=win-amd64 bdist_wininst --user-access-control auto
In both cases I get the correct executeable format for the specified architecture but the defined console_script somescript was not executeable after installation.
The python documentation says that I need to crosscompile the whole python package for windows - but I'am uncertain if this is even necessary because the installer was for the right architecture and I got no error message while the build process.
Is there something wrong with the command? Do I really need to crosscompile or is it sufficiant to have a second 32bit installation of python?
As I found out this is a reported bug https://github.com/pypa/setuptools/issues/253
The setuptools do only check for the OS architecture and ignore the plat-name string for the installation of scripts.
Workaround (until this issue is closed): Use the target architecture for building the wininst.

Is it possible to use cx_freeze on a python 3 project using the pip module?

I am writing an installation program for a larger program I am writing, and I am using CxFreeze to convert it to an executable file, however, when I run the .exe file, it crashes with the line "import pip", and brings up (as shown below), so basically my question is: Is it possible to use CxFreeze on an application with pip imported?
Edit:
Here are all the files I am using:
setup.py (V1):
from cx_Freeze import *
import os, pip
setup(name=("ARTIST"),
version = "1",
description = "ARTIST installation file",
executables = [Executable("Install ARTIST.py"), Executable("C:\\Python34\\Lib\\site-packages\pip\\__init__.py")],
)
This brings up the error:
setup.py (V2):
from cx_Freeze import *
import os, pip
setup(name=("ARTIST"),
version = "1",
description = "ARTIST installation file",
executables = [Executable("Install ARTIST.py"],
options = {"build_exe": {"packages":[pip]}}
)
This brings up an error in the setup.bat file:
Edit:
If anyone wants to look at the website where I am publishing the larger program, here is the link:
alaricwhitehead.wix.com/artist
Edit2:
this is the error i get when i use py2exe:
Edit3:
here is a copy of the code:
https://www.dropbox.com/s/uu46iynm8fr8agu/Install%20ARTIST.txt?raw=1
please note: I didn't want to have to post a link to it, but it was too long to post directly.
The are two problems in your setup script. The first problem is that you specified extra modules to include in your frozen application under the packages option of the build_exe command: packages is for specifying which packages of your application you need to include, for the external modules (such as pip) you need to use includes. The second problem is that you need to pass to includes a list of strings of modules and not the module itself:
setup(
name=("ARTIST"),
version="1",
description="ARTIST installation file",
options={
'build_exe': {
'excludes': [], # list of modules to exclude
'includes': ['pip'], # list of extra modules to include (from your virtualenv of system path),
'packages': [], # list of packages to include in the froze executable (from your application)
},
},
executables=[
Executable(
script='run.py', # path to the entry point of your application (i.e: run.py)
targetName='ARTIST.exe', # name of the executable
)
]
)

py2app ImportError with watchdog

I am attempting to use py2app to bundle a small Python app that I've made in Python 2.7 on Mac. My app uses the Watchdog library, which is imported at the top of my main file:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
When running my program, these import statements work just fine, and the program works as expected. However, after running py2app, launching the bundled application generates the following error:
ImportError: No module named watchdog.observers
At first I thought it was something to do with the observers module being nested inside watchdog, but to test that, I added the line
import watchdog
to the top of my program, and then upon running the app, got the error
ImportError: No module named watchdog
so it seems that it actually can't find the watchdog package, for some reason.
I tried manually adding the watchdog package using py2app's --packages option:
$ python setup.py py2app --packages watchdog
but it had no effect.
My unbundled Python program runs just fine from the command line; other downloaded modules I've imported are giving no errors; and I have successfully bundled a simple "Hello World!" app using py2app, so I believe my setup is correct.
But I'm kind of out of ideas for how to get py2app to find the watchdog package. Any ideas or help would be greatly appreciated.
Edit: Here is the text of my setup.py, as generated by py2applet. I haven't modified it.
from setuptools import setup
APP = ['watcher.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Try manually including the desired packages in the setup.py file:
from setuptools import setup
APP = ['watcher.py']
DATA_FILES = []
PKGS = ['watchdog', /*whatever other packages you want to include*/]
OPTIONS = {
'argv_emulation': True,
'packages' : PKGS,
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
I had installed watchdog 0.5.4, a very old version as it turns out, and got the same error. The error was fixed after upgrading it to 0.8.3:
pip install watchdog --upgrade
Your problem generally indicates that the package (in your case "watchdog", or one of its dependencies) isn't installed, or at least not in a location that py2app expects to find packages.
Do you use the same python command for running py2app as for running the script from the command-line? What is the message of the ImportError you're getting (both when importing "watchdog" and importing "watchdog.observers"?
The (way too long) output of py2app should also mention that it cannot find some packages, and which ones.
As alluded to in one of the answers py2app does not seem to search the same set of paths that are used by the python interpreter, so you need to copy the python library to one of those locations.
For example I've got the MacPorts version of Python installed and found that when I had a module installed in /Library/Python/2.7/site-packages/ py2app wasn't finding it, but it would find it when I copied it into /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. So to copy it over run :
sudo cp /Library/Python/2.7/site-packages/thatmodule.so /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
Then run the py2applet script again and build the app to check. If it's elsewhere you can do a search for all site-packages locations using Spotlight's command line interface:
mdfind -name site-packages

Categories

Resources