-
Notifications
You must be signed in to change notification settings - Fork 834
Running in virtualenvs on Windows #1896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
after some investigation: running this code in both linux and windows #[test]
pub fn test_py() {
pyo3::prepare_freethreaded_python();
pyo3::Python::with_gil(|py| {
let sys = py.import("sys").unwrap();
let version: String = sys.getattr("version").unwrap().extract().unwrap();
println!("Python version: {}", version);
let prefix: String = sys.getattr("prefix").unwrap().extract().unwrap();
println!("Python prefix: {}", prefix);
let executable: String = sys.getattr("executable").unwrap().extract().unwrap();
println!("Python executable: {}", executable);
let python_path = sys.getattr("path").unwrap();
let python_path: Vec<String> = python_path.extract().unwrap();
println!("Python path: {:?}", python_path);
let os = py.import("os").unwrap();
let path: String = os.getattr("getcwd").unwrap().call0().unwrap().extract().unwrap();
println!("Current directory: {}", path);
});
} I found that on linux the executable resolves to "python",
while on windows the executable resolves to "test.exe"
So they resolve to different prefix. Change initial code to things like this would work let output = Command::new("python").env("PYTHONIOENCODING", "utf-8").arg("-c").arg("import sys; print(sys.executable)").output()?;
let executable = String::from_utf8(output.stdout).unwrap();
let mut config = unsafe { std::mem::zeroed() };
unsafe {
ffi::PyConfig_InitIsolatedConfig(&mut config);
ffi::PyConfig_SetString(&mut config, &mut config.executable, executable.to_wchar().as_mut_ptr());
ffi::Py_InitializeFromConfig(&config)
} |
See also #1835
If I create a new virtualenv on Windows, install
tomli
into it, and run the following program:Then this fails to import
tomli
, despite building correctly with the virtualenv. On linux (at least Ubuntu 20.04) the equivalent works correctly.I suspect we could investigate how
pyo3::prepare_freethreaded_python
sets up the interpreter - presumably on Windows it fails to detect the virtualenv, but on Linux it's fine.The text was updated successfully, but these errors were encountered: