[Python] 'builtin_function_or_method' object cannot be interpreted as an integer when trying...

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    While trying to develop python packages using rust code, I had some problems that seem to preceed the function invocation in rust. I've tried to implement this simple function:

    use polars::prelude::DataFrame as PolarsDataFrame;
    use pyo3::prelude::*;
    use pyo3_polars::pyDataFrame;

    #[pyfunction]
    fn test_print(pydf: PyDataFrame) {
    let df: PolarsDataFrame = pydf.into();
    println!("Inside print!");
    println!("Test print from Rust: {}", df.size());
    }

    #[pymodule]
    fn rustpackage(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(test_print, m)?)?;
    Ok(())
    }


    My cargo.toml looks like this:

    [package]
    name = "rustpackage"
    version = "0.1.0"
    edition = "2024"

    [dependencies]
    polars = "0.50.0"
    pyo3 = "0.25.1"
    pyo3-polars = "0.23.1"


    And my pyproject:

    [build-system]
    requires = ["maturin>=1,<2"]
    build-backend = "maturin"

    [project]
    name = "rustpackage"
    requires-python = ">=3.7"
    classifiers = [
    "Programming Language :: Rust",
    "Programming Language :: Python :: Implementation :: CPython",
    "Programming Language :: Python :: Implementation :: PyPy",
    ]
    version = "0.1.0"
    description = "Example packge"


    Then, when running this script:

    from rustpackage import test_print

    import polars as pl
    from polars._utils.construction.dataframe import dataframe_to_pydf

    df = pl.DataFrame(
    {
    "list_a": [["1", "2", "3"], ["5", "5"]],
    "list_b": [["1", "2", "3", "8"], ["5", "1", "1"]],
    }
    )
    pydf = dataframe_to_pydf(df)
    test_print(pydf)


    I get this error message:

    Traceback (most recent call last):
    File "~/Workspace/test-polars/run.py", line 13, in <module>
    test_print(pydf)
    TypeError: argument 'pydf': 'builtin_function_or_method' object cannot be interpreted as an integer


    I was expecting sucessfull invocation of test_print fn.

    Continue reading...

Compartilhe esta Página