我在从Excel VBA运行一个Python脚本时遇到了麻烦。这是VBA的代码。
Sub RunPython()
Dim objShell As Object
Dim PythonExe, PythonScript As String
Set objShell = VBA.CreateObject("Wscript.Shell")
PythonExe = """C:\Users\carlo\AppData\Local\Microsoft\WindowsApps\python3.exe"""
PythonScript = """C:\Users\carlo\Desktop\MSc_Thesis\ML applications\BlackBox\BlackBoxAbsorbers.py"""
objShell.Run PythonExe & PythonScript
End Sub
The python code is the following.
print("hello")
input("hello, press enter")
当我运行VBA时,Python执行器打开了,但它马上就关闭了,没有实际执行脚本。换句话说,"hello"
和"hello, press enter"
都没有出现在python执行器的窗口中,它突然关闭了。
我确信 python.exe 和 python 脚本的路径是正确的。我在""""
和""
中都试过了,在这两种情况下都不能工作。很明显(这很简单!)如果我直接从Python执行器中运行,Python脚本就能工作。