Skip to content

Commit 6dac76a

Browse files
committed
adding src_python dir to sys.path
1 parent 9479a58 commit 6dac76a

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

src/lib.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use py_lib_pyo3 as py_lib;
2626

2727
pub use error::{Error, Result};
2828
use models::*;
29+
use std::path::PathBuf;
2930

3031
#[cfg(desktop)]
3132
use desktop::Python;
@@ -63,21 +64,14 @@ impl<R: Runtime, T: Manager<R>> crate::PythonExt<R> for T {
6364
}
6465
}
6566

66-
fn read_main_py_from_resources<R: Runtime>(app: &AppHandle<R>) -> String {
67-
let py_file_path = app
68-
.path()
69-
.resolve("src-python/main.py", BaseDirectory::Resource)
70-
.unwrap_or_default();
71-
std::fs::read_to_string(&py_file_path).unwrap_or_default()
67+
fn get_resource_dir<R: Runtime>(app: &AppHandle<R>) -> PathBuf {
68+
app.path()
69+
.resolve("src-python", BaseDirectory::Resource)
70+
.unwrap_or_default()
7271
}
7372

74-
fn read_main_py_from_current_dir() -> String {
75-
let py_file_path = std::env::current_dir()
76-
.unwrap()
77-
.join("src-python")
78-
.join("main.py");
79-
std::fs::read_to_string(py_file_path).unwrap_or_default()
80-
// include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
73+
fn get_current_dir() -> PathBuf {
74+
std::env::current_dir().unwrap().join("src-python")
8175
}
8276

8377
/// Initializes the plugin with functions
@@ -101,17 +95,19 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
10195
let python = desktop::init(app, api)?;
10296
app.manage(python);
10397

104-
let mut code = read_main_py_from_resources(app);
98+
let mut dir = get_resource_dir(app);
99+
let mut code = std::fs::read_to_string(&dir.join("main.py")).unwrap_or_default();
105100
if code.is_empty() {
106101
println!(
107102
"Warning: 'src-tauri/main.py' seems not to be registered in 'tauri.conf.json'"
108103
);
109-
code = read_main_py_from_current_dir();
104+
dir = get_current_dir();
105+
code = std::fs::read_to_string(&dir.join("main.py")).unwrap_or_default();
110106
}
111107
if code.is_empty() {
112108
println!("ERROR: Error reading 'src-tauri/main.py'");
113109
}
114-
py_lib::init_python(code).unwrap();
110+
py_lib::init_python(code, dir).unwrap();
115111
for function_name in python_functions {
116112
py_lib::register_function_str(function_name.into(), None).unwrap();
117113
}

src/py_lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

6+
use std::path::PathBuf;
67
use std::sync::atomic::AtomicBool;
78
use std::{collections::HashSet, sync::Mutex};
89

@@ -23,16 +24,22 @@ lazy_static! {
2324
static ref GLOBALS: rustpython_vm::scope::Scope = create_globals();
2425
}
2526

26-
pub fn init_python(code: String) -> crate::Result<()> {
27+
pub fn init_python(code: String, dir: PathBuf) -> crate::Result<()> {
2728
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
28-
let code_obj = vm
29-
.compile(
30-
&code,
31-
rustpython_vm::compiler::Mode::Exec,
32-
"<embedded>".to_owned(),
33-
)
34-
.map_err(|err| vm.new_syntax_error(&err, Some(&code)))?;
35-
vm.run_code_obj(code_obj, GLOBALS.clone())
29+
vm.import("sys", 0).unwrap();
30+
let path_import = format!("sys.path.append('{}')", dir.to_str().unwrap());
31+
let exec_python = |code: String| {
32+
let code_obj = vm
33+
.compile(
34+
&code,
35+
rustpython_vm::compiler::Mode::Exec,
36+
"<embedded>".to_owned(),
37+
)
38+
.map_err(|err| vm.new_syntax_error(&err, Some(&code)))?;
39+
vm.run_code_obj(code_obj, GLOBALS.clone())
40+
};
41+
exec_python(path_import)?;
42+
exec_python(code)
3643
})?;
3744
Ok(())
3845
}

src/py_lib_pyo3.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

6+
use std::path::PathBuf;
67
use std::sync::atomic::AtomicBool;
78
use std::{collections::HashMap, ffi::CString, sync::Mutex};
89

@@ -21,18 +22,14 @@ lazy_static! {
2122
Mutex::new(marker::Python::with_gil(|py| { PyDict::new(py).into() }));
2223
}
2324

24-
fn get_py_path() -> std::path::PathBuf {
25-
std::env::current_dir().unwrap().join("src-python")
26-
}
27-
28-
pub fn init_python(code: String) -> crate::Result<()> {
25+
pub fn init_python(code: String, dir: PathBuf) -> crate::Result<()> {
2926
let c_code = CString::new(code).expect("error creating cstring from code");
3027
Ok(marker::Python::with_gil(|py| -> PyResult<()> {
3128
let syspath = py
3229
.import("sys")?
3330
.getattr("path")?
3431
.downcast_into::<PyList>()?;
35-
syspath.insert(0, get_py_path().to_str())?;
32+
syspath.insert(0, dir.to_str())?;
3633
let globals = GLOBALS.lock().unwrap().clone_ref(py).into_bound(py);
3734
py.run(&c_code, Some(&globals), None)
3835
})?)

0 commit comments

Comments
 (0)