compile twisted in pyInstaller - python

I'm currently looking into pyInstaller. I've been using py2exe for now but it would be nice if I'm only using one compiler for all platforms we target. My py2exe setup.py looks like this:
from distutils.core import setup
import py2exe
setup(
name='agent',
description='Service Test',
version='1.00.00',
service=['agent'],
console=['agent.py'],
zipfile=None,
options={ "py2exe":{
"includes":"win32service,win32serviceutil,win32event,servicemanager,autobahn",
"packages": 'twisted, autobahn',
'bundle_files': 1
},
},
)
I've managed to compile the windows service but as soon as i start using twisted it fails.
Commanlines I've used to compile with pyInstaller:
python PyInstaller.py --onefile c:\path\here\agent.py
python PyInstaller.py --hidden-import=twisted --onefile c:\path\here\agent.py
The error I get when I try to install my service
agent.exe?175104\twisted\python\modules.py:758:
UserWarning: C:\dist\path\agent.exe?175104 (for module twisted.web.util) not in path importer cache (PEP 302 violation - check your local configuration).

Related

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 MemoryLoadLibrary failed loading _ssl.pyd, Win7<->Win10

Introduction
I have a script which uses SSL and is build with py2exe (bundle_files=1, pack everything together into the *.exe)
Now I faced this problem
Running py2exe on Win7 creates an *.exe which will run in Win7 and Win10
Running py2exe on Win10 creates an *.exe which will run in Win10 but produces this error in Win7:
ImportError: MemoryLoadLibrary failed loading _ssl.pyd
Workaround
Setting bundle_files to 3 (don't pack) will result in an *.exe which is working fine in Win7 even when it is built on Win10.
I tried out some py2exe options and suddenly it worked, when changing bundle_files. But I don't understand why.
My setup
python 32bit 2.7.11
ssl.OPENSSL_VERSION => 'OpenSSL 1.0.2d 9 Jul 2015'
py2exe 0.6.9
Same on both machines (win7 and win10).
This is a minimal demo to reproduce it:
demo.py
import ssl
print "import done"
It can be built using this
exebuilder.py
from distutils.core import setup
import py2exe
import sys
sys.argv.append("py2exe") # run py2exe (instead of supplying the command line argument)
# exclude some DLLs
dll_excludes = [
# win9x leftovers
"w9xpopen.exe",
# don't import these - otherwise win7 created *.exe won't work in winXP
# http://stackoverflow.com/questions/1979486/py2exe-win32api-pyc-importerror-dll-load-failed
"mswsock.dll",
"powrprof.dll"
]
sys.argv.append("--dll-excludes=%s" % ",".join(dll_excludes))
app_name = "win10ssl"
params = {
'zipfile': None, # pack everything into the *.exe
'options': {
"py2exe": {
"compressed": 1,
"optimize": 2,
# bundle_files
# 1 = EVERYTHING packed into the *.exe
# 2 = everything except for the pythonXX.dll
# 3 = don't pack
"bundle_files": 3
}
},
'version': "0.0.1.0",
'description': "demo to show MemoryLoadLibrary error",
'name': app_name,
'console': [{
"script": "demo.py",
"dest_base": app_name
}
]
}
setup(**params)
Add "crypt32.dll" and "mpr.dll" to your dll_excludes. These are loaded by _ssl.pyd in newer versions of Python such as 2.7.11. But these libraries are Windows system libraries and OS version dependent, so they should not be packaged and distributed with your project. The Win7 "crypt32.dll" probably works on Win10, but the Win10 "crypt32.dll" most likely won't work on Win7.

py2exe can't find module if in different path

I'm trying to create an exe using the following config options -
setup(name='tidalZabbix',
version=version,
description='python module to submit job stats to Zabbix',
url='',
author='Me',
author_email='me#company.com',
license='',
# folders with functions
console=[{'script': os.path.join(BASE_DIR, 'code/tidal_zabbix.py')}],
options={
'build': {'build_base': 'c:/tidalZabbix/build'},
'py2exe':
{
'dist_dir': 'c:/tidalZabbix',
'includes': ['decimal'],
}
}
)
I have a separate module in the code directory called code/ZabbixSender.py
when I try to run python setup.py py2exe I get the following error -
The following modules appear to be missing
['ZabbixSender']
If I move the ZabbixSender.py to the same location as my setup.py the build works fine.
ie. if I do this:
'includes': ['decimal', 'code.ZabbixSender'],
or
'includes': ['decimal', 'ZabbixSender'],
It still doesn't find the Module.
I'd like to think this is bad error reporting by p2exe.
The solution has nothing to do with my py2exe config but in the tidal_zabbix.py script being called.
This was the "incorrect" import
from ZabbixSender import ZabbixPacket, ZabbixSender
As soon as I modified it to this -
from code.ZabbixSender import ZabbixPacket, ZabbixSender
The build worked with no issues. Odd though considering running directly from python had no issues with the code.

cx-Freeze executable ImportError: No module named 'zipfile'

I'm on Windows 7, as are all potential users of my program. I packaged a Python program I wrote into an executable file using cx_Freeze, with the following command:
python setup.py build
This generates a build directory that contains my_program.exe. The executable works flawlessly on my computer, but on a coworker's machine, it throws an exception:
ImportError: No module named 'zipfile'
Here's my setup.py, where zipfile is explicitly included (and it's definitely in library.zip):
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name='Z-Wizard',
version='0.1',
description='Z1/Z2 data extraction tool',
author='Liz Rosa',
author_email='me#url',
options = {
'build_exe': {
'packages': ['zipfile']
}
},
executables = [Executable('my_program.py', base=base)]
)
The traceback is quite long; there's a screenshot at the URL below. It apparently involves a series of functions in _bootstrap.py. I'm not quite sure what's going on here. Also, "C:\Users\lizr..." is my home directory, not hers. Why does it appear in the traceback on her computer? In case it's not obvious, I don't know much about the freezing process.
http://i.imgur.com/cAQKWxq.png

