This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vinay.sajip
Recipients ncoghlan, vinay.sajip
Date 2012-12-20.13:37:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1356010622.32.0.479096681415.issue16737@psf.upfronthosting.co.za>
In-reply-to
Content
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__
History
Date User Action Args
2012-12-20 13:37:02vinay.sajipsetrecipients: + vinay.sajip, ncoghlan
2012-12-20 13:37:02vinay.sajipsetmessageid: <1356010622.32.0.479096681415.issue16737@psf.upfronthosting.co.za>
2012-12-20 13:37:02vinay.sajiplinkissue16737 messages
2012-12-20 13:37:02vinay.sajipcreate