Sitemap

How to use iron python to call python functions in VB:

1 min readOct 6, 2023

--

You should follow these steps:

· First you need to download Iron Python from https://ironpython.net/download/

· Then in visual studio in your solutions directory you need to add the iron python dll’s (from the location you places them in the previous steps) as references.

. Now you can call your python functions from your python scripts in your VB code as follows:

Example of driver VB code:

Get Sheerak M’s stories in your inbox

Join Medium for free to get updates from this writer.

I create a VB console application called Module1.vb as follows:

Imports System
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting

Module Module1

Sub Main()
Console.WriteLine("Called from VB")
Dim pyEngine As ScriptEngine = Python.CreateEngine()
Dim scope As ScriptScope = pyEngine.CreateScope()

' the script you gonna call n
pyEngine.ExecuteFile("pythonfile.py", scope)

' get the name of the class or function
Dim workingMethod As Object = scope.GetVariable("print_hello")
Dim result As Object = workingMethod() 'actually calling the function
Console.WriteLine(result) ' printing hte results to the console
End Sub

End Module

Python code called pythonfile.py that is being called from VB:

Pythonfile.py:

def print_hello():
print("Hello World!")
import time
time.sleep(10)
return 1
input()

Thank you, please follow if you enjoyed this post.

--

--

Sheerak M
Sheerak M

Written by Sheerak M

Berkeley Alumnus, Software Engineer