Skip to main content
TopMiniSite

Back to all posts

How to Call A Python Function From Rust?

Published on
7 min read
How to Call A Python Function From Rust? image

Best Programming Language Tools to Buy in October 2025

1 Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language

Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language

BUY & SAVE
$29.50
Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language
2 Write Powerful Rust Macros

Write Powerful Rust Macros

BUY & SAVE
$46.72 $59.99
Save 22%
Write Powerful Rust Macros
3 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$29.99
The Rust Programming Language, 2nd Edition
4 Rust Programming Language - Developer Tools and Libraries T-Shirt

Rust Programming Language - Developer Tools and Libraries T-Shirt

  • MEMORY-EFFICIENT: NO RUNTIME OR GARBAGE COLLECTOR FOR FASTER PERFORMANCE.
  • FRIENDLY COMPILER: GET USEFUL ERROR MESSAGES AND SMART SUPPORT TOOLS.
  • PERFECT FOR DEVELOPERS: BUILD RELIABLE SOFTWARE ACROSS MULTIPLE PLATFORMS.
BUY & SAVE
$19.99
Rust Programming Language - Developer Tools and Libraries T-Shirt
5 Rust Programming Language - Developer Tool for Collaborating T-Shirt

Rust Programming Language - Developer Tool for Collaborating T-Shirt

  • BUILD RELIABLE, EFFICIENT SOFTWARE WITH ZERO RUNTIME OVERHEAD.
  • PERFECT FOR PERFORMANCE-CRITICAL SERVICES AND EMBEDDED DEVICES.
  • USER-FRIENDLY TOOLING: SMART EDITOR SUPPORT & HELPFUL ERROR MESSAGES.
BUY & SAVE
$19.99
Rust Programming Language - Developer Tool for Collaborating T-Shirt
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
7 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$51.16
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
8 Rust Programming: A Fast-Track Guide: Learn the fundamentals of Rust programming language

Rust Programming: A Fast-Track Guide: Learn the fundamentals of Rust programming language

BUY & SAVE
$2.99
Rust Programming: A Fast-Track Guide: Learn the fundamentals of Rust programming language
9 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$30.65
Programming Rust: Fast, Safe Systems Development
10 Rust Programming for Beginners: An Introduction to Learning Rust Programming with Tutorials and Hands-On Examples

Rust Programming for Beginners: An Introduction to Learning Rust Programming with Tutorials and Hands-On Examples

BUY & SAVE
$3.99
Rust Programming for Beginners: An Introduction to Learning Rust Programming with Tutorials and Hands-On Examples
+
ONE MORE?

To call a Python function from Rust, you need to follow these steps:

  1. Add the pyo3 crate as a dependency in your Cargo.toml file. This crate provides Rust bindings for the Python interpreter.

[dependencies] pyo3 = "0.15"

  1. In your Rust code, import the necessary modules and functions from pyo3:

use pyo3::prelude::*; use pyo3::types::IntoPyDict;

  1. Initialize the Python interpreter using the Python::acquire_gil method. This will ensure thread safety when calling Python functions:

let gil = Python::acquire_gil(); let py = gil.python();

  1. Import your Python module using PyModule::new and PyModule::add methods. For example, if your module is named my_module:

let my_module = PyModule::new(py, "my_module")?; py.get("sys")?.dict()?.set_item("my_module", my_module)?;

  1. Call the desired Python function using the PyObject::call method. You need to pass the function name and its arguments:

let result = my_module.call(py, "my_python_function", (arg1, arg2), None)?;

  1. Extract the result from the returned PyObject and convert it to the appropriate Rust data type:

let result_string: String = result.extract()?;

Note that it's important to handle errors appropriately using proper error handling mechanisms like Result or panicking. Also, ensure that you have a Python environment properly installed and your Python module is accessible in the Python path.

Remember to release the Python interpreter once you are done to prevent any resource leaks:

drop(gil);

That's it! You can now call a Python function from Rust.

Can I use Python libraries directly from Rust?

Yes, it is possible to use Python libraries directly from Rust. Rust provides a Foreign Function Interface (FFI) that allows interoperability with other programming languages. Python has a C API, which makes it possible to call Python functions and interact with Python objects from Rust.

To use Python libraries from Rust, you can follow these steps:

  1. Use the rust-cpython crate: The rust-cpython crate provides Rust bindings for the Python C API. It allows you to write Rust code that can call Python functions, handle Python objects, and interact with the Python runtime.
  2. Configure your Rust project: Add rust-cpython as a dependency in your Cargo.toml file.
  3. Write Rust code with Python interop: In your Rust code, you can use the types and functions provided by rust-cpython to interact with Python. You can import Python modules, call Python functions, create Python objects, and manipulate Python data.

