4

How I could execute a python script from within the VB.NET code?

I would like to use VB.NET for building a GUI for my python script. This is to be able to create a GUI for the user of a script.

I found this example:

Shell(c:\Program Files\python 266\python.exe path\myPythonScript.py +
       " " + arg1 + " " + argN)

But I would like to know is there a more suitable way of executing python script?

I would also like to understand how I could handle errors in such setup.

Your help much appreciated!

Thanks,

meryloo

4 Answers 4

3

In VB you can also use the command:

 shell( "d:\path\to\your\script.py arg1 arg2" )
Sign up to request clarification or add additional context in comments.

1 Comment

I was wondering in the python how would you manage the args ?
3

You could call python from within .NET but if the only reason you want to do that is because you want to design a GUI fro your Python, why not keep things 'cleaner' and do it all in Python? You can use TKInter or wxPython to name but two GUI APIs. The latter also has wxDesign which can lighten the load of GUI design.

This list gives you some more options for GUI design in Python other than the two I've listed.

On the other hand, you don't say what you are developing that needs python, but if it is either for ArcGIS or GDAL (probably the two most common GIS scripting environments) both of those have .NET APIs as well as Python. So, you could potentially do it ALL in .NET.

Comments

2

This is how I did it in C# .Net:

    protected override void OnClick()
    {
        System.Diagnostics.Process.Start("C:\\path\\to\\pyscript\\MyScriptHere.py");
    }

Comments

1

In my case, I wanted the Python script to create a csv file, then the VB macro to gather info from that csv file. This code worked when I added a delay to the macro:

Shell ("C:\Python27\ArcGIS10.1\python.exe myPath\PythonScript.py arg1 arg2")
FourSecDly

The delay allowed the shell process to complete before running the macro. The code for the delay was:

Public Function FourSecDly()
y = Timer + 4#
Do While Timer < y
DoEvents
Loop
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.