Make exe file from python selenium tests

I try to build my python selenium tests in exe file and run it on many machines for keeping tests independent of the environment. But result *.exe file can't find selenium webdriver. How can I include all selenium dependencies in *.exe file? Or maybe is there some another way for it?
Is it possible to make virtual environment and distribute it?
I am assuming you are using py2exe for generating exe. You will need to specify the location of selenium webdriver in the setup.py file.
Following code should help:
from distutils.core import setup
import py2exe
# Change the path in the following line for webdriver.xpi
data_files = [('selenium/webdriver/firefox', ['D:/Python27/Lib/site-packages/selenium/webdriver/firefox/webdriver.xpi'])]
setup(
name='General name of app',
version='1.0',
description='General description of app',
author='author name',
author_email='author email',
url='',
windows=[{'script': 'abc.py'}], # the main py file
data_files=data_files,
options={
'py2exe':
{
'skip_archive': True,
'optimize': 2,
}
}
)
Like most other binary, it's probably required to include the DLL or whatever library you need, with the binary file. For example:
C:\tests\
run_tests.exe -- this will read from webdriver.dll
selenium-webdriver.dll
Also, from my .NET days i know that you were able to actually embed the library straight into the EXE, which makes it rather large.
You may try pyinstaller,it is simple to install and simple to use.
Install:
pip install pyinstaller
To exe:
pyinstaller yourprogram.py
This is old but I was looking for the same thing and I did have to dig in many different websites to find out my problem, so hopefully this will help others.
I was using py2exe to build my exe file but it didn't work so I decided trying pyinstaller and yeah, it worked.
Just gonna put the things in items to be more organized:
Py2exe:
I first started with py2exe and I was getting error like this:
python setup.py py2exe Invalid Syntax (asyncsupport.py, line 22)
I could fix it by deleting some of the things in the setup file, it looked like this in the end.
data_files = [('selenium/webdriver/chrome', ['C:\Python27\Lib\site-packages\selenium\webdriver\chrome\webdriver.py'])]
setup(
name='General name of app',
version='1.0',
description='General description of app',
author='author name',
author_email='author email',
url='',
windows=[{'script': 'final_headless.py'}], # the main py file
data_files=data_files,
options={
'py2exe':
{
'skip_archive': True,
'optimize': 2,
'excludes': 'jinja2.asyncsupport',
'dll_excludes': ["MSVCP90.dll","HID.DLL", "w9xpopen.exe"]
}
}
)
It could run the py2exe but the exe file didn't work, then I moved to pyinstaller.
Pyinstaller:
For me pyinstaller look way easier than py2exe, so I am keeping with this from now. We only "problem" was that without the webdriver in the path the exe doesn't run.
But as soon as you have it in your variables path you are good to go.
Conclusion, using pyinstaller was my solution + adding webdriver to path.

Categories