0

I am trying to open a py script via VBA Excel.

The script is to interact with engineering software which undertakes finite elements analysis. The script can run directly from the py editor.

They prepared a Python library I have to import at the beginning of the script which also requires some password and log in credentials for the script to interact with that specific software.

I can open simple py scripts (such as the classic Hello World) via the Shell in VBA.
The script that I have prepared is more complex. When opening my script via VBA using the shell it flashes the cmd window and nothing happens.

Below is the subroutine I have coded in VBA to open the Python interpreter and Python script, for illustration: it returns error message 2.

Sub RunPythonScript()
    Dim wsh As Object
    Dim PythonExe, PythoScript As String
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    Dim errorCode As Long
    Dim pth As String
        
    PythonExe = """C:\ProgramData\Bentley\Geotechnical\PLAXIS 2D CONNECT Edition V20\python\python.exe"""
    PythoScript = """C:\Users\ukjfv001\Desktop\MyPython\MyAnalysis.py"""
    pth = PythonExe & PythoScript
        
    Set wsh = VBA.CreateObject("WScript.Shell")
    
    errorCode = wsh.Run(pth, windowStyle, waitOnReturn)
    
    If errorCode = 0 Then
        MsgBox "Done! No error to report."
    Else
        MsgBox "Program exited with error code " & errorCode & "."
    End If
        
End Sub

There is a lot of information online on how to run Python scripts.
In this forum: How to call python script on excel vba?. I have also created a bat file which I could open via VBA but, got the following error message:

C:\Users\ukjfv001\Desktop\MyPython>"C:\Users\ukjfv001\Anaconda3\python.exe" ""C:\Users\ukjfv001\Desktop\MyPython\MyAnalysis.py"" start Traceback (most recent call last): File "C:\Users\ukjfv001\Desktop\MyPython\MyAnalysis.py", line 16, in from plxscripting.easy import * #call Plaxis scritping library ModuleNotFoundError: No module named 'plxscripting'

Below is a bit of the Python code which I have to place at the top of the script in the SciTE editor (which comes with the engineering software).
From what I can see in the error message (above) and the bit of code below from plxscripting.easy import there is something I am not doing.
I am new to Python so usually I need practical examples to understand what is to be done.

from plxscripting.easy import * #callS engineering scritping library
inputport = 8888888
plaxispw = some_password
plaxis_path = C:\Users\ukjfv001\... #to here the software is intalled
plaxis_input = Plaxis.exe #software executable

if not process_exists(plaxis_input): #checkS if software is alreayd running
    # first launch software
    args = [os.path.join(plaxis_path, plaxis_input),"--AppServerPort={}".format(inputport),"--AppServerPassWord={}".format(plaxispw)]
    inputprocess = subprocess.Popen(args)

# Initialize new_server with waiting time
s_i, g_i = new_server('localhost', inputport, password=plaxispw, timeout=10.0)
s_i.new()#starts a new Project 

#after this point is where I have my Python script...```
Community
  • 1
  • 1
  • This is a tricky one. My first guess would be that Python is running from a different locations in both your script and when you call it manually. Try putting a ‘import sys’ and ‘print(sys.path)’ at the top of you Python script. – AlexNe Oct 10 '20 at 08:50
  • 1
    Then check if the [modules path](https://stackoverflow.com/questions/247770/how-to-retrieve-a-modules-path) is in ‘sys.path’. – AlexNe Oct 10 '20 at 08:54
  • I don't know the answer but a good way to test is to take the pth string that you are passing to wsh.run and put it directly into your command prompt to see if it runs it/ errors you get. – Josh Pachner Oct 10 '20 at 14:27

1 Answers1

0

I had a similar problem where I got an error running a python script from a scheduler (via running a file) even though it ran in my IDE. I also had to run it from a batch file to see the error, otherwise nothing happened. The issue was that python could not find the path to my custom libraries (which may be your issue, since the error is a library import). I think this can happen if you set your Python Path to custom libraries in your IDE and the environmental variables/ path are not also set (that is what you need to synchronize).

In Spyder, you just go to Tools: Python Path Manager in the menu and hit synchronize. I don't know what IDE you are using, but probably it also has a Python Path Manager if it isn't Spyder.