Skip to content

Commit c4220ef

Browse files
committed
fix for #8
1 parent 38ac1b3 commit c4220ef

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn get_resource_dir<R: Runtime>(app: &AppHandle<R>) -> PathBuf {
7070
.unwrap_or_default()
7171
}
7272

73-
fn get_current_dir() -> PathBuf {
73+
fn get_src_python_dir() -> PathBuf {
7474
std::env::current_dir().unwrap().join("src-python")
7575
}
7676

@@ -79,9 +79,16 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
7979
init_and_register(vec![])
8080
}
8181

82+
fn cleanup_path_for_python(path: &str) -> String {
83+
path.replace("\\\\?\\", "").replace("\\", "/")
84+
}
85+
8286
fn init_python(code: String, dir: PathBuf) {
8387
#[allow(unused_mut)]
84-
let mut sys_pyth_dir = vec![format!("\"{}\"", dir.to_string_lossy())];
88+
let mut sys_pyth_dir = vec![format!(
89+
"\"{}\"",
90+
cleanup_path_for_python(&dir.canonicalize().unwrap().to_string_lossy())
91+
)];
8592
#[cfg(feature = "venv")]
8693
{
8794
let venv_dir = dir.join(".venv").join("lib");
@@ -91,7 +98,12 @@ fn init_python(code: String, dir: PathBuf) {
9198
let site_packages = entry.path().join("site-packages");
9299
// use first folder with site-packages for venv, ignore venv version
93100
if Path::exists(site_packages.as_path()) {
94-
sys_pyth_dir.push(format!("\"{}\"", site_packages.to_string_lossy()));
101+
sys_pyth_dir.push(format!(
102+
"\"{}\"",
103+
cleanup_path_for_python(
104+
&site_packages.canonicalize().unwrap().to_string_lossy()
105+
)
106+
));
95107
break;
96108
}
97109
}
@@ -106,6 +118,7 @@ sys.path = sys.path + [{}]
106118
sys_pyth_dir.join(", "),
107119
code
108120
);
121+
dbg!(&sys_pyth_dir);
109122
py_lib::run_python_internal(path_import, "main.py".into())
110123
.unwrap_or_else(|e| panic!("Error initializing main.py:\n\n{e}\n"));
111124
}
@@ -132,12 +145,13 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
132145
println!(
133146
"Warning: 'src-tauri/main.py' seems not to be registered in 'tauri.conf.json'"
134147
);
135-
dir = get_current_dir();
148+
dir = get_src_python_dir();
136149
code = std::fs::read_to_string(dir.join("main.py")).unwrap_or_default();
137150
}
138151
if code.is_empty() {
139152
println!("ERROR: Error reading 'src-tauri/main.py'");
140153
}
154+
dir = dir.canonicalize().unwrap();
141155
init_python(code, dir);
142156
for function_name in python_functions {
143157
py_lib::register_function_str(function_name.into(), None).unwrap();

0 commit comments

Comments
 (0)