Import Python Modules in SystemVerilog

Python’s modules can be accessed directly via PyStim py_module::import . This allows you to leverage Python’s standard library or other modules, such as NumPy and pandas, for computation, visualization, or machine learning.

import pystim_pkg::*;

module math_pi();

    typedef pystim_pkg::pystim py;

    initial begin
        py::initialize_interpreter();
        begin
            automatic py_module math_module = py_module::import_("math");
            automatic py_object pi = math_module.attr("pi").obtain();
            automatic real pi_value = pi.cast_float().get_value();
            $display("The value of pi is: %0f", pi_value);
        end
        py::finalize_interpreter();
    end

endmodule

This SystemVerilog code snippet demonstrates how to interact with Python’s math module from within a SystemVerilog simulation environment. It retrieves the value of the mathematical constant pi from Python and displays it in the simulation output.