Creating an Exe with Selenium Module: Py2exe/Pyinstaller - python

I've looked everywhere. Stackoverflow, various messageboards, the py2exe website, the pyinstaller website...nothing is helping. Including the selenium module, particularly making an exe that supports firefox, seems impossible. I am starting to pull out my hair because using either py2exe and pyinstaller is becoming a huge headache.
Both py2exe and pyinstaller have their share of problems.
My goal is to make one single exe file, without any extra directories, so that other people can use my program if they do not have python/modules.
With py2exe, if I create a setup.py file as such
from distutils.core import setup
import py2exe
setup(
name='Ask Alfred',
data_files = [('Drivers', ['Drivers/chromedriver.exe',
'Drivers/webdriver.xpi','Drivers/webdriver_prefs.json'])],
version='1.0',
description='Find emails fast!',
author='Me',
windows=[{'script': 'alphy.py'}],
options={
'py2exe':
{
'skip_archive': False,
'optimize': 2,
}
}
)
it will create an exe in the dist folder and a Drivers folder with the files I need, however, if I try to run the exe it will tell me that it could not find these files (because it is looking for them in the library.zip folder). On top of that, my GUI looks terrible (fonts are now grey instead of black and images with white backgrounds now have grey backgrounds).
With pyinstaller, if I use the "--onefile" flag when creating the exe it does not work at all/neither firefox nor chrome gets started.
With either, I only get workable results if I choose to not archive/not make one file. In that case, pyinstaller delivers a fully working solution.

Try this:
options={
'py2exe':
{
'skip_archive': True,
'unbuffered': True,
'bundle_files': 2, #assuming you dont want to include the python interpreter
'optimize': 2,
},
},
zipfile = None

Related

Bundle sqldrivers into .exe using py2exe

