【Batch Job】Batch Job中执行一段Python代码,遇见Failure Exit Code
问题描述
在Azure Batch Job中执行一段Python代码,遇见Failure Exit Code。
print("start mian.py") if __name__ =="__main__": print("__main__")
就算只使用一个简单的 print 方法,也是出现Failure Exit Code错误。
进一步在Batch Job的 stderr 日志中,发现了错误消息:
Traceback (most recent call last): File "site-packages\PyInstaller\loader\rthooks\pyi_rth_win32comgenpy.py", line 49, in <module> File "c:\hostedtoolcache\windows\python\3.7.9\x64\lib\site-packages\PyInstalle r\loader\pyimod03_importers.py", line 623, in exec_module File "site-packages\win32com\__init__.py", line 8, in <module> File "c:\hostedtoolcache\windows\python\3.7.9\x64\lib\site-packages\PyInstalle r\loader\pyimod03_importers.py", line 623, in exec_module File "site-packages\pythoncom.py", line 2, in <module> File "c:\hostedtoolcache\windows\python\3.7.9\x64\lib\site-packages\PyInstalle r\loader\pyimod03_importers.py", line 623, in exec_module File "site-packages\win32\lib\pywintypes.py", line 126, in <module> File "site-packages\win32\lib\pywintypes.py", line 116, in __import_pywin32_sy stem_module__ ImportError: DLL load failed: The specified module could not be found. [2504] Failed to execute script pyi_rth_win32comgenpy
问题解答
首先,出现 Failure Exit Code 表示Batch Job已经开始执行,但是在运行脚本或者执行文件中,出现了异常导致Job退出 Exit with 1 (正常的退出code 为 Exit code 0)。而本次错误中的异常为 “ DLL load failed: The specified module could not be found.”。缺少Python代码的可执行环境。
因为Batch Job中执行的文件是通过PyInstaller把python code打包为一个main.exe可执行文件,这样在没有预先安装Python runtime的环境中,也可以执行Python代码。所以需要研究 PyInstaller 在打包时,是否没有包含Python Runtime文件。
参考资料
ImportError: DLL load failed while importing win32api: The specified procedure could not be found. [25960] Failed to execute script pyi_rth_win32comgenpy : https://github.com/mhammond/pywin32/issues/1791
python - Failed to execute script pyi_rth_win32comgenpy after packing with pyinstaller : https://stackoverflow.com/questions/65587443/failed-to-execute-script-pyi-rth-win32comgenpy-after-packing-with-pyinstaller
added
--hidden-import "pywin32"
to the pyinstaller command, and it worked!
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2023-04-07 【Azure Redis 缓存】对于Azure Redis 从 Redis 4 升级到 Redis 6 的一些疑问
2021-04-07 【Azure Developer】AAD API如何获取用户“Block sign in”信息(accountEnabled)
2021-04-07 【Azure 事件中心】EPH (EventProcessorHost) 消费端观察到多次Shutdown,LeaseLost的error信息,这是什么情况呢?
2021-04-07 【Azure Redis 缓存】Redis连接无法建立问题的排查(注:Azure Redis集成在VNET中)