Py2Exe - can't find modules - python

I want to create an exe file using Py2exe module. The problem is that the exe file says that there is not os module. I've put it into includes in setup.py file so it should work.
Here is the error after run main.exe created by Py2Exe
import linecache
ImportError: No module named linecache
Traceback (most recent call last):
File "main.py", line 3, in <module>
ImportError: No module named os
And here is my setup.py:
from distutils.core import setup
import py2exe
setup(console=["main.py"],options = {
"py2exe":{
"includes": ["os","linecache"]
}
},)

The problem is that if you want to import packages, you should use the option packages and not includes. The first one imports libraries, the second modules.py.This should work now:
from distutils.core import setup
import py2exe
setup(console=["main.py"],
options = {
"py2exe":{
"packages": ["os","linecache"]
}
})

Related

cx_freeze - ImportError: numpy.core.multiarray failed to import

After building my exe, when I run it I get an error saying that it failed to import numpy.core.multiarray.
What I have already tested:
Updated numpy to latest version
Checked if I have more than one version of numpy
The file multiarray is inside the build/../numpy/core/multiarray
If i run python on console i can "from numpy.core import multiarray" without any problem
Packages I use: Easygui, Opencv2, pytesseract, os, pillow, regex
I'm running python 3.6.1 on W10
This is my setup.py.
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'C:\Users\Farinha\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\Farinha\Anaconda3\tcl\tk8.6'
includes = []
include_files = [r"C:\Users\Farinha\Anaconda3\DLLs\tcl86t.dll", \
r"C:\Users\Farinha\Anaconda3\DLLs\tk86t.dll"]
setup(name='InstantScale',
version = '0.1',
description='Parse stuff',
options = {"build_exe": {"includes": includes, "include_files": include_files}},
executables = [Executable("main.py")])
And the error when i run a bat to pause the console
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "C:\Users\Farinha\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\Users\Farinha\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "main.py", line 2, in <module>
ImportError: numpy.core.multiarray failed to import
All help welcome, thanks in advance
Copy the numpy packages directly into your directory.
then add these lines :
import numpy.core._methods
import numpy.lib.format
more information in this post
I Manage to fix it.
I added manually the package to the options.
includes = []
include_files = [r"C:\Users\Farinha\Anaconda3\DLLs\tcl86t.dll", \
r"C:\Users\Farinha\Anaconda3\DLLs\tk86t.dll"]
packages = ["numpy"]
setup(name='InstantScale',
version = '0.1',
description='Parse stuff',
options = {"build_exe":{"includes": includes, "include_files":
include_files, "packages":packages}}
In my case, the error was happening when using optimize=2 in cxFreeze options. More info: https://github.com/numpy/numpy/issues/13248
setup(name='InstantScale',
version = '0.1',
description='Parse stuff',
options = {"build_exe": {"optimize": 1}},
executables = [Executable("main.py")])

Python - using py2exe with pandas

I'm trying to create an *.exe file from my Python code.
This is my setup.py file:
from distutils.core import setup
import py2exe
import matplotlib
import sys
sys.setrecursionlimit(5000)
setup(
options={
'py2exe':
{
'includes': ['lxml.etree', 'lxml._elementpath', 'gzip','pandas'],
}
},
data_files=matplotlib.get_py2exe_datafiles(),
console=['test_zone_A_main.py'])
The code works fine on my machine, but when I'm running the exe on different machine , I'm getting the following error:
C:\Program Files (x86)\company\Campaign_Analyze>"C:\Program Files (x86)\sintec\Ca
mpaign_Analyze\dist\test_zone_A_main.exe"
Traceback (most recent call last):
File "test_zone_A_main.py", line 9, in <module>
File "calcs_performence.pyc", line 11, in <module>
File "test_config_cs.pyc", line 11, in <module>
File "pandas\__init__.pyc", line 13, in <module>
ImportError: C extension: DLL load failed: The specified module could not be found. not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.
Any ideas?

How to identify which .pyd files are required for the execution of exe files generated using py2exe module

I have written a python script and generated an exe using py2exe on Windows 32 bit OS. While I'm trying to execute the generated exe file, I'm getting the below error:
Traceback (most recent call last):
File "program01.py", line 3, in <module>
File "PIL\Image.pyc", line 67, in <module>
File "PIL\_imaging.pyc", line 12, in <module>
File "PIL\_imaging.pyc", line 10, in __load
ImportError: DLL load failed: The specified module could not be found.
Is there any way to identify the complete list what .pyd files are required for my program to be executed.
Below is my program import statements.
from __future__ import division
import os, sys, math, aggdraw
from PIL import Image
import xml.etree.ElementTree as ET
import lxml.etree as LETREE
Any kind of help would be appreciated!!!
Thanks,
Ram
You can include modules by adding options argument into setup:
options = {'py2exe': {'bundle_files': 1, 'compressed': True, "includes" : ['os', 'sys', 'math', 'aggdraw', 'PIL', 'xml.etree.ElementTree', 'lxml.etree' ]}
}
Only thing that could be different in code above is that you might need to replace xml.etree.ElementTree with xml and lxml.etree with lxml as I'm not quite sure about those.

ImportError: No module named allcontrols with pywinauto and py2exe

I want to generate an .exe file from a python script which includes the pywinauto module.
It builds fine, however when running the resulting dist\pywinauto_sample.exe, I get this error:
Traceback (most recent call last):
File "pywinauto_sample.py", line 2, in <module>
from pywinauto import application
File "pywinauto\application.pyc", line 68, in <module>
File "pywinauto\controlactions.pyc", line 45, in <module>
File "pywinauto\tests\__init__.pyc", line 128, in <module>
File "pywinauto\tests\__init__.pyc", line 114, in __init_tests
ImportError: No module named allcontrols
Here's my pywinauto_sample.py:
import pywinauto
from pywinauto import application
app = pywinauto.application.Application()
And here's my setup.py:
from distutils.core import setup
import py2exe
setup(console=['pywinauto_sample.py'])
I compile the program with:
python setup.py py2exe
Add this to your setup.py:
from distutils.core import setup
import py2exe
setup(
console=
[
'pywinauto_sample.py'
],
options=
{
"py2exe":
{
"includes":
[
"pywinauto.tests.truncation",
"pywinauto.tests.translation",
"pywinauto.tests.repeatedhotkey",
"pywinauto.tests.overlapping",
"pywinauto.tests.missingextrastring",
"pywinauto.tests.missalignment",
"pywinauto.tests.miscvalues",
"pywinauto.tests.leadtrailspaces",
"pywinauto.tests.comparetoreffont",
"pywinauto.tests.comboboxdroppedheight",
"pywinauto.tests.asianhotkey",
"pywinauto.tests.allcontrols",
]
}
}
)
Source: http://permalink.gmane.org/gmane.comp.python.py2exe/4663

Create standalone MoinMoin wiki executable

I'm trying to create a standalone, desktop version of a MoinMoin wiki so I can distribute it on a CDROM to people who may or may not have Python installed. I've tried both py2exe and bbfreeze with no luck. They both create an executable, but when that executable is run I get the same error from both:
C:\python_class\cdrom\bb-binary>wikiserver.exe
2011-08-22 15:06:21,312 WARNING MoinMoin.log:138 load_config for "C:\python_class\cdrom\bb-binary\wikiserverlogging.conf
" failed with "No section: 'formatters'".
2011-08-22 15:06:21,312 WARNING MoinMoin.log:139 using logging configuration read from built-in fallback in MoinMoin.log
module!
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "__main__.py", line 128, in <module>
File "__main__wikiserver__.py", line 35, in <module>
File "MoinMoin/script/__init__.py", line 138, in run
File "MoinMoin/script/__init__.py", line 248, in mainloop
File "MoinMoin/wikiutil.py", line 1078, in importBuiltinPlugin
File "MoinMoin/wikiutil.py", line 1117, in builtinPlugins
File "MoinMoin/util/pysupport.py", line 81, in importName
ImportError: No module named server
Here is the setup.py script I used for py2exe:
from distutils.core import setup
import py2exe
includes = ["MoinMoin"]
excludes = []
packages = []
setup(options = {
"py2exe" : {
"includes" : includes,
"excludes" : excludes,
"packages" : packages,
"dist_dir" : "dist"
}
},
console=["wikiserver.py"])
And here is the setup.py script I used for bbfreeze:
from bbfreeze import Freezer
includes = ["MoinMoin.*"]
excludes = []
f = Freezer(distdir="bb-binary", includes=includes, excludes=excludes)
f.addScript("wikiserver.py")
f.use_compression = 0
f.include_py = True
f()
If anyone has any help or suggestions, I'd greatly appreciate it!
Thanks,
Doug
py2exe has limitations in identifying which modules to include, especially if they are imported conditionally. For example,
import module
on its own line will work, however,
if someCondition:
import module
won't. As is often the case with many large frameworks, MoinMoin only imports the modules it needs to use when it needs them. Unfortunately, you will need to tell py2exe to include these missing modules manually, and this is going to take some trial-and-error until you find all the ones you need.
See here for how to include modules manually.

Categories

Resources