Cannot run generated .exe with py2exe - python

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

Related

How to use cx_Freeze to make an executable python program

I downloaded cx_Freeze because I'm trying to make a .exe file to share with my platoon and I've been reading through the docs as well as scrolling through cx_Freeze tutorial. After following both of those I still don't know why this isn't working for me. I'm on Python 3.6.2 and I have the path directly setup to the command line.
I tried to launch with setup.py and Julian date 2.py on the desktop and I tried adding them to same folder, but no matter what I try I get back this error when I type python setup.py build, python: can't open file 'setup.py': [Error2] no such file or directory or file exsists. Below is my setup.py code.
from cx_Freeze import setup, Executable
setup(name = "Julian date 2" ,
version = "0.1" ,
description = "" ,
executables = [Executable("Julian date 2.py")])
Another issue I ran into was trying to type cxfreeze Julian date 2.py --target-dir dist I get the error 'cxfreeze' is not recognized as an internal or external command, operable program or batch file.
When you type python setup.py build, you are supposed to be in the directory with setup.py and not anywhere else. So use the command cd to get there.
cx_freeze is not in your path variable so cxfreeze Julian date 2.py --target-dir dist will not work and you have to instead add it to your path (somehow) [not recommended]
Hope this helped.
P.S.
executables = [Executable("Julian date 2.py")]) takes base too. If you want a console application:
executables = [Executable("Julian date 2.py",base='None')])
Gui for windows:
executables = [Executable("Julian date 2.py",base='Win32GUI')])
And you forgot your exe options in setup(). I recommend adapting the setup.py script on cx_freeze doxs:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = "None"
setup( name = "name",
version = "0.1",
description = " ",
options = {"build_exe": build_exe_options},
executables = [Executable("file.py", base=base)])
I solved the first issue, my file was named 'setup.py' and not just 'setup' as it's supposed to be...The name must be setup, the extension .py
Know it's DUMB, after hours, that was the problem...

APScheduler import error when generate executable with py2exe or cx_freeze

When generating an executable on python 2.7 using py2exe (0.6.9) or cx_freeze (5.0.1) with APScheduler (3.3.1) it gives me the following error:
File "apscheduler\__init__.pyc", line 2, in <module>
File "pkg_resources\__init__.pyc", line 552, in get_distribution
File "pkg_resources\__init__.pyc", line 426, in get_provider
File "pkg_resources\__init__.pyc", line 968, in require
File "pkg_resources\__init__.pyc", line 854, in resolve
pkg_resources.DistributionNotFound: The 'APScheduler' distribution was not found and is required by the application
This is my cx_freeze setup.py file:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"includes": ["requests", "apscheduler"], "include_files": ["XXX"]}
# 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 = "XXX",
version = "XXX",
description = "XXX",
options = {"build_exe": build_exe_options},
executables = [Executable("XXX.py", base=base), Executable("XXX2.py", base=base)])
And this is my py2exe setup.py file:
from distutils.core import setup
import py2exe
data_files = ['XXX']
setup(
data_files=data_files,
windows=[
{'script': 'XXX.py'},
{'script': 'XXX2.py'},
],
options={'py2exe':{
'includes': ['requests', 'apscheduler'],
'bundle_files': 1,
}
},
)
I already tried to use the 'packages' option but with no success.
If I remove the code from APScheduler __init__.py (apscheduler/__init__.py), it works.
Below is the __init__.py from APScheduler package:
# These will be removed in APScheduler 4.0.
release = __import__('pkg_resources').get_distribution('apscheduler').version.split('-')[0]
version_info = tuple(int(x) if x.isdigit() else x for x in release.split('.'))
version = __version__ = '.'.join(str(x) for x in version_info[:3])
Do I need to include somehow the dependencies into py2exe/cx_freeze library pack?
I already did some research on the internet but with no success.
Found the solution. The problem was py2exe doesn't include the dist-info directory to library.zip. Each module has its own dist-info directory in the site-packages python libraries.
Those directories are used by pkg_resources libraries to import modules
as we can see on the apscheduler __init__.py:2
All you have to do is to add those dist-info directories to the library.zip file generated by py2exe.
Below is a example from google importing the (non-related, but could be used as an example) zoneinfo directory to library.zip.
https://github.com/google/transitfeed/blob/master/setup.py#L96
To get the dist-info path of a module, use follow:
import os
import pkg_resources
dist_info_dir = pkg_resources.get_distribution('desired_module')._provider.egg_info
# get base name of directory
base_name = os.path.basename(dist_info_dir)
Note: If there is no dist-info directory and an egg-info instead. You should probably search for the wheel package for the library or build yourself with:
python setup.py bdist_wheel
Cheers!

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.

Cannot find module "cx_freeze__init__" when freezing Python script

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!

Having problems with making an .exe with cx_freeze with python and pygame, including additional files

I'm having trouble creating an .exe file with cx_Freeze. I'm trying to use this Pygame game which needs some .png's, .gif's and .ogg's to run. I have tried to compile a simple Python only (no pygame or additional files) using the commmand line and a setup.py, but neither worked and I'm a bit out of my death.
I have installed cx_Freeze and checked it worked with ''import cx_freeze' in the IDLE not throwing an error. I'm using Python 3.3 on Windows 7 with the correct versions of pygame and cx_freeze for my python version.
Can anyone assist me in creating this .exe?
To include files in your .exe you should write a setup.py file that is similar to this:
from cx_Freeze import setup, Executable
exe=Executable(
script="file.py",
base="Win32Gui",
icon="Icon.ico"
)
includefiles=["file.ogg","file.png",etc]
includes=[]
excludes=[]
packages=[]
setup(
version = "0.0",
description = "No Description",
author = "Name",
name = "App name",
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [exe]
)

Categories