pyo3: Can not link to virtual environment on OS X
๐ Bug Reports
I can not link to the correct virtual environment in OS X. Instead, the system Python is always used.
๐ Environment
- Your operating system and version: OS X Big Sur 11.5
- Your python version: 3.8.11
- How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: Python was installed with homebrew via
brew install python@3.8A virtual environment was created withpython3 -m venv redvoxand is located at /Users/anthony/venvs/redvox - Your Rust version (
rustc --version): rustc 1.56.0-nightly (9c25eb7aa 2021-07-25) - Your PyO3 version: 14.1
- Have you tried using latest PyO3 main (replace
version = "0.x.y"withgit = "https://github.com/PyO3/pyo3")?:
No
๐ฅ Reproducing
- Activate the virtual environment
source /Users/anthony/venvs/redvox/bin/activate - Ensure matplotlib is installed
pip install matplotlib - Run the rust code
cargo run
use pyo3::prelude::*;
use std::io::Result;
fn main() -> Result<()> {
let src = r#"
import sys
print(sys.executable, sys.path)
import matplotlib
"#;
Python::with_gil(|py| {
py.run(src, None, None).unwrap();
});
Ok(())
}
The following shows how I activate the virtual environment and attempt to call the rust code. The error message I receive is also included.
anthony@REDVOX-00-MACBOOK-PRO rust-sandbox % source ~/venvs/redvox/bin/activate
(redvox) anthony@REDVOX-00-MACBOOK-PRO rust-sandbox % python3
Python 3.8.11 (default, Jun 29 2021, 03:08:07)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> exit()
(redvox) anthony@REDVOX-00-MACBOOK-PRO rust-sandbox % cargo run
Compiling pyo3 v0.14.1
Compiling rust-sandbox v0.1.0 (/Users/anthony/Development/rust-sandbox)
Finished dev [unoptimized + debuginfo] target(s) in 4.63s
Running `target/debug/rust-sandbox`
/Users/anthony/Development/rust-sandbox/target/debug/rust-sandbox ['/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: PyErr { type: <class 'ModuleNotFoundError'>, value: ModuleNotFoundError("No module named 'matplotlib'"), traceback: Some(<traceback object at 0x100ddbbc0>) }', src/main.rs:12:33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Iโve tried setting PYTHONHOME and PYTHONPATH. Neither of these have worked for me. The system Python is always used no matter what I try. How can I get pyo3 to work with my virtual environment on OS X?
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 5
- Comments: 17 (8 by maintainers)
Something like this could work?
Hiya, Iโm experiencing the same issue using poetry. The โsolutionโ is to append to the
PYTHONPATHthesite-packagefolder` from the virtualenv lib, e.g.:This is of course very ugly and quite brittle. Can I suggest that as a safeguard, the user will be able to specify the env or virtual env (depending if running in system or virtual env of course) as a parameter passed to
prepare_freethreaded_python?The solution proposed by @messense works great, but does not work if you have editable installed packages (aka packages installed with
-e). I still cant find a solution for this. Is there anything that can be done to help deal with this issue?I spent yesterday fighting this and wanted to raise another difficulty I found. I was able to include my virtualenvโs site-packages by setting
PYTHONPATHto$VIRTUAL_ENV/lib/python3.$MINOR_VERSION/site-packages. However, the system site-packages was still included, so my sys.path ended up looking likeThis leads to unexpected behavior like successfully importing a package that only exists in the main python installation but not in the virtualenv.
Next step was to use the env var PYTHONPATH to set the paths to search (as an alternative to finding them via the venv). This seems to completely replace the sys.path rather than prepending it.
So as a follow on step I set the all the relevant paths using the PYTHONPATH env var.
This seems to work and I have bene able to load my python modules. But I do not believe it is the right way to use PYTHONPATH. I believe PYTHONPATH is supposed to be used to extend the search path of python modules not to define the complete search path.
I base this on how python is working:
So it points me to the sys.path is not being initialised correctly. I was not able to easily locate where in the code this was set. But if you can point me in the right direction I am happy to take a look.