pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

`py.run`: `__name__` is `"builtins"`, should be `"__main__"`

Open nschloe opened this issue 10 months ago • 1 comments

Bug Description

Python's exec() has __name__ set to __main__:

exec("print(__name__)")
__main__

When running the same code through PyO3's py.run(), it'll give builtins.

Steps to Reproduce

See above.

Backtrace


Your operating system and version

Linux 6.12.13-1-lts

Your Python version (python --version)

Python 3.11.11

Your Rust version (rustc --version)

rustc 1.85.0 (4d91de4e4 2025-02-17) (Arch Linux rust 1:1.85.0-1)

Your PyO3 version

v0.23.4

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

uv venv

Additional Info

No response

nschloe avatar Feb 22 '25 13:02 nschloe

Cannot reproduce in newer version (Python 3.13.2, PyO3 0.24.2, rustc 1.86.0 ) - it gives __main__ as expected

Cheukting avatar Apr 22 '25 07:04 Cheukting