PyO3 / PyO3/pyo3

PyO3 Fails to Import DLL in Python Virtual Environment on Windows

Open
#3,589 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description

When running cargo test within a python virtual environment on Windows, PyO3 fails to import DLLs, resulting in a STATUS_DLL_NOT_FOUND error. This occurs even after attempting to use --no-default-features to exclude potential issues caused by default feature sets.

Additional Attempt to Resolve:
Following the suggestion from the PyO3 FAQ, adding the directory containing the Python DLL (from the existing Python environment) to the PATH environment variable was attempted. However, this leads to importing modules from the existing Python environment instead of the virtual environment.

Steps to Reproduce
  1. Set up a python virtual environment on Windows.

  2. Create a minimal Rust project using PyO3.

  3. Add a simple test case in src/lib.rs, for example:

    #[test]
    fn test() {
        pyo3::prepare_freethreaded_python();
        Python::with_gil(|py| {
            let _numpy = PyModule::import(py, "numpy").unwrap();
        });
    }
  1. Run the project using cargo test.

  2. Observe the error output.

Backtrace

No response

Your operating system and version

Win 10

Your Python version (python --version)

Python 3.11.0

Your Rust version (rustc --version)

rustc 1.71.0-nightly (4a59ba4d5 2023-05-12)

Your PyO3 version

0.19.2

How did you install python? Did you use a virtualenv?

python.org install
python -v venv venv

Additional Info

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal project and the test in src/lib.rs, then reproduce the failure by running cargo test on Windows inside a Python 3.11 virtual environment. Read the linked PyO3 FAQ guidance and compare DLL loading with and without the existing Python directory on PATH. Done means the test imports numpy from the virtual environment without STATUS_DLL_NOT_FOUND or selecting the wrong environment.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.