I am trying to run a python script from VB.Net using IronPython. So far, I have installed Python and IronPython. I have the ExecPython method shown below. It works fine when I call a simple print/hello world type of script. This DBTest.py script is just using pyodbc and connecting to the database and executing a basic select query.
The error I get at the source.Execute(scope) line is "IronPython.Runtime.Exceptions.ImportException: 'No module named 'pyodbc''"
I've installed pyodbc using pip install pyodbc. The DBTest.py script runs fine when I run it with IDLE. I'm not sure if this is a limitation or if there's something I'm missing in the setup.
Thanks in advance for your help!!
Sub ExecPython(ByVal argv As List(Of String))
Dim engine As ScriptEngine = Python.CreateEngine
Dim scriptPath As String = "C:\scripts\DBTest.py"
Dim source As ScriptSource = engine.CreateScriptSourceFromFile(scriptPath)
argv.Add("")
engine.GetSysModule.SetVariable("argv", argv)
engine.SetSearchPaths({"C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39\Scripts",
"C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39\include",
"C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39\Lib",
"C:\Program Files\IronPython 3.4\Lib"})
Dim eIO As ScriptIO = engine.Runtime.IO
Dim errors As New MemoryStream
eIO.SetErrorOutput(errors, Text.Encoding.Default)
Dim results As New MemoryStream
eIO.SetOutput(results, Text.Encoding.Default)
Dim scope As ScriptScope = engine.CreateScope
source.Execute(scope)
Console.WriteLine("ERRORS:")
Console.WriteLine(FormatResult(errors.ToArray))
Console.WriteLine("")
Console.WriteLine("RESULTS:")
Console.WriteLine(FormatResult(results.ToArray))
End Sub
Here is the python script that I am calling. It runs when I run the module from IDLE.
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=MYSERVERNAME;'
'Database=MYDBNAME;'
'Trusted_Connection=yes;')
cursor = conn.cursor() cursor.execute('SELECT * FROM dbo.TABLENAME')
for row in cursor:
print(row)