这段代码从VBA启动python脚本,在我的旧电脑上运行良好
Sub py_launcher_8()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Long
errorCode = wsh.Run("C:/Users/belose/PycharmProjects/week2/venv/Scripts/python.exe" & " " & "C:\Users\belose\PycharmProjects\week2\VGM\VGM_eval.py", windowStyle, waitOnReturn)
If errorCode = 0 Then
MsgBox "Done! No error to report."
Else
MsgBox "Program exited with error code " & errorCode & "."
End If
End Sub
然后我不得不转移到另一台PC上,python位于anaconda环境中:C:\Users\belose\AppData\Local\Continuum\anaconda3\python.exe
Sub py_launcher_8()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Long
errorCode = wsh.Run("C:\Users\belose\AppData\Local\Continuum\anaconda3\python.exe" & " " & "C:\Users\belose\PycharmProjects\DRM\venv\VGM\VGM_eval.py", windowStyle, waitOnReturn)
If errorCode = 0 Then
MsgBox "Done! No error to report."
Else
MsgBox "Program exited with error code " & errorCode & "."
End If
End Sub
VBA现在不会启动python脚本并返回错误代码1
python脚本本身是正常的
原因可能是什么
我建议您使用
print ("Hello World..!!")
编写一个test.py
脚本,并运行它来检查问题是否是由于python.exe
的路径或python script
的执行造成的如果它正在运行,那么应该检查python脚本,如果没有,那么应该验证
python.exe
路径要定位python路径,可以使用以下步骤:
Anaconda Prompt
李>activate environment-name
李>where python
李>相关问题 更多 >
编程相关推荐