Call external python/CMD from within Grasshopper

Hi,

sorry if this has been asked before – but I could find any working solution.

Is it possible to call a external script, (python or .bat) form within Grasshopper? Ideally while passing arguments and output of the script.

Mock-up:

thanks!

Hello,

You should be able to use subprocess.run in a Python component to do this…

With Python this could involve saving your inputs as a text file or building a command line interface, eg using argparse or a CPython package such as click .

thanks graham, i will try;

also I found “Dodo: run executable”; but when I input the path to my python directory and the path to the python script as an argument nothing happens.

Hi Graham,

I tried

import subprocess

path_programm = "C:\\Users\\charlieNagamoto\\Documents\\test\\emptyFile.bat"
subprocess.call([path_programm])

inside a GHpython node, sadly the bat file isn’t executed
Could you help?

what also could work is the following inside a vb node:

Dim path As String
path = (System.Environment.GetFolderPath(System.Environment.SpecialFolder.System)) & "\cmd.exe"
Print(path)
Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(path)
proc.WaitForExit()
proc.Close()

it opens the CMD but I can not figure out how to pass arguments to it. Does anyone know?

Search for subprocess.Popen. You can use stdin to pass the arguments and use stdout to get the output. You will see in the documentation how to redirect the stdout to PIPE.

2 Likes

Thanks Mostapha, This helps! I think I am on the right track

1 Like

if anyone in the future is searching for this:
Here is what I came up with; use at own risk;


run_external_script.gh (4.1 KB)

4 Likes

This worked fine. The only problem was that I had to give the absolute path to python.exe.

Hi , where did you indicated the full python path? I need to run a python file but within a specific environment.

To call an external command in a Python script, use any of the following methods:

  • subprocess.run() //Python 3.5+ only.

You can get the stdout, stderr, the “real” status code, better error handling, etc…

  • os.system()

It passes the command and arguments to your system’s shell.

  • subprocess.call() function
  • subprocess.Popen Class
  • os.popen() function

You could also try Hops.

hello, I was trying this way to run an external script and it doesn’t work for me, does it work for you?

hi, it does. maybe you have to specify a absolute path to your python exe…