Get the executing file's path from an installed package?
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.curr开发者_JS百科entframe().f_code.co_filename
return the path of the package'd file.
Is this possible?
Use inspect.getouterframes()
or inspect.stack()
, then get the filename from the calling frame.
精彩评论