It is important to note that using Python libraries from Rust may introduce some complexities due to the differences in memory management and concurrency models between the two languages. Additionally, there might be some performance overhead when crossing the language boundary.

Are there any compatibility concerns between different versions of Rust and Python?

Yes, there can be compatibility concerns between different versions of Rust and Python. When using the Python Rust integration, such as using the rust-cpython or pyo3 libraries, it is important to ensure that the versions of Rust and Python you are using are compatible.

Compatibility issues can arise due to differences in the API provided by the Rust library and the Python interpreter. In general, the Rust library should be compiled with the same version of Rust that was used to compile the Python interpreter or the Python extension module it is interacting with.

Additionally, changes in the Python C API or Rust library API can impact compatibility. These changes may introduce breaking changes that require modifications in the Rust code or Python code.

It is recommended to consult the documentation and release notes of the Rust library and Python interpreter you are using to determine their compatibility or any specific version requirements.

Can Python functions return values to Rust?

Yes, Python functions can return values to Rust.

To achieve this, you can use Rust's Foreign Function Interface (FFI) to call Python functions from Rust. The cpython crate is commonly used for this purpose, as it provides a safe and high-level interface for calling Python functions.

Here's an example of using FFI to call a Python function from Rust and return the result:

  1. On the Rust side, you need to include the cpython crate and define the Python function's signature:

use cpython::{Python, PyResult};

fn call_python_function() -> PyResult { let gil = Python::acquire_gil(); let py = gil.python(); let function_name = "my_python_function";

let result = py
    .import("my\_module")?
    .getattr(function\_name)?
    .call(py, None)?;

Ok(result.extract::<u32>(py)?)

}

  1. On the Python side, you need to have the my_module module with the my_python_function function defined:

def my_python_function(): return 42

  1. Finally, you can call the call_python_function from Rust and receive the return value:

fn main() { match call_python_function() { Ok(result) => println!("The Python function returned: {}", result), Err(err) => eprintln!("Error calling Python function: {}", err), } }

Note that you need to configure your project to ensure that the Python interpreter and the necessary dependencies are available for the Rust code to link against. The cpython crate's documentation provides detailed instructions on how to set up your environment.

Can Rust call Python functions?

Yes, Rust can call Python functions by using the CPython API. The CPython API allows Rust code to interact with Python code, making it possible to call Python functions, pass data between Rust and Python, and access Python modules and types.

To call Python functions from Rust, you would need to:

  1. Embed the CPython interpreter in your Rust application using the cpython crate.
  2. Create a Python interpreter instance and initialize it.
  3. Import the required Python modules or functions.
  4. Use the PyObject_CallObject function to call the Python function, passing any required arguments.
  5. Convert the returned Python object back to a Rust value if needed.

Here's an example of calling a Python function from Rust using the cpython crate:

use cpython::{Python, PyResult, PyString, PyDict};

fn main() -> PyResult<()> { let gil = Python::acquire_gil(); let py = gil.python();

let sys = py.import("sys")?;
let version: PyString = sys.get(py, "version")?.extract(py)?;
println!("Python version: {}", version.to\_string(py));

let math = py.import("math")?;
let result = math.call(py, "sqrt", (144,), None)?;

let result\_str: String = result.extract(py)?;
println!("Square root of 144: {}", result\_str);

Ok(())

}

Note that calling Python functions from Rust may involve handling Python exceptions, converting Python objects to Rust types, and managing the Python Global Interpreter Lock (GIL) appropriately for multi-threaded applications.

What tools or libraries are available to facilitate calling Python functions from Rust?

There are several options available for calling Python functions from Rust. Some popular tools and libraries are:

  1. rust-cpython: This library provides a low-level interface for embedding the CPython interpreter in Rust. It allows calling Python functions from Rust and vice versa. It supports passing simple types and converting data between Rust and Python.
  2. python-embed crate: It provides a higher-level interface to embed the CPython interpreter in Rust. It simplifies the process of calling Python functions, handling errors, and manipulating Python objects.
  3. pyo3: It is a Rust binding for the Python interpreter. It offers a high-level, safe, and convenient interface to interact with Python. It provides an idiomatic Rust API for calling Python functions, handling Python objects, and managing the Python Global Interpreter Lock (GIL).
  4. cpython: This library provides a set of Rust macros to define and call Python functions. It has a direct mapping of Python functions and supports passing arguments and returning values.
  5. cffi: It is a foreign function interface for Python. It allows you to define C functions that call Python code, with the ability to pass data between C and Python.

These libraries provide different levels of abstraction and flexibility, so you can choose the one that suits your needs and preferences.