tags:

views:

303

answers:

1

Hi there:

I've been trying to pack my app with py2exe. The application works fine but it keeps failing to find/use pywinauto. I been googling but I get nothing, I'm now I'm totally lost...

Here's the packing script:

from distutils.core import setup

setup(
  windows = ["mainForm.py"],
  data_files=[
      ('', ['mainForm.ui']),
      ('', ['osk.sqlite'])
  ],
  options = {
              "py2exe":{
                        "optimize": 2,
                        "includes": [
                          'sip', 'pyttsx.drivers.sapi5', 'win32com', 'xml.etree.ElementTree', 'sqlite3',
                          'pywinauto', 'pywinauto.application', 'pywinauto.controls', 'pywinauto.tests', 'SendKeys'
                            ],
                          "typelibs": [('{C866CA3A-32F7-11D2-9602-00C04F8EE628}', 0, 5, 0)]
                         }
              }
    )

And here's the ouput when running the exe

Traceback (most recent call last):
  File "mainForm.py", line 129, in changeState
  File "mainForm.py", line 230, in setWriteMode
  File "mainForm.py", line 105, in FillApps
  File "WindowHandler.pyo", line 26, in getWindowList
NameError: global name 'pywinauto' is not defined

I hope anyone could point me into the right direct.

Thanks in advance

+1  A: 

From my experience, py2exe handles imports in a weird way. Sometimes it has trouble finding linked-imports (like you import WindowHandler, which imports pywinauto).

I would start with this in mainForm.py:

import sys
import WordOps 
import Voice 
import WindowHandler
from PyQt import QtCore, QtGui, uic

And in setup.py, start with this:

options={'py2exe':{
                    'includes': ['sip'],
                    'bundle_files': 1
                  }
        }

Make sure your program works before compiling to an exe, then try running setup.py. When you start getting errors when running setup.py (like the one you posted), add more imports to mainForm.py. So for that error, your new header will look like:

import sys
import WordOps 
import Voice 
import WindowHandler
from PyQt import QtCore, QtGui, uic
# imports for py2exe
import pywinauto

It will not break your code because it will just be an "unused" import. Keep doing that until setup.py works.

jcoon
I changed the imports and the options in the setup file, but now I get an error telling me that pywinauto\controls\win32_controls.pyc can't find win32functions. This lib is on pywinauto root folder. Now I'm importing it like this 'from pywinauto import win32functions'. Still not working.Adding pywinauto to includes in setup has no effect.Also py2exe generates a corrupted zip file when using "'bundle_files': 1"
masterLoki
You can remove bundle_files; try adding pywinauto to `packages` (another py2exe option) instead of `includes`. Also try adding pywinauto's dependencies too: `ctypes`, `SendKeys`, `PIL` and `elementtree`
jcoon
Adding <code>packages:['pywinauto']</code> did the trick. Thanks A LOT for your help :)
masterLoki