py2exe `ImportError: No module named backend_tkagg` - python

I am trying to make a windows executable from a python script that uses matplotlib and it seems that I am getting a common error.
File "run.py", line 29, in
import matplotlib.pyplot as plt File "matplotlib\pyplot.pyc", line 95, in File "matplotlib\backends__init__.pyc", line
25, in pylab_setup ImportError: No module named backend_tkagg
The problem is that I didn't found a solution while googling all over the internet.
Here is my setup.py
from distutils.core import setup
import matplotlib
import py2exe
matplotlib.use('TkAgg')
setup(data_files=matplotlib.get_py2exe_datafiles(),console=['run.py'])

First, the easy question, is that backend installed? On my Fedora system I had to install it separately from the base matplotlib.
At a Python console can you:
>>> import matplotlib.backends.backend_tkagg
If that works, then force py2exe to include it. In your config:
opts = {
'py2exe': { "includes" : ["matplotlib.backends.backend_tkagg"] }
}

If you are using py2exe it doesn't handle .egg formatted Python modules. If you used easy_install to install the trouble module then you might only have the .egg version. See the py2exe site for more info on how to fix it.
http://www.py2exe.org/index.cgi/ExeWithEggs

This works well
from distutils.core import setup
import py2exe, sys, os
import matplotlib
sys.setrecursionlimit(12000)
sys.argv.append('py2exe')
setup(
options = {
"py2exe" : {
"bundle_files":3,
"compressed":True,
"includes" : ["matplotlib.backends.backend_tkagg"]
}
},
windows = [{"script": "script.py"}],
zipfile = None,
data_files=matplotlib data_files = matplotlib.get_py2exe_datafiles(),
)

Run the following command to install the backend_tkagg
For centos -- sudo yum install python-matplotlib-tk
This should work.

Related

cx_freeze and cryptodome error after compilation

This error appears after compiling my Python 2.7 project with cx_freeze : https://imgur.com/a/sNvYtEO
I have the impression that the error comes from the package pycryptodome / pycryptodomex which is well installed since everything works before compiling with cx_freeze.
I tried to modify the import with :
from Crypto.Cipher import AES
Instead of :
from Cryptodome.Cipher import AES
But there is always the same error..
Here are my build options on cx_freeze :
build_options = {
'packages': ['jinja2.ext'],
'namespace_packages':['zope'],
'includes': ['zope.interface', 'M2Crypto'],
'excludes': ['Tkinter']
}
I will be happy to try other solutions if you have ideas, thanks !
Try to modify the import (in your main script or importing module) as
import cffi
import _cffi_backend
from Cryptodome.Cipher import AES
If this does not work, try to add 'cffi' and '_cffi_backend' to the includes list in your setup script.
If this still does not work, see the cffi documentation and this resource for further suggestions.

Cannot run generated .exe with py2exe

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")])

py2exe executes file but doesn't attempt to build

My question is rather simple: if I try to build my .exe using:
python setup.py py2exe
Python just executes my main file, starting my application, however py2exe doesn't try to build it. Meaning: it does the same as if I'd do:
python setup.py
I guess something is wrong with the distutil?
Anybody already encountered this problem?
py2exe is installed (I reinstalled it, hoping it'd fix it).
My Code:
from testmain import *
from Initfile import *
import math
import py2exe
import matplotlib
import PyQt4
import numpy
from distutils.core import setup
setup(
windows=['Initfile.py'],
data_files=[("GUI", ["testmain.ui"]),*matplotlib.get_py2exe_datafiles()],
options = {
'py2exe': {
'includes' : ['math','py2exe','numpy','matplotlib','PyQt4']
}
}
)
Well the error isn't within distutil, it seems when one imports the entry point itself (in my case Initfile.py) pyexe isnt executing.
So the solution is: remove the line from Initfile import *

py2exe error when running exe file

I have successfully converted .py file to .exe file using py2exe.
I can successfully run the .py file,if i run it standalone.However, when iam trying to run the .exe file, it throws an error as seen in attached image.
In my .py file, i have the below import statements:
import xlrd,xlwt,xlutils.copy,re,time,openpyxl,os
from openpyxl.styles import Alignment
from openpyxl import load_workbook
I have also accordingly tweaked setup.py file to include these packages as below setup.py code shows
from distutils.core import setup
import py2exe
setup(
console=['vu_t2.py'],
options = {
'py2exe': {
'packages': ['xlrd','xlwt','xlutils','openpyxl','openpyxl.workbook']
}
}
)
Please refer the attached error snapshot
I used the below command to run py2exe
python setup.py py2exe
openpyxl only supports distribution via pip.

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".

Categories