Creating *.exe files of Python Scripts which import Cantera

708 views
Skip to first unread message

Alan Thomason

unread,
Jun 30, 2015, 9:26:08 PM6/30/15
to canter...@googlegroups.com
Hello All...

This is also posted in Stack Overflow.  It occurred to me that this might be a better place to post the question. 

I am trying to create a *.exe file of a Python script which imports Cantera. So far I have been unsuccessful. I have narrowed down the problem to the import cantera line in the attached script. If I comment it out, the classic Hello World program works and vice versa.

Has anyone successfully done this? I do not see anything noteworthy in the data that comes over on the console.


#import cantera as ct

print ("Hello World From Alan's First Python Script")
input("Press Enter to continue...")

C:\Python34\Alan\Combustion\CreatingPlot>py setup.py py2exe
running py2exe

  3 missing Modules
  ------------------
? readline                            imported from cmd, code, pdb
? win32api                            imported from platform
? win32con                            imported from platform
Building 'dist\HelloWorld.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy C:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy C:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy C:\Python34\DLLs\select.pyd to dist\select.pyd
Copy C:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy C:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy C:\Python34\lib\site-packages\cantera\_cantera.pyd to dist\cantera._cantera
.pyd
Copy C:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy C:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy C:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy C:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd

C:\Python34\Alan\Combustion\CreatingPlot>py setup.py py2exe
running py2exe

  3 missing Modules
  ------------------
? readline                            imported from cmd, code, pdb
? win32api                            imported from platform
? win32con                            imported from platform
Building 'dist\HelloWorld.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy C:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy C:\Python34\DLLs\select.pyd to dist\select.pyd
Copy C:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy C:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy C:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy C:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy C:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy C:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy C:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd

C:\Python34\Alan\Combustion\CreatingPlot>

The first group is with the import Cantera active, the second is without. You can see the cantera package being referenced, but no extra problems.


An *.exe is created, but when I run it the console pops up for a second then disappears.


Thanks very much, Alan

Ray Speth

unread,
Jun 30, 2015, 11:39:26 PM6/30/15
to canter...@googlegroups.com
Alan,

You might want to try running the .exe file from a command prompt, so that any output (e.g. an error message) can actually be read, rather than vanishing with the console popup.

Regards,
Ray

Alan Thomason

unread,
Jul 1, 2015, 4:29:57 PM7/1/15
to canter...@googlegroups.com
Hi Ray...

That was a super idea.  Now I have more data to work with...thanks!

No module named 'interrupts' looks like a good lead.

C:\Python34\Alan\Combustion\CreatingPlot\dist>HelloWorld
Traceback (most recent call last):
  File "HelloWorld.py", line 4, in <module>
  File "C:\Python34\lib\site-packages\cantera\__init__.py", line 1, in <module>
    from ._cantera import *
  File "<loader>", line 10, in <module>
  File "<loader>", line 8, in __load
ImportError: (No module named 'interrupts') 'C:\\Python34\\Alan\\Combustion\\Cre
atingPlot\\dist\\cantera._cantera.pyd'
C:\Python34\Alan\Combustion\CreatingPlot\dist>

Ray Speth

unread,
Jul 1, 2015, 5:29:40 PM7/1/15
to canter...@googlegroups.com
Alan,

There is a submodule named 'interrupts' within Cantera. I think the issue may be that py2exe is failing to detect that your script implicitly depends on this module, since it is only imported from inside the compiled extension module. If there is a way to explicitly tell py2exe to include the module 'cantera.interrupts', that may resolve the issue, or you can try adding 'import cantera.interrupts' to your top-level script.

Regards,
Ray

Alan Thomason

unread,
Jul 1, 2015, 11:52:00 PM7/1/15
to canter...@googlegroups.com
Hi Ray et al...

There is a file called 'interrupts.py' in the

C:\Python34\Lib\site-packages\cantera

Now I am investigating what this means...is it just that py2exe is looking in the wrong directory for this file?

Alan Thomason

unread,
Jul 1, 2015, 11:53:59 PM7/1/15
to canter...@googlegroups.com
Super, will try that.  My recent reply was slowly in the works and I didn't see your suggestion below til just now, so sorry if it looks redundant.

Alan Thomason

unread,
Jul 2, 2015, 1:42:21 PM7/2/15
to canter...@googlegroups.com
Hi Ray...

That was very helpful.  I think I mentioned the gorilla defusing the nuclear bomb scenario before.  Fortunately this gorilla has some real nuclear scientists like you helping him. 

So I can now import Cantera by adding the first two lines...

