My question is that why do i have to write all of this code to get the filename:
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
Let's take it piece by piece.
First, the print is 1 character longer than PHP's echo. I doubt you have a problem with that.
Next, you have to use os.path.dirname instead of just dirname because Python allows code to be organized into modules, and allows you to reference things by qualified name. This is why Python can have functions like gzip.open and io.open that have the same name. But it doesn't force you to use qualified names if you don't want to; if you'd done, say, from os.path import dirname instead of import os, you could just use dirname.
Next, you need the abspath because you may have a relative path. This is actually exactly the same as in PHP. It's just that you're used to running PHP from a web browser setup that always runs your scripts by absolute path. You can do the same in Python if you want.
Finally:
There are suggestions that dirname(__file__) should work in python but it returns an empty string when running via the command line.
Not true.
$ cat > test.py
print __file___
$ python test.py
test.py
If by "running via the command line" you meant "using the interactive interpreter", of course, there is no __file__, because there is no file (except the terminal stdin).
Or, if you're printing the dirname of a relative path like test.py, of course that's always going to be an empty string, just as it would be in PHP.
Putting it all together:
$ cat > test.py
from os.path import dirname
print dirname(__file__)
$ python $(abspath test.py)
/Users/abarnert/src/test
Just like PHP.