I created a python script that uses pythonnet. The script is in a file named main.py
. When I run the script from the command line (simply typing main.py
at the Windows command prompt), the imported pythonnet module clr
works fine. But when I try to build an exe I get an error saying: No module named clr
.
To isolate the cause of this, I have verified that building an executable (in my case a simple Tkinter app) using py2exe works. I only have Python 3.4 installed and have verified that where python
points to C:\Python34\python.exe
.
The error occurs at executable build time and seems to be triggered by including clr
in the section {"includes":["sip","clr"]}}
in my setup.py
for py2exe
. The full traceback is below:
Traceback (most recent call last):
File "setup.py", line 32, in <module>
windows = [{'script': "main.py"}],
File "C:\Python34\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python34\lib\distutils\dist.py", line 917, in run_commands
self.run_command(cmd)
File "C:\Python34\lib\distutils\dist.py", line 936, in run_command
cmd_obj.run()
File "C:\Python34\lib\site-packages\py2exe\distutils_buildexe.py", line 188, i
n run
self._run()
File "C:\Python34\lib\site-packages\py2exe\distutils_buildexe.py", line 267, i
n _run
builder.analyze()
File "C:\Python34\lib\site-packages\py2exe\runtime.py", line 164, in analyze
mf.import_hook(modname)
File "C:\Python34\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
module = self._gcd_import(name)
File "C:\Python34\lib\site-packages\py2exe\mf3.py", line 273, in _gcd_import
raise ImportError('No module named {!r}'.format(name), name=name)
ImportError: No module named 'clr'
I also read/tried these:
https://docs.python.org/2/distutils/setupscript.html
https://pythonhosted.org/setuptools/setuptools.html
http://sourceforge.net/p/py2exe/mailman/message/6937658
leading me to move clr.pyd
and Python.Runtime.dll
into various locations including the location of main.py
, C:\Python34\Lib\site-packages
(where they were originally) and C:\Python34\Lib\site-packages\py2exe
None of these have worked and I don't know what to try next. I can see that for some reason py2exe
can't find either clr.pyd
or Python.Runtime.dll
or both, but can't see why. Does anyone have any ideas?
Code details
My main.py
script looks like this:
import clr
clr.AddReference("name.xxxx")
from name.xxxx import aaa
from clr import System
# All my functioning code, that I've verified works when run from the command line
This is what my setup.py
file contains (I've left some bits commented so you can see what I've tried):
from distutils.core import setup
import py2exe, sys, os
mydata_files = []
for files in os.listdir('C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL'):
f1 = 'C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL\\' + files
if os.path.isfile(f1): # skip directories
f2 = '.', [f1]
mydata_files.append(f2)
setup(
data_files=mydata_files,
# options = {"py2exe" : {"includes" : "module1,module2,module3"}}
options = {"py2exe": {"includes":["sip", "clr"]}},
# options = {'py2exe': {'bundle_files': 1 , 'compressed': True,"includes":["sip"]}},
#python setup.py py2exe
#CLR.dll and PythonRuntime.dll
# options = {'py2exe': {'bundle_files': 1, "skip_archive":1 ,"includes":["sip"]}},
windows = [{'script': "main.py"}],
# data_files=mydata_files,
# zipfile = None
)
If I change the line options = {"py2exe": {"includes":["sip", "clr"]}},
to options = {"py2exe": {"includes":["sip"]}},
then the .exe
builds, but obviously does not function correctly.