PyO3 / PyO3/pyo3

Unable to open file with long path on Windows

Open
#5,515 0 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

The behavior of pyo3 is inconsistent with the behavior of Python when opening files with extended paths over 256 characters long on Windows. When trying to execute the code via pyo3, it returns a FileNotFoundError. Long paths are enabled in the registry.

Steps to Reproduce

An example to reproduce the behavior, along with comparison to the python command follows. It requires python.exe to be accessible in PATH. If that's not the case, the PYTHON const should be updated to point to your python executable. The python command exits with code 0 (no exception), while the pyo3 part fails with FileNotFoundError.

src/main.rs

use pyo3::Python;
use std::ffi::CString;
use std::process::Command;

const PYTHON: &str = "python.exe";

fn main() {
    let tmpdir = tempfile::tempdir().unwrap();
    let mut pathbuf = tmpdir.path().to_path_buf();
    for i in 0..10 {
        pathbuf.push(i.to_string().repeat(30));
    }
    std::fs::create_dir_all(&pathbuf).unwrap();

    pathbuf.push("file.txt");
    std::fs::write(&pathbuf, "file contents").unwrap();

    assert!(pathbuf.as_path().as_os_str().len() > 256);

    let code = format!("open({:?})", pathbuf.to_str().unwrap());

    let cmd_result = Command::new(PYTHON)
        .args(["-c", &code])
        .spawn()
        .unwrap()
        .wait()
        .unwrap();

    let ccode = CString::new(code.as_str()).unwrap();
    let pyo3_result = Python::attach(|py| py.run(&ccode, None, None));

    println!(" cmd result: {cmd_result:?}");
    println!("pyo3 result: {pyo3_result:?}");
}
pyo3_result = Python::attach(|py| py.run(&ccode, None, None));

    println!(" cmd result: {cmd_result:?}");
    println!("pyo3 result: {pyo3_result:?}");
}

Cargo.toml

[package]
name = "pyo3_long_path_test"
version = "0.1.0"
edition = "2024"

[dependencies]
dirs = "6.0.0"
pyo3 = { version = "0.26.0", features = ["auto-initialize"] }
tempfile = "3.23.0"
Backtrace

Your operating system and version

Windows 11 Pro 23H2

Your Python version (python --version)

Python 3.13.1

Your Rust version (rustc --version)

rustc 1.90.0 (1159e78c4 2025-09-14)

Your PyO3 version

0.26.0

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

python.org installer

no virtualenv

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 Windows reproduction in src/main.rs and its dependencies in Cargo.toml, then run it with Python 3.13 and PyO3 0.26 on Windows. Compare the embedded PyO3 result with the external Python command for a path longer than 256 characters; done means the embedded call opens the file without FileNotFoundError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
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.