[Python-Dev] __file__ is not always an absolute path
Ezio Melotti
ezio.melotti at gmail.com
Sat Feb 6 21:49:05 CET 2010
In #7712 I was trying to change regrtest to always run the tests in a
temporary CWD (e.g. /tmp/@test_1234_cwd/).
The patches attached to the issue add a context manager that changes the
CWD, and it works fine when I run ./python -m test.regrtest from trunk/.
However, when I try from trunk/Lib/ it fails with ImportErrors (note
that the latest patch by Florent Xicluna already tries to workaround the
problem). The traceback points to "the_package = __import__(abstest,
globals(), locals(), [])" in runtest_inner (in regrtest.py), and a
"print __import__('test').__file__" there returns 'test/__init__.pyc'.
This can be reproduced quite easily:
trunk$ ./python
Python 2.7a2+ (trunk:77941M, Feb 3 2010, 06:40:49)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> os.getcwd()
'/home/wolf/dev/trunk'
>>> import test
>>> test.__file__ # absolute
'/home/wolf/dev/trunk/Lib/test/__init__.pyc'
>>> os.chdir('/tmp')
>>> test.__file__
'/home/wolf/dev/trunk/Lib/test/__init__.pyc'
>>> from test import test_unicode # works
>>> test_unicode.__file__
'/home/wolf/dev/trunk/Lib/test/test_unicode.pyc'
>>>
[21]+ Stopped ./python
trunk$ cd Lib/
trunk/Lib$ ../python
Python 2.7a2+ (trunk:77941M, Feb 3 2010, 06:40:49)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> os.getcwd()
'/home/wolf/dev/trunk/Lib'
>>> import test
>>> test.__file__ # relative
'test/__init__.pyc'
>>> os.chdir('/tmp')
>>> from test import test_unicode # fails
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name test_unicode
Is there a reason why in the second case test.__file__ is relative?
More information about the Python-Dev
mailing list