In my first attempts, my pyQt application bundled with py2exe refused to connect to the sqlite database although it was working in its python version.
I guessed that it was a problem of libraries not loaded into the .exe application. I solved that problem by including the full path to the sqlite DLL into the setup.py file and thus copying this DLL to the executable folder.
Now I would like to include this DLL into the .exe file in order to "hide" this DLL to my users. Do you have a clue how to do that ?
my current setup.py:
from distutils.core import setup
import py2exe
setup(
windows=[{
"script": 'myscript.py'
}],
options={
'py2exe': {
"dll_excludes": [
"MSVCP90.dll",
"MSWSOCK.dll",
"mswsock.dll",
"powrprof.dll",
],
'includes': [
'sip',
'PyQt4.QtNetwork',
],
'bundle_files': 1,
}
},
data_files = [
'config.ini',
'template.htm',
# This is the File that I wish to be "hidden"
('sqldrivers', ('C:\Python27\Lib\site-packages\PyQt4\plugins\sqldrivers\qsqlite4.dll',)),
zipfile=None,
)
I ran into the same problem and you are half way to solving the issue. The first part of the problem is as you identified, getting the file into the EXE. I can't speak to the correctness of your py2exe solution as I am using pyinstaller, but that is the general idea. You need to get the qsqlite4.dll into a sqldrivers directory within your single file app.
The second part is that your main .py needs to have the path added to its running directory which will now contain that sqldrivers folder. What you will need to do is get the relative path to where your main .py is running and set that directory as your library path in your QT application. I use the standard resource_path() function for pyinstaller, but using something like this should work for py2exe:
def resource_path(relative_path)
if sys.frozen:
base_path = os.path.dirname(sys.executable)
else:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
Then you can use this code in the main function of your application
app = QApplication(sys.argv)
new_lib_path = app.libraryPaths()
new_lib_path.append(resource_path(''))
app.setLibraryPaths(new_lib_path)
. . .
With logging added, here is my app.libraryPaths() before and after:
08/25/2014 01:33:24 AM CRITICAL: Before[u'C:/dev/WORKSP~1/db/dist']
08/25/2014 01:33:24 AM CRITICAL: After[u'C:/dev/WORKSP~1/db/dist', u'C:\\Users\\jeff\\AppData\\Local\\Temp\\_MEI2042\\']
You could replace the '\' with '/' but I didn't bother, it still works with windows separators.

Compiling docx with py2exe

I'm compiling a python script to .exe via py2exe. I originally started compiling it and the entire program ran fine aside from the Word document creation.
My logfile would give me: ERROR: Could not close or save Word Document 'docName.docx' :, so where I am supposed to be supplied an error message - I am not.
I started to think it could do with the missing modules in py2exe, reported as:
The following modules appear to be missing
['ICCProfile', '_imaging_gif', '_scproxy', '_sysconfigdata']
And then I noticed many people didn't care too much about these errors.
I looked in the docx package, located in C:\Python27\Lib\site-packages\docx-0.2.4-py2.7.egg\, as installed by easy_install, and saw this docx-templates folder, which I am certain was not imported, so I wrote my setup.py referencing the docx-template I put in my build folder manually:
from distutils.core import setup
from glob import glob
import os
import py2exe
#templatePath = 'C:/Python27/Lib/site-packages/docx-0.2.4-py2.7.egg/docx-template'
setup(
console=['xmlpolicydocx-0.4.py'],
options={
'py2exe':
{
'includes': ['docx', 'PIL', 'lxml.etree', 'lxml._elementpath', 'gzip']
}
},
packages=[
'docx-template'
],
package_data={
'docx-template': [
'_rels/*',
'docProps/*',
'word/theme/*.xml',
'word/*.xml'
],
},
)
Yet I still have no luck in getting docx to work when compiled via py2exe. Any suggestions or methods of debugging I can take to take another look at solving this problem?

Compiling a python script I've written as an .exe and .app

To preface, I have very little python knowledge, so the simplest solution will probably be the best one.
Essentially, I've written a program that takes user input and writes it to a text file. I've created a GUI with Qt and PySide. What I want to do now is to compile everything together to be a single .exe file that I can just drop into the laps of anyone who wants to use it. Basically, it needs to be able to run out of 1 single .exe file on a computer that doesn't necessarily have any of the python libraries installed that mine does.
The only imports on the program are
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from math import *
in case those are important to the compiling. Thank you for the help, I appreciate it.
P.S. It's for my grandmother, who has nearly no knowledge of computers whatsoever. If possible, it'd be awesome if it would just...open. I don't mind if it has to run something in cmd to install all the python stuff, as long as the .exe from which the program runs is still JUST an .exe.
I suspect your grandmother uses windows in which case I would recommend using py2exe.
Here is probably all than you need...
1). create the below script and modify the last line of it to be the name of the actual script (see its last line)
#execmaker.py would be the name of this file
#stable version
from distutils.core import setup
import py2exe
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter']
packages = []
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
'tk84.dll']
setup(
options = {"py2exe": {"compressed": 2,
"optimize": 2,
"includes": includes,
"excludes": excludes,
"packages": packages,
"dll_excludes": dll_excludes,
"bundle_files": 3,#dont bundle else unstable
"dist_dir": "dist",
"xref": False,
"skip_archive": False,
"ascii": False,
"custom_boot_script": '',
}
},
windows=['My_Script.py'] #this is the name of your actual script
)
2). Then you can go to the directory where this script and your actual script resides via cmd and then type
python execmaker.py py2exe
You now should have a working executable. Now a you can double click on the executable and your script will run. oh yeah and if you have questions follow this guy's instructions...he is good!
http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/
download pyinstaller (http://www.pyinstaller.org/)
open your cmd prompt
cd folder
c:\pyinstaller\pyinstaller.py --noconsole --onefile my_script.py
the exe should be found in the dist folder that is created

Py2Exe and Easgui

I am trying to convert a py file to an exe.
Here is the code for my setupfile
from distutils.core import setup
import py2exe
setup(console=["mycode.py"])
When I use cmd, it says:
Import Error: No module named easygui
How do I let py2exe know about the easygui? As well as the numpy and mathplotlib (all are used in mycode.py)
First, use pyinstaller. It is newer and better (though I have used py2exe until switching to pyinstaller) And it seems to have much better recipes for finding your included libs.
But for py2exe, you will need to expand that setup.py a bit more to tell it what to include (since they are probably hidden imports)
setup(
console=["mycode.py"],
options={
"py2exe": {
"includes": ["easygui"],
"bundle_files": 1
},
},
zipfile = None,
)
If this fails to build, then easygui is not in your PYTHONPATH properly. Make sure you are not doing something special in your script to add a pythonpath, which would not be visible to py2exe.
You may need to do a little more work with this file for numpy and matplotlib. See this wiki for help
Relative to the issue of the specific dll's mentioned, I had similar issues but fixed those problems by specifically excluding those in the setup lie so:
setup(
console=['DET14.py'],
options={
'py2exe': {
'packages' : ['matplotlib', 'pytz'],
'dll_excludes':['MSVCP90.DLL',
'libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll'],
'includes':['scipy.sparse.csgraph._validation',
'scipy.special._ufuncs_cxx']
}
},
data_files=matplotlib.get_py2exe_datafiles()
)`
I would say try adding that exclude to your setup statement.

PyQt/PySide - icon display

I have a PySide app which has an icon for the MainWindow (a QMainWindow instance). When I run the file normally, the icon is visible and everything is fine but when I create an exe with py2exe, the icon does not appear. This happens with cx_freeze also( so I don't think the problem's with py2exe).
The app was designed using QtDesigner and converted to python with pyside-uic. I tried both using icons as a file and as a resource(qrc file) and both don't seem to work.
Any help or pointers would be appreciated.
Thanks.
kochelmonster's solution works so long as you don't try and bundle the Qt dlls into library.zip or the exe. You also don't need to set a library path if you put the plugins in the base of the app directory.
I still wanted to bundle everything else so I excluded the qt dlls and added them manually. My setup.py looks something like this:
from os.path import join
_PYSIDEDIR = r'C:\Python27\Lib\site-packages\PySide'
data_files =[('imageformats',[join(_PYSIDEDIR,'plugins\imageformats\qico4.dll')]),
('.',[join(_PYSIDEDIR,'shiboken-python2.7.dll'),
join(_PYSIDEDIR,'QtCore4.dll'),
join(_PYSIDEDIR,'QtGui4.dll')])
]
setup(
data_files=data_files,
options={
"py2exe":{
"dll_excludes":['shiboken-python2.7.dll','QtCore4.dll','QtGui4.dll'],
"bundle_files": 2
...
}
}
...
)
If your project uses additional Qt dlls you will have to exclude and manually add them as well. If you need to load something other than an .ico image you'll also need to add the correct plugin.
I'm assuming it works with a bmp, but not a png/jpg? If so, it's likely that the image format plugins don't load properly.
I'd guess setting up a qt.conf file in the installed application's directory and making sure plugin-dll's go to /plugins/imageformats/ will make things work better.
I had the same problem. After some investigation I found a solution:
(Macke had the right idea)
cx_freeze does not copy the PyQt plugins directory, which contains the ico image reader.
Here are the steps:
in setup.py copy the PyQt4 plugins directory to your distribution
In your code write something like:
application_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
try:
if sys.frozen:
plugin_path = os.path.join(application_path, "qtplugins")
app.addLibraryPath(plugin_path)
except AttributeError:
pass
Could it be related to Windows 7's taskbar icon handling?
See How to set application's taskbar icon in Windows 7 for an answer to that.
You must include "qico4.dll" manually in your release folder. Insert this in your setup.py:
import sys
from os.path import join, dirname
from cx_Freeze import setup, Executable
_ICO_DLL = join(dirname(sys.executable),
'Lib', 'site-packages',
'PySide', 'plugins',
'imageformats', 'qico4.dll')
build_exe = {
'include_files': [(
_ICO_DLL,
join('imageformats', 'qico4.dll'))]}
setup(name = "xxxxx",
version = "1.0.0",
...
options = { ...
'build_exe': build_exe
...},
...)

Categories

Resources