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.
Related
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")])
Using py2exe I create an exe version of my script with instructions I found here. The script compiles well and generates a dist and build folder each but when I run the program on the command line it gives this error. N.B the script works fine on my IDE environment, however I intend give a colleague the exe version.How can I fix this error?
Traceback (most recent call last):
File "tester2.py", line 4, in <module>
ImportError: No module named mechanize
here is the setup.py file:
from distutils.core import setup
import py2exe
setup(
console = ['tester2.py'],
zipfile = None,
)
You have to declare your dependencies.
This is my setup
setup(
executables=executables,
options=options,
name='bla',
version='0.3',
packages=[...],
url='',
license='',
author='',
author_email='',
description='', requires=['pandas', 'unidecode', 'click',
'xlsxwriter'] // you would have to add mechanize here
)
Have you added files to the build?
Pls have a look at include option in setup.py: exe built with cx_Freeze, PyQt5, Python3 can't import ExtensionLoader_PyQt5_QtWidgets.py and run
Also here's my solution to similar problem, how to add files to build and run them later: Python - create an EXE that runs code as written, not as it was when compiled
I am a beginner in python and django. This error keeps coming, even after installing cx_freeze from http://www.lfd.uci.edu/~gohlke/pythonlibs/#cx_freeze
I am trying to make an executable file and I want to run my server through it, which I normally do by:
manage.py runserver
I am currently using commands:
setup.py build
I have already tried:
pyinstaller --name=mysite myproject_dir/manage.py
and my setup.py file contains
import sys
from distutils.core import setup
from cx_Freeze import setup, Executable
setup(
name = "Management System",
version = "1.0",
description = "A Database Management System",
py_modules=['virtualenv'],
executables = [Executable("manage.py", base = "Win32GUI")])
I have also tried py2exe, it doesn't work. You can also suggest to read something for this knowledge.
Here is the image of error that keeps appearing on running the exe file
If I use this command:
Barcode.exe runserver
There also comes an error
WindowsError: [Error 3] The system cannot find the path specified:'C:\\Users\\D ell\\AppData\\Local\\Temp\\_MEI85~1\\Entry\\migrations/*.*'
I have created the following Python script using the gspread and oauth2 modules
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
credentials = SignedJwtAssertionCredentials("client_email","private_key", "https://spreadsheets.google.com/feeds")
gc = gspread.authorize(credentials)
spreadsheet = gc.open_by_key("spreadsheet_id")
worksheet = spreadsheet.worksheet("sheet_name")
lstoforders = worksheet.get_all_values()
...some extra code...
When I run this code as a .py file everything works smoothly. However, when I try to package it into an executable Windows program using py2exe, I get the following output
The following modules appear to be missing
['ElementC14N', 'IronPythonConsole', 'System', 'System.Windows.Forms.Clipboard', '_scproxy', 'ca_certs_locater', 'clr', 'console', 'email.FeedParser', 'email.Message', 'email.Utils', 'google.appengine.api', 'google.appengine.api.urlfetch','google3.apphosting.api', 'google3.apphosting.api.urlfetch', 'http', 'modes.editingmodes', 'oauth2client.client' 'pyreadline.keysyms.make_KeyPress', 'pyreadline.keysyms.make_KeyPress_from_keydescr', 'pyreadline.keysyms.make_keyinfo', 'pyreadline.keysyms.make_keysym', 'startup', 'urllib.parse']
Accordingly, when I try to run the resulting exe file, I get the following error
Traceback (most recent call last):
File "gspread_to_autocomplete_json.py", line 2, in <module> ImportError: No module named oauth2client.client
It appears as if py2exe cannot find the gspread and oauth2client.client modules. These modules are installed on my machine.
Does anybody have a clue why this is happening?
Thanks.
Nicola
You can choose in your setup.py with packages and modules you want to include.It might be that your setup script is not finding all the dependencies automatically (actually it is pretty common).Try to have a look at the answer I gave to this question.
Add options to your setup.py
You can also use more py2exe options in order that you are importing all the modules and the packages required by your project. E.g.
# setup.py
from distutils.core import setup
import py2exe
setup(console=["script.py"],
options={
"py2exe":{
"optimize": 2,
"includes": ["mf1.py", "mf2.py", "mf3.py"], # List of all the modules you want to import
"packages": ["package1"] # List of the package you want to make sure that will be imported
}
}
)
In this way you can force the import of the missing script of your project
I have this line of code:
xl = win32.gencache.EnsureDispatch("Excel.Application")
This works fine when I run the script.
However, I exported the python script to be an exe with py2exe, and after running the exe, it gives this error:
KeyError: '{000208D5-0000-0000-C000-000000000046}'
All I did was:
setup.py
from distutils.core import setup
import py2exe
setup(console=['xerxes2excel.py'])