cython: [BUG] __file__ is not the path of pyx file, it points to compiled shared library instead.

Describe the bug

The __file__ value in pyx (cython) file is the path to shared library, it is not point to pyx file.

Code to reproduce the behaviour:

print(__file__)

Expected behaviour

It should point to .pyx file or .py file.

OS

No response

Python version

No response

Cython version

No response

Additional context

One possible solution is to manually modify the value of file in Importer and Loader before execute the module.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I prefer that __file__ point to the actual file imported

Note that this is not even true for Python. The variable now always points to the .py source file, regardless of whether the .py, .pyc or .pyo file was loaded. The reasoning is that the .py file is the most relevant of the three files for users, the only one that users can actually look into and read. I could argue that the same applies to Cython generated modules, in a way.

satisfy user demand

For another data point, I prefer that __file__ point to the actual file imported. If that is a compiled C-extension, it should point to the shared object. It does not interest me so much if the compiled c-extension came from cython, PyO3, pybind11, or hand-written C-code, I want to know what is being loaded into the interpreter.