[Tutor] querying the name of a calling python file

शंतनू shantanoo at gmail.com
Fri Nov 5 12:24:17 CET 2010


On 05-Nov-2010, at 4:45 PM, Garry Willgoose wrote:

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

Try the following code:

===
#!/usr/bin/env python
import sys
print sys.argv
===



More information about the Tutor mailing list