[New-bugs-announce] [issue16737] Different behaviours in script run directly and via runpy.run_module

Vinay Sajip report at bugs.python.org
Thu Dec 20 14:37:02 CET 2012


New submission from Vinay Sajip:

If a script is run directly, the value of __file__ in it is relative to the current directory. If run via runpy.run_module, the value of __file__ is an absolute path. This is a problem in certain scenarios - e.g. if the script is a distribution's setup.py, a lot of distributions (rightly or wrongly) assume that the __file__ in setup.py will be relative, and mess up if it's absolute.

Example:
# script.py
print(__file__, __name__)

#runscript.py
import runpy
runpy.run_module('script', run_name='__main__')

Example output (2.7):
$ python script.py
('script.py', '__main__')
$ python runscript.py
('/home/vinay/projects/scratch/script.py', '__main__')

Example output (3.2):
$ python3.2 script.py
script.py __main__
$ python3.2 runscript.py
/home/vinay/projects/scratch/script.py __main__

----------
components: Library (Lib)
messages: 177814
nosy: ncoghlan, vinay.sajip
priority: normal
severity: normal
status: open
title: Different behaviours in script run directly and via runpy.run_module
type: behavior
versions: Python 2.7, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16737>
_______________________________________


More information about the New-bugs-announce mailing list