{"id":13437941,"url":"https://github.com/dgrunwald/rust-cpython","last_synced_at":"2025-05-13T21:11:47.847Z","repository":{"id":25391765,"uuid":"28820369","full_name":"dgrunwald/rust-cpython","owner":"dgrunwald","description":"Rust \u003c-\u003e Python bindings","archived":false,"fork":false,"pushed_at":"2024-08-14T18:24:30.000Z","size":6718,"stargazers_count":1820,"open_issues_count":61,"forks_count":136,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-04-28T14:57:25.987Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgrunwald.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-01-05T16:14:46.000Z","updated_at":"2025-04-26T04:57:05.000Z","dependencies_parsed_at":"2024-06-18T13:55:49.699Z","dependency_job_id":"4df8b7fb-4cc1-471f-8431-4d555eac2235","html_url":"https://github.com/dgrunwald/rust-cpython","commit_stats":{"total_commits":494,"total_committers":55,"mean_commits":8.981818181818182,"dds":0.4311740890688259,"last_synced_commit":"a60f149f2f64be2d533cf72364fdaeaa8b08893f"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrunwald%2Frust-cpython","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrunwald%2Frust-cpython/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrunwald%2Frust-cpython/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrunwald%2Frust-cpython/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgrunwald","download_url":"https://codeload.github.com/dgrunwald/rust-cpython/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254029004,"owners_count":22002283,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:01.481Z","updated_at":"2025-05-13T21:11:42.806Z","avatar_url":"https://github.com/dgrunwald.png","language":"Rust","readme":"rust-cpython [![Build Status](https://github.com/dgrunwald/rust-cpython/actions/workflows/test.yml/badge.svg)](https://github.com/dgrunwald/rust-cpython/actions/workflows/test.yml)\n====================\n\nWarning: this package is no longer actively maintained.\nPlease switch to [PyO3](https://github.com/PyO3/pyo3) instead.\n\n[Rust](http://www.rust-lang.org/) bindings for the [python](https://www.python.org/) interpreter.\n\n* [Documentation](http://dgrunwald.github.io/rust-cpython/doc/cpython/)\n* Cargo package: [cpython](https://crates.io/crates/cpython)\n\n---\n\nCopyright (c) 2015-2021 Daniel Grunwald.\nRust-cpython is licensed under the [MIT license](http://opensource.org/licenses/MIT).\nPython is licensed under the [Python License](https://docs.python.org/2/license.html).\n\nSupported Python versions:\n* Python 2.7\n* Python 3.7 to 3.12\n\nWarning: this package is no longer actively maintained.\nPlease switch to [PyO3](https://github.com/PyO3/pyo3) instead.\n\nRequires Rust 1.41.1 or later.\n\n# Usage\n\nTo use `cpython`, add this to your `Cargo.toml`:\n\n```toml\n[dependencies]\ncpython = \"0.7\"\n```\n\n#### Example program displaying the value of `sys.version`:\n\n```rust\nuse cpython::{Python, PyDict, PyResult};\n\nfn main() {\n    let gil = Python::acquire_gil();\n    hello(gil.python()).unwrap();\n}\n\nfn hello(py: Python) -\u003e PyResult\u003c()\u003e {\n    let sys = py.import(\"sys\")?;\n    let version: String = sys.get(py, \"version\")?.extract(py)?;\n\n    let locals = PyDict::new(py);\n    locals.set_item(py, \"os\", py.import(\"os\")?)?;\n    let user: String = py.eval(\"os.getenv('USER') or os.getenv('USERNAME')\", None, Some(\u0026locals))?.extract(py)?;\n\n    println!(\"Hello {}, I'm Python {}\", user, version);\n    Ok(())\n}\n```\n\n#### Example library with python bindings:\nThe following two files will build with `cargo build`, and will generate a python-compatible library.\nOn Mac OS, you will need to rename the output from \\*.dylib to \\*.so.\nOn Windows, you will need to rename the output from \\*.dll to \\*.pyd.\n\n###### Note:\nAt build time `python3-sys/build.rs` will look for interpreters in: \n* `PYTHON_SYS_EXECUTABLE`\n* `python`\n* `python3`\n\npicking the first one that works and is compatible with the configured expected version (by default, any Python 3.X interpreter will do). If a specific interpreter is desired, the `PYTHON_SYS_EXECUTABLE` environment variable should point to it.\n\n**`Cargo.toml`:**\n```toml\n[lib]\nname = \"rust2py\"\ncrate-type = [\"cdylib\"]\n\n[dependencies.cpython]\nversion = \"0.7\"\nfeatures = [\"extension-module\"]\n```\n\n**`src/lib.rs`**\n```rust\nuse cpython::{PyResult, Python, py_module_initializer, py_fn};\n\n// add bindings to the generated python module\n// N.B: names: \"rust2py\" must be the name of the `.so` or `.pyd` file\npy_module_initializer!(rust2py, |py, m| {\n    m.add(py, \"__doc__\", \"This module is implemented in Rust.\")?;\n    m.add(py, \"sum_as_string\", py_fn!(py, sum_as_string_py(a: i64, b:i64)))?;\n    Ok(())\n});\n\n// logic implemented as a normal rust function\nfn sum_as_string(a:i64, b:i64) -\u003e String {\n    format!(\"{}\", a + b).to_string()\n}\n\n// rust-cpython aware function. All of our python interface could be\n// declared in a separate module.\n// Note that the py_fn!() macro automatically converts the arguments from\n// Python objects to Rust values; and the Rust return value back into a Python object.\nfn sum_as_string_py(_: Python, a:i64, b:i64) -\u003e PyResult\u003cString\u003e {\n    let out = sum_as_string(a, b);\n    Ok(out)\n}\n```\n\nOn windows and linux, you can build normally with cargo build --release. On Mac Os, you need to set additional linker arguments. The simplest solution is to create a `.cargo/config` with the following content:\n\n```\n[target.x86_64-apple-darwin]\nrustflags = [\n  \"-C\", \"link-arg=-undefined\",\n  \"-C\", \"link-arg=dynamic_lookup\",\n]\n```\n\nFor `setup.py` integration, see https://github.com/PyO3/setuptools-rust\n\n# Development\n\nTo build the crate, run: `make build`\n\nTo test the crate, run: `make test`\n\nNote: This crate has several files that are auto-generated using scripts. Using the Makefile ensures that these\nfiles are re-generated as needed.\n","funding_links":[],"categories":["Development tools","Rust","开发工具 Development tools","开发工具"],"sub_categories":["FFI","FFI FFI","示例 FFI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrunwald%2Frust-cpython","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgrunwald%2Frust-cpython","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrunwald%2Frust-cpython/lists"}