0

I am trying to Execute python script from my .Net Web Application. For this I have installed Python from nuget package manager. But I am getting following errors during execution:

I have attempted following code chunks for getting resolution but I am not able to execute it successfully.

Below is the Python Scrip to Insert a record in SQL Server database:

import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 
11.0};SERVER=*****\SQLEXPRESS;DATABASE=TestDB;UID=sa;PWD=****')
cur = conn.cursor()
cur.execute("Insert into Results([EmailID],[Journey]) VALUES (?,?)", 55, 
"JourneyName") 
conn.commit()
Print("Success")  

Below is the C# Code to execute above python file.

public string run_cmd(string strPath) //this must not be async
{
    try
    {
       ProcessStartInfo start = new ProcessStartInfo();
       start.FileName = @"C:\Anaconda\python.exe";
       start.Arguments = string.Format("C:\\Users\\261866\\TestPYCode.py");
       start.UseShellExecute = false;
       start.CreateNoWindow = true; 
       start.RedirectStandardOutput = true;
       start.RedirectStandardError = true;
       using (Process process = Process.Start(start))
       {
           using (StreamReader reader = process.StandardOutput)
           {
               string stderr = process.StandardError.ReadToEnd();
               string result = reader.ReadToEnd(); 
               return result;
           }
        }
     }
        catch (Exception ex)
        {
            return ex.Message;
        }
            return "run till end";
    }

As I have executed above c# code I am getting below Error:

Traceback (most recent call last):"C:\Users\261866\TestPYCode.py" import pyodbc Module Not Found Error: No module named 'pyodbc'

Error: No module named 'pyodbc'

8
  • The error speaks for itself. Commented Jan 31, 2019 at 11:41
  • Hi @user5173426, Thanks for advise. I have edited my post for more clarity. Commented Jan 31, 2019 at 11:45
  • 1
    you have to install pyodbc package. How did you installed it? Try to exec command: "npm i pyodbc@latest" from cmd. Remember that you have to be on your application folder. Commented Jan 31, 2019 at 11:46
  • I am getting errors there also is not helping. You need to specify the errors you are getting. Also, with that edit (errors while installing), the remainder of the question is kind of irrelevant now. So, either delete the question and ask a new one for the pyodbc install errors, or edit this and make the installation errors as the primary ask. Commented Jan 31, 2019 at 12:56
  • @Presto I have installed pyodbc packages, its saying, 'Requirement already satisfied'. and installing "npm i pyodbc@latest" in cmd, giving eror npm is not recognized as an internal or external command, operable program or batch file. Commented Feb 4, 2019 at 10:48

1 Answer 1

1

The problem might be the source from where comes pyodbc. If your pyodbc comes from a .whl file then probably you have to install it using pip.

Here is the Microsoft's documentation says how to configure development environment for pyodbc.

Did you try it?

And here you've got ODBC Driver for SQL Server on Windows where you can download it and install.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Presto for your suggestions. My colleagues have found a solution to it.
good for you @ParagS! Would you present solution of this problem? Might be a worth to know it or you could do it for other users with this same problem.

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.