import sys
sys.path.append('C:\Python34\Lib\site-packages\cantera')
import numpy as np
import matplotlib as mpl
import interrupts
import cantera as ct
print ("Hello World...Cantera now imports!")

input("Press Enter to continue...")

I barely grasp what is happening, but I am on to the next hurdle. 

If anyone has any advice on what the errors mean in the attached file (excerpted below) I'd love to understand this better...

Error: Namespace packages not yet supported: Skipping package 'mpl_toolkits'
  25 missing Modules
  ------------------
? IPython                             imported from matplotlib.backends.backend_
nbagg
? PIL                                 imported from matplotlib.backend_bases, ma
tplotlib.backends.backend_agg, matplotlib.image
? PyQt4                               imported from matplotlib.pyplot
? PyQt5                               imported from matplotlib.pyplot
? UserDict                            imported from pytz.lazy
? backports                           imported from setuptools.ssl_support
? gobject                             imported from matplotlib.pyplot
? interrupts                          imported from __SCRIPT__
? mpl_toolkits                        imported from matplotlib.mlab
? netbios                             imported from uuid
? packaging                           imported from pkg_resources

? readline                            imported from cmd, code, pdb
? sets                                imported from matplotlib.backends.backend_
pdf, matplotlib.font_manager, matplotlib.mathtext, matplotlib.transforms, numpy.
distutils.command.build_ext, numpy.distutils.fcompiler, numpy.distutils.misc_uti
l, pytz.tzinfo
? setuptools_svn                      imported from setuptools.command.egg_info
? tornado                             imported from matplotlib.backends.backend_
nbagg, matplotlib.backends.backend_webagg
? urllib.pathname2url                 imported from setuptools.compat
? urllib.quote                        imported from matplotlib.textpath
? urllib.splittag                     imported from setuptools.compat
? urllib.url2pathname                 imported from setuptools.compat
? win32api                            imported from distutils.msvccompiler, plat
form
? win32com                            imported from setuptools.sandbox
? win32con                            imported from distutils.msvccompiler, plat
form
? win32pdh                            imported from numpy.testing.utils
? win32wnet                           imported from uuid
? wincertstore                        imported from setuptools.ssl_support
ErrorMessages.txt

Ray Speth

unread,
Jul 2, 2015, 8:00:36 PM7/2/15
to canter...@googlegroups.com
Hi Alan,

I think adding the site-packages\cantera directory to your path is probably not the solution you want, since it will leave your executable dependent on the installed copy of Python and the Cantera Python module, instead of ending up with a portable executable which is presumably what you're after.

Does simply adding "import cantera.interrupts" instead work? This should tell py2exe to include that submodule.

As for Matplotlib, you might want to check http://www.py2exe.org/index.cgi/MatPlotLib and if that doesn't answer your questions you can try the matplotlib-users mailing list.

Regards,
Ray

Alan Thomason

unread,
Jul 3, 2015, 1:53:05 PM7/3/15
to canter...@googlegroups.com
Hi Ray...

Brilliant...I tried that and it worked.  I also did check that in fact just as you thought the initial solution that I thought I had was not portable, whereas your solution is.  Thanks again! 
Message has been deleted

Alan Thomason

unread,
Jul 3, 2015, 4:03:05 PM7/3/15
to canter...@googlegroups.com
And now I've tried the link you supplied and was able to use the advice there to end up with an executable directory. The only exception was that I had to create a directory and put two files into it: ...

Eric Burdette

unread,
Oct 22, 2015, 1:36:50 PM10/22/15
to Cantera Users' Group
Alan,

Did you create any Cantera solutions in your executable?  When I try to run the resulting executables with any kind of solution, the command window will spawn processes until I close or interrupt it.  A short example:

import cantera as ct
import cantera.interrupts
print('hello world')
gas=ct.Solution('gri30.cti')

for building: use normal setup.py parameters, and drop gri30.cti into the dist folder

I'm running python 3.4 and Cantera 2.2.0  on windows 7.

I also tried pyinstaller, and found the same problem present.

Thanks,
Eric
Message has been deleted

Xiang Gao

unread,
Mar 9, 2016, 9:13:44 AM3/9/16
to Cantera Users' Group
Hi Eric, 

I got the same issue (exe stuck at the line of cantera.Solution). And the task manager shows that the exe is keep generating itself (if the process of my exe is hello, then there will be 20 hello processes going on)

Did you get any way to fix it?

P.S. everything is fine on Mac (I used py2app to convert .py to executable program on Mac)

Xiang
Reply all
Reply to author
Forward
0 new messages