Conversation
|
关于 pyo3 在实际使用的时候可能会遇到的环境问题 (比如运行在一个启动了venv的终端里,但是并不使用) |
Comment on lines
+172
to
+188
| pyo3::append_to_inittab!(ranimpy_module); | ||
| pyo3::prepare_freethreaded_python(); | ||
| Python::with_gil(|py| { | ||
| let sys = PyModule::import(py, "sys")?; | ||
|
|
||
| let executable = sys.getattr("executable")?; | ||
| let path = sys.getattr("path")?; | ||
| let version = sys.getattr("version")?; | ||
| // if args.len() == 2 { | ||
| // let venv_dir = Path::new(&args[1]); | ||
| let site_packages_path = dunce::canonicalize("../../.venv/Lib/site-packages").unwrap(); | ||
| path.call_method1("append", (site_packages_path.to_str().unwrap(),)) | ||
| .unwrap(); | ||
| // } | ||
| println!("pyo3 sys.executable: {}", executable); | ||
| println!("pyo3 sys.path: {}", path); | ||
| println!("pyo3 sys.version: {}", version); |
There was a problem hiding this comment.
这里的环境处理应该将 173 的 pyo3::prepare_freethreaded_python();
替换为手动使用 ffi 直接对 PyConfig 进行设置
现在这样的实现并不能实现所需的功能
Comment on lines
+212
to
+229
| fn test_python_module_methods() -> PyResult<()> { | ||
| pyo3::append_to_inittab!(ranimpy_module); | ||
| pyo3::prepare_freethreaded_python(); | ||
| Python::with_gil(|py| { | ||
| let sys = PyModule::import(py, "sys")?; | ||
|
|
||
| let executable = sys.getattr("executable")?; | ||
| let path = sys.getattr("path")?; | ||
| let version = sys.getattr("version")?; | ||
| // if args.len() == 2 { | ||
| // let venv_dir = Path::new(&args[1]); | ||
| let site_packages_path = dunce::canonicalize("../../.venv/Lib/site-packages").unwrap(); | ||
| path.call_method1("append", (site_packages_path.to_str().unwrap(),)) | ||
| .unwrap(); | ||
| // } | ||
| println!("pyo3 sys.executable: {}", executable); | ||
| println!("pyo3 sys.path: {}", path); | ||
| println!("pyo3 sys.version: {}", version); |
Owner
Author
收到!十分感谢大佬,当时确实受这个问题折磨了很久ww(/。\) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.