Opened 5 years ago

Last modified 2 years ago

#475 new defect

__file__ variable not available in module init

Reported by: robertwb Owned by: somebody
Priority: major Milestone: wishlist
Component: Code Generation Keywords:
Cc: bfroehle

Description (last modified by robertwb)

test.pyx

try:
    print __file__
except NameError, ex:
    print "Got name error."

def get_file():
    return __file__
>>> import test
Got name error.
>>> test.get_file()
'test.so'

Change History (3)

comment:1 Changed 5 years ago by robertwb

  • Description modified (diff)

comment:2 Changed 2 years ago by bfroehle

  • Cc bfroehle added

As far as I know it's technically impossible to access __file__ when the module is being imported.

Regular Python modules are run with a dictionary which has been which has been pre-initialized with some values (__file__, __builtins__).

On the other hand, extension modules (like what Cython produces) have their init function run and _then_ have the __file__ attribute set. (See Python/importdl.c).

Since this cannot be fixed, I guess it should be documented and closed.

Note: See TracTickets for help on using tickets.