docs(guide): example of loading python file from path at runtime in a way that it sees its dependencies (#2109)
* docs(guide): example of loading python file from path at runtime in a way that it sees its dependencies * docs: editorial fixes suggested by @mejrs Co-authored-by: Bruno Kolenbrander <59372212+mejrs@users.noreply.github.com> Co-authored-by: Bruno Kolenbrander <59372212+mejrs@users.noreply.github.com>
This commit is contained in:
parent
86f5389526
commit
d1542a8a79
|
@ -255,7 +255,7 @@ Example directory structure:
|
||||||
.
|
.
|
||||||
├── Cargo.lock
|
├── Cargo.lock
|
||||||
├── Cargo.toml
|
├── Cargo.toml
|
||||||
├── python
|
├── python_app
|
||||||
│ ├── app.py
|
│ ├── app.py
|
||||||
│ └── utils
|
│ └── utils
|
||||||
│ └── foo.py
|
│ └── foo.py
|
||||||
|
@ -263,7 +263,7 @@ Example directory structure:
|
||||||
└── main.rs
|
└── main.rs
|
||||||
```
|
```
|
||||||
|
|
||||||
`python/app.py`:
|
`python_app/app.py`:
|
||||||
```python
|
```python
|
||||||
from utils.foo import bar
|
from utils.foo import bar
|
||||||
|
|
||||||
|
@ -272,7 +272,7 @@ def run():
|
||||||
return bar()
|
return bar()
|
||||||
```
|
```
|
||||||
|
|
||||||
`python/utils/foo.py`:
|
`python_app/utils/foo.py`:
|
||||||
```python
|
```python
|
||||||
def bar():
|
def bar():
|
||||||
return "baz"
|
return "baz"
|
||||||
|
@ -288,8 +288,8 @@ The example below shows:
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
fn main() -> PyResult<()> {
|
fn main() -> PyResult<()> {
|
||||||
let py_foo = include_str!("../python/utils/foo.py");
|
let py_foo = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/python_app/utils/foo.py"));
|
||||||
let py_app = include_str!("../python/app.py");
|
let py_app = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/python_app/app.py"));
|
||||||
let from_python = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
let from_python = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
||||||
PyModule::from_code(py, py_foo, "utils.foo", "utils.foo")?;
|
PyModule::from_code(py, py_foo, "utils.foo", "utils.foo")?;
|
||||||
let app: Py<PyAny> = PyModule::from_code(py, py_app, "", "")?
|
let app: Py<PyAny> = PyModule::from_code(py, py_app, "", "")?
|
||||||
|
@ -304,23 +304,28 @@ fn main() -> PyResult<()> {
|
||||||
```
|
```
|
||||||
|
|
||||||
The example below shows:
|
The example below shows:
|
||||||
* how to load content of `app.py` and `utils/foo.py` at runtime
|
* how to load content of `app.py` at runtime so that it sees its dependencies
|
||||||
|
automatically
|
||||||
* how to call function `run()` (declared in `app.py`) that needs function
|
* how to call function `run()` (declared in `app.py`) that needs function
|
||||||
imported from `utils/foo.py`
|
imported from `utils/foo.py`
|
||||||
|
|
||||||
It is recommended to use an absolute path to your Python files because then
|
It is recommended to use absolute paths because then your binary can be run
|
||||||
your binary can be run from anywhere.
|
from anywhere as long as your `app.py` is in the expected directory (in this example
|
||||||
|
that directory is `/usr/share/python_app`).
|
||||||
|
|
||||||
`src/main.rs`:
|
`src/main.rs`:
|
||||||
```ignore
|
```ignore
|
||||||
use std::fs;
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
use pyo3::types::PyList;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
fn main() -> PyResult<()> {
|
fn main() -> PyResult<()> {
|
||||||
let py_foo = fs::read_to_string("/some/absolute/path/to/python/utils/foo.py")?;
|
let path = Path::new("/usr/share/python_app");
|
||||||
let py_app = fs::read_to_string("/some/absolute/path/to/python/app.py")?;
|
let py_app = fs::read_to_string(path.join("app.py"))?;
|
||||||
let from_python = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
let from_python = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
||||||
PyModule::from_code(py, &py_foo, "utils.foo", "utils.foo")?;
|
let syspath: &PyList = py.import("sys")?.getattr("path")?.downcast::<PyList>()?;
|
||||||
|
syspath.insert(0, &path)?;
|
||||||
let app: Py<PyAny> = PyModule::from_code(py, &py_app, "", "")?
|
let app: Py<PyAny> = PyModule::from_code(py, &py_app, "", "")?
|
||||||
.getattr("run")?
|
.getattr("run")?
|
||||||
.into();
|
.into();
|
||||||
|
|
Loading…
Reference in a new issue