I've been using py2exe for a while, but I keep running into some problem
importing some modules.
I have a program that uses the "pywinusb" library. The pywinusb library is
in the same folder as the program file that's being compiled.
main.py
import pywinusb
...
And pywinusb uses ctypes:
import ctypes
The program runs fine if I just run it in python. But when I use py2exe, it
gives the error:
Traceback (most recent call last):
File "my_file.py", line 34, in <module>
zipfile=None,
File "C:\Program Files\Python26\Lib\distutils\core.py", line 152, in
setup
dist.run_commands()
File "C:\Program Files\Python26\Lib\distutils\dist.py", line 975, in
run_commands
self.run_command(cmd)
File "C:\Program Files\Python26\Lib\distutils\dist.py", line 995, in
run_command
cmd_obj.run()
File
"C:\VirtualEnvironment\lib\site-packages\py2exe-0.6.9-py2.6-win32.egg\py2exe\build_exe.py",
line 243, in run
self._run()
File
"C:\VirtualEnvironment\lib\site-packages\py2exe-0.6.9-py2.6-win32.egg\py2exe\build_exe.py",
line 296, in _run
self.find_needed_modules(mf, required_files, required_modules)
File
"C:\VirtualEnvironment\lib\site-packages\py2exe-0.6.9-py2.6-win32.egg\py2exe\build_exe.py",
line 1297, in find_needed_modules
mf.import_hook(mod)
File
"C:\VirtualEnvironment\lib\site-packages\py2exe-0.6.9-py2.6-win32.egg\py2exe\mf.py",
line 757, in import_hook
return Base.import_hook(self,name,caller,fromlist,level)
File
"C:\VirtualEnvironment\lib\site-packages\py2exe-0.6.9-py2.6-win32.egg\py2exe\mf.py",
line 141, in import_hook
q, tail = self.find_head_package(parent, name)
File
"C:\VirtualEnvironment\lib\site-packages\py2exe-0.6.9-py2.6-win32.egg\py2exe\mf.py",
line 218, in find_head_package
raise ImportError, "No module named " + qname
ImportError: No module named ctypes
The line numbers may be off, as I've added a few debug statements. I can
determine that the error happens because "ctypes" is in mf.py ModuleFinder's
badmodules list. From what I can tell, the order is this:
-py2exe finds the pywinusb.hid module, and finds all the import statements.
-It sees "import ctypes", and calls ModuleFinder.find_head_package() with
parent==pywinusb.hid and name=="ctypes".
-find_head_package() then calls import_module() with partname == "ctypes",
fqname=="pywinusb.hid.ctypes", and parent==pywinusb.hid.
-import_module() then calls find_module(), which then calls
imp.find_module() with name=="cypes" and
path=="C:\path\to\project\pywinusb\hid", which fails since ctypes is not in
pywinusb's folder.
At this point, since imp.find_module() has failed, the "ctypes" module is
added to the badmodules list and therefore fails the entire py2exe build
process.
It seems that if a given module is not found in the folder of the
parent/importing module, then py2exe should attempt to find it using
imp.find_module(name="ctype", path=None) as a secondary. Otherwise, seems
like there's no way to properly find any modules imported from any child
module.
I've tried adding include=["ctypes"] in the setup file, but that doesn't
work - ctypes gets put in the badmodules list, and therefore fails no matter
what.
Is there something I've overlooked? Is this just a bug with how ctypes
attempts to find modules?
Thanks!
|