Updated on 22 Sep 2025 by Admin

Bridging the Gap – Advanced Python.NET: Type Conversion, Collections, and Exception Handling

In the previous article, we successfully connected our Python model with a .NET console application. If you faced any errors, the following might be the reason behind:

  • DLLNotFoundException: This exception occurs when you have specified an incorrect path for the Python DLL. Ensure that your PYTHONHOME environment variable is set to the correct path, and the bitness of your .NET application and Python interpreter match.
  • ImportError: This indicates that .NET failed to locate your sentiment_model.py file. Ensure that the file exists in the same directory as your C# application.
  • Missing Dependencies: This error indicates that some of the required packages are not installed.

Once you have resolved the issues with your previous project and it runs seamlessly, you are ready to explore the advanced Python.NET concepts.

In this article, you will learn:

  • Passing complex data structures like dictionaries and lists between C# and Python seamlessly.
  • Iterating over Python collections from C#.
  • Gracefully catching and handling Python errors in your C# application.

Without further ado, let's get started with the project.


Setting Up the Project

To work with collections and data types, let's create a new console application in C#. You can do so using an IDE. In this tutorial, I'll be using the terminal to create and run files.

  1. Create a new console application by running the following command in your terminal:

dotnet new console -o AdvancedPythonNet

  1. Now, go to the project directory:

cd AdvancedPythonNet

  1. Add Python.NET from NuGet.org:

dotnet add package pythonnet


Creating the Python Module

For our example, we will use Python's built-in `json` module that parses a string and returns a dictionary. This is a very common way to understand how to work with the Python standard library and handle complex data types.

  1. Open your Python IDE and create a new Python file named data_processor.py in your AdvancedPythonNet folder.
  2. Add the following code to data_processor.py:

# data_processor.py
import json

def process_user_data(json_string):
    """
    Parses a JSON string and returns a Python dictionary.
    """
    print("Python received a JSON string. Now processing...")
    try:
        data = json.loads(json_string)
        print("JSON parsing successful. Returning data as a dictionary.")
        return data
    except json.JSONDecodeError as e:
        print(f"Error: Could not parse JSON string. {e}")
        return None


def get_keys(dictionary):
    """
    Takes a dictionary and returns a list of its keys.
    """
    print("Python received a dictionary. Getting keys...")
    return list(dictionary.keys())

def generate_error():
    """
    A function that deliberately raises an error.
    """
    raise ValueError("This is a deliberate error from Python.")


Writing C# Integration Code

Now, it's time to call our new Python module inside our .NET application. We'll write some critical code to understand how to convert data types and handle exceptions:

  1. Open your Program.cs file inside a code editor and add the following code to it:

// Program.cs

using System;
using System.IO;
using Python.Runtime;

class Program
{
    static void Main(string[] args)
    {
        // Set the Python DLL. This is crucial for Python.NET to find the correct
        // Python installation. Replace the path with the location of your pythonXX.dll.
        
        Runtime.PythonDLL = @"C:\\Users\\raoha\\AppData\\Local\\Programs\\Python\\Python312\\python312.dll";

        // Initialize the Python engine.
        PythonEngine.Initialize();

        try
        {
            using (Py.GIL())
            {
                // Set the path to our Python module.
                string pythonModulePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data_processor.py");

                // Get a reference to Python's sys module and add our module's directory to the search path.
                dynamic sys = Py.Import("sys");

                // Use a robust method to call the `insert` function on the Python list.
                PyObject pyPath = sys.path;
                pyPath.GetAttr("insert").Invoke(new PyInt(0), new PyString(Path.GetDirectoryName(pythonModulePath)));

                // 1. Calling a function and passing a string.
                dynamic dataProcessor = Py.Import("data_processor");
                string jsonString = "{\"user_id\": 123, \"name\": \"Jane Doe\", \"is_active\": true}";
                dynamic pythonDict = dataProcessor.process_user_data(jsonString);

                // Python.NET automatically converts the Python dictionary to a C# dynamic type.
                if (pythonDict != null)
                {
                    Console.WriteLine("\n--- C# Received the Data ---");
                    Console.WriteLine($"User ID: {pythonDict["user_id"]}");
                    Console.WriteLine($"Name: {pythonDict["name"]}");
                    Console.WriteLine($"Is Active: {pythonDict["is_active"]}");
                }

                Console.WriteLine("\n--- Getting Keys from the Dictionary ---");
                // 2. Passing the dictionary back to Python and working with collections.
                dynamic keysList = dataProcessor.get_keys(pythonDict);

                // You can iterate over the Python list directly from C#.
                Console.WriteLine("C# iterating over the Python list:");
                foreach (dynamic key in keysList)
                {
                    Console.WriteLine($"- {key}");
                }

                Console.WriteLine("\n--- Demonstrating Exception Handling ---");
                // 3. Handling exceptions.
                try
                {
                    dataProcessor.generate_error();
                }
                catch (PythonException ex)
                {
                    Console.WriteLine($"C# caught a Python exception!");
                    Console.WriteLine($"Error type: {ex.Type.Name}");
                    Console.WriteLine($"Error message: {ex.Message}");
                }
            }
        }
        finally
        {

        }
    }
}

Note: Before running the code, ensure to replace "C:\\Users\\raoha\\AppData\\Local\\Programs\\Python\\Python312\\python312.dll" with your Python installation path.


Run the Application

Finally, run your console application by running the following command in Command Prompt:

<
dotnet run

If everything is set up properly, your application will run smoothly, initializing the Python engine, processing the JSON string, and gracefully handling the exceptions.

Now you have another tool in your hands that allows you to pass complex data types, iterate over Python collections, and handle exceptions gracefully. In our next article, we will dive deep into more advanced tools while creating a real-world application that uses Python libraries like pandas to perform data analysis tasks.


You need to login to post a comment.

Sharpen Your Skills with These Next Guides