I'm trying to make an .exe file of a python script using the cantera library with help of PyInstaller module.
我正在尝试使用cantera库在PyInstaller模块的帮助下创建一个.exe文件。
I'm working with python 3.9 and Pyinstaller 5.13.1.
我正在使用的是Python3.9和Pyinstaller 5.13.1。
My Script is very simple :
我的脚本非常简单:
import numpy as np
import cantera as ct
print(np.zeros(2))
S = ct.Solution('gri30.cti')
I'm working in virtual environnement with all packages necessary installed.
When I execute my script with python in venv everything is ok, the script work well.
我在虚拟环境中工作,安装了所有必要的程序包。当我在venv中使用python执行我的脚本时,一切正常,脚本工作得很好。
I can generate the .exe file with this command line : pyinstaller script.py --collect-all cantera
我可以使用以下命令行生成.exe文件:pyinstaller script.py--Collect-all cantera
The .exe is correctly created is dist folder.
已正确创建dist文件夹中的.exe。
When I execute the .exe in the virtual env everything is ok, but when I try to run it in arbitrary folder I have this log :
当我在虚拟环境中执行.exe时,一切正常,但是当我试图在任意文件夹中运行它时,我有这个日志:
[0. 0.]
Traceback (most recent call last):
File "\\t-vgate-3\\dev1\tst.py", line 7, in <module>
S=ct.Solution("C:\\Users\\Downloads\\gri30.cti")
File "build\python\cantera\base.pyx", line 71, in cantera._cantera._SolutionBase.__cinit__
File "build\python\cantera\base.pyx", line 137, in cantera._cantera._SolutionBase._cinit
File "build\python\cantera\base.pyx", line 256, in cantera._cantera._SolutionBase._init_cti_xml
cantera._cantera.CanteraError:
*******************************************************************************
CanteraError thrown by call_ctml_writer:
Error converting input file "C:\Users\Downloads\gri30.cti" to CTML.
Python command was: 'python'
The exit code was: 1
-------------- start of converter log --------------
sys.path: ['', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "cantera\__init__.py", line 4, in <module>
from ._cantera import *
ImportError: No module named _cantera
--------------- end of converter log ---------------
*******************************************************************************
[14960] Failed to execute script 'test' due to unhandled exception!
In this case, numpy seems to work well but not cantera.
在这种情况下,NumPy似乎效果很好,但Cantera效果不佳。
I think that Cantera doesn't use the python interpreter of the .exe file.
我认为Cantera没有使用.exe文件的python解释器。
Somebody have an idea to solve this issue ? Or maybe some test to do ?
有没有人有办法解决这个问题?或者是要做一些测试?
Thanks a lot for your help.
非常感谢你的帮助。
更多回答
Cantera is working, but it cannot read such file. It is possible that it needs some external library (DLL). Find out which library it requires (there should be in the documents), and then you should add it as required file in pyinstaller. It is a bit messy using pyinstaller on libraries which requires extra files (and without receipts in pycharm). It is a lot of test and error (and reading documentation and sources)
Cantera正在工作,但它无法读取这样的文件。它可能需要一些外部库(DLL)。找出它需要哪个库(文档中应该有),然后您应该将其作为所需的文件添加到pyinstaller中。在库上使用pyinstaller有点麻烦,因为它需要额外的文件(并且在pycharm中没有回执)。这是大量的测试和错误(以及阅读文档和源代码)
I think I have found the problem source. Cantera library try to use C program that convert cti file to ctml file. This C program call a python script with executable wich is give by "PYTHON_CMD" environment variable. The problem is that the python executable is wrapped in the executable when running this program in c. I tried to pass venv python executable path in PYTHON_CMD before execute pyinstaller but it doesn't work. Thanks for helping.
我想我已经找到了问题的根源。Cantera库尝试使用C程序将CTI文件转换为CTML文件。这个C程序调用一个由“PYTHON_CMD”环境变量提供的带有可执行文件的PYTHON脚本。问题是,当在c中运行这个程序时,python可执行文件被包装在可执行文件中。在执行pyinstaller之前,我试图在PYTHON_CMD中传递venv python可执行文件路径,但它不起作用。谢谢你的帮助。
You need to make sure that pyinstaller is also packaging the _cantera.dll
file. In addition, the CTI format is deprecated and removed in the most recent version. If you use a YAML input file, you will not require Python to load the file.
您需要确保pyinstaller也打包了_cantera.dll文件。此外,最新版本中已弃用并删除了CTI格式。如果使用YAML输入文件,则不需要使用Python来加载该文件。
Your tags include python-3.x
but the path in the error is for Python 2.7. Which is it? What versions of Cantera and Python are you trying to use?
您的标记包括python-3.x,但错误中的路径是针对Python2.7的。到底是哪一个?您正在尝试使用哪些版本的Cantera和Python?
我是一名优秀的程序员,十分优秀!