VBA无法在新电脑上运行python脚本

2024-05-22 09:36:48 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码从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脚本本身是正常的

原因可能是什么


Tags: pyifaserrorvbausersdimsub
1条回答
网友
1楼 · 发布于 2024-05-22 09:36:48

我建议您使用print ("Hello World..!!")编写一个test.py脚本,并运行它来检查问题是否是由于python.exe的路径或python script的执行造成的

如果它正在运行,那么应该检查python脚本,如果没有,那么应该验证python.exe路径

要定位python路径,可以使用以下步骤:

  1. 从开始菜单打开Anaconda Prompt
  2. 如果您想要conda环境中Python解释器的位置,而不是 根conda环境,运行activate environment-name
  3. 运行where python

相关问题 更多 >