Running Python script from vb.net by passing VB varialbes as Python parameters

2

I'm trying to run a python script by clicking a button in visual basic. I need to pass variables from visual basic to python.

try the following:

Shell("path\programa.py arg1 arg2")

But in doing it I get the error

  

System.IO.FileNotFoundException: 'File not found.'

also try:

cd path & python progama.py

This command works from the windows console but not from visual basic

    
asked by Manuel 21.09.2017 в 06:22
source

1 answer

2

To execute a program in python you must first call the python executable and pass it as an argument the program you want to run, so in your case it would be something like this:

Shell("python path\programa.py arg1 arg2")

For this to work, the python executable must be in the environment variable PATH so that the system can find it.

Anyway, instead of Shell I would recommend using Process which is more powerful and flexible. In your case, it would be something like this:

Dim ejecutable As String = "pyton.exe" //aqui puedes poner la ruta a python.exe si no está 
                                       //en la variable de entorno PATH
Dim psi As New ProcessStartInfo(ejecutable) 
psi.WorkingDirectory = IO.Path.GetDirectoryName(path) //Cambias el directorio activo a tu path
psi.Arguments ="programa.py arg1 arg2"

Process.Start(psi)
    
answered by 21.09.2017 в 09:10