Hops and running python file automatically

Hello
I use bat file to run python file related to Hops component, and i try this code to run the bat file from Ghpython but didn’t work.

  • How we can run bat from Ghpython?
  • Can we see this as option in hops component to run the python file automatically when use hops?
file ="....run.bat"

import os
os.system(file)

Probably using the subprocess module.

I tried it but always got error about win32

I think this is the problem, i tried to solve it by adding path of python 3.9 but don’t work

Solved:
I add the full path of python and the file in the bat file:

C:\Python39\python.exe C:\Users\user\excel_gh.py

But the problem i can’t use grasshopper when cmd window is open

I don’t understand how hops is involved. Are you trying to run the batch file in a remote component?

You can use System.Diagnotics.Process to launch applications from ghpython.

Thanks @stevebaer , i just noticed that hops component work without need to open python file and run it.

I try again, Hops sever don’t start automatically, this feature still needed, i don’t know why it work before!
* Can we see this as option in hops component to run the python file automatically when use hops?

I use this code and it work fine

import subprocess

p = subprocess.Popen(r'start cmd /c '+file, shell=True)

p.wait()

Is there a way to hide the console window?

With shell=False?

No with shell=False the process don’t run

Hi,

it’s a diversion from the topic but you seem to be using multiple python projects with dependancies and without virtual environments : I definitely recommend taking the time to learn virtual environments if that is the case - the main advantages are

  • if you have an unusual dependency in one project it won’t ‘pollute’ all your projects
  • If you have complex dependencies like Scipy or Flask then you can upgrade them in one project without worrying that the upgrade will break other projects
  • If you completely break your environment for one project you can delete it and start again
1 Like

Thank you , i will check it