[Tutor] querying the name of a calling python file

Garry Willgoose garry.willgoose at newcastle.edu.au
Fri Nov 5 12:15:26 CET 2010


For a while now I have been using the command below in a python file to determine the name of the python file that I use to launch an application (i.e.IF I go 'python junk.py' I want to get 'junk.py'). The command I have use that I came across somewhere some time ago was

sys.modules['__main__'].__dict__['__file__']

Now this works fine for the standard python interpretter but when I use ipython (I'm using the enthought distribution 6.1) this command returns

/Library/Frameworks/Python.framework/Versions/6.1/lib/python2.6/site-packages/IPython/FakeModule.py

I discovered this when  I was tracking down inconsistencies/errors between the matplotlib in the enthought distribution depending on whether you use python or ipython. My question is this: Is the way I'm getting the file name the recommended way or did I pick up a bit of flaky advice? 

I'm puzzled by the matplot lib errors (the y axes on imgshow() show problems in python but appear to 1st order to be OK in ipython) ... but I'll pursue that at the Matplotlib, enthought and ipython forums once I've been able to run a broader cross-section of my codes. 


====================================================================
Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574 (Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose at newcastle.edu.au; g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
personal webpage: www.telluricresearch.com/garry
====================================================================
"Do not go where the path may lead, go instead where there is no path and leave a trail"
                          Ralph Waldo Emerson
====================================================================







More information about the Tutor mailing list