tags:

views:

38

answers:

1

If one installs a python package using setuptools, then executes a method in that package from a standard python script, is it possible to get the file path of the calling/executing file?

For instance, the file I'm executing is /usr/foo/bar.py, which looks like this:

import baz
baz.get_current_path()
# should print /usr/foo/bar.py

and the package baz has been installed using setuptools and is located in that magical place all python packages are installed when they've been good little packages.

Both __file__ and import inspect; inspect.currentframe().f_code.co_filename return the path of the package'd file.

Is this possible?

+1  A: 

Use inspect.getouterframes() or inspect.stack(), then get the filename from the calling frame.

nezroy
`import inspect; inspect.stack()[-1][0].f_code.co_filename` did the trick. Thanks!
digitala