Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__file__ variable not available in module init #1035

Closed
robertwb opened this issue Jan 8, 2010 · 4 comments
Closed

__file__ variable not available in module init #1035

robertwb opened this issue Jan 8, 2010 · 4 comments

Comments

@robertwb
Copy link
Contributor

robertwb commented Jan 8, 2010

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'

Migrated from http://trac.cython.org/ticket/475

@robertwb
Copy link
Contributor Author

robertwb commented Jan 8, 2010

@robertwb changed description from

----------- 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'

to

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'

commented

@robertwb
Copy link
Contributor Author

robertwb commented Aug 1, 2012

bfroehle changed cc to bfroehle
commented

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.

@robertwb
Copy link
Contributor Author

robertwb commented Aug 9, 2012

scoder commented

See http://bugs.python.org/issue13429

@robertwb robertwb added this to the wishlist milestone Aug 16, 2016
@scoder scoder modified the milestones: 0.27, wishlist Sep 5, 2017
@scoder
Copy link
Contributor

scoder commented Sep 5, 2017

Fixed with Py3.5 (PEP 489) in #1715.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants