Ticket #475 (new defect)

Opened 4 years ago

Last modified 21 months ago

__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) (diff)


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

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

Change History

Changed 4 years ago by robertwb

  • description modified (diff)

Changed 21 months 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.