Question: ```
mModule.__name__

```

Will helpfully return the module's name when imported. But when run as a script it will (also helpfully) return

```
 __main__

```

Is there a simple way to get the file name of a module currently being run as a script?

Answer: 
I think [`sys.argv[0]`](http://docs.python.org/2/library/sys.html) is your best bet - it should work from any module, not only the main module.

Note that this may or may not be a full path and you'll have to test for `"-c"`, the empty string, and otherwise use `os.path.abspath` to get a full path.
You can use `__file__`:

> 
> \_\_file\_\_ is the pathname of the file from which the module was loaded, if it was loaded from a file.
> 
> 
> 

This contains an absolute path name, so, in order to get the script file name use:

```
import os
print os.path.basename(__file__)

```

`__file__` works in any case: the file was executed as a script, the file was imported as a module.