I have a utility package for Python projects I maintain and use. It’s a bundle of lightweight, common, junk-drawer tools (e.g. command parsing, app lifecycle management, document generation, &c &c). Currently, using this stuff is all very simple in one’s own project, but most modules written to work with it require this very irritating bit of boilerplate, like so:
from myproject.exporting import Exporter
# This is the irritating part:
exporter = Exporter(path=__file__)
export = exporter.decorator()
@export
def public_function(…):
""" This function has been exported, and is public. """
...
def private_function(…):
""" This function is private. """
...
# This bit does not really bother me:
__all__, __dir__ = exporter.all_and_dir()
This exporter interface lets one label classes, functions (even lambdas!) as public. These get added to generated module __all__ and __dir__(…) values, but the whole thing assists with documentation generation and other introspective things. This is besides the point, though, which is: having to do this hacky instatiation with a __file__ value is an eyesore. Linters hate __file__, so do most human Python programmers (including myself) and in general it feels like an unstable and antiquated way of doing things.
Is there a way of getting the value of __file__ in another manner? I am looking for a way to surface this datum via some other (hopefully less fragile) route.
My immediate thought is to have a module-level __getattr__(…) function, as I have had good results using these to provide instances of things on import – but, regardless of whether module __getattr__(…) is the best tool or not in this case, I am not sure about which Python introspection facilities would be warranted in their use.
Also, this answer has the basic info on the __file__ value, for those who are curious.
__file__in the first place? It doesn't look like you should need that info.Exporterclass is a per-project subclass of something calledExporterBaseand it knows the name and base path of the project in question. It uses__file__to determine the dotted path and the relative path of the module, which help the behind-the-scenes machinery with stuff like e.g. documentation generation, dependency introspection, qualified-name lookups, and many other things. That’s why I need the__file__datum. I’d like to get that datum some other way!import xyz.abc, you can do arbitrary things, including introspection, AST manipulation, etc. to the modulexyz.abcbefore it is fully initialised. Here's the public API oftypes.ModuleType(module) objects.__file__, you could callinspect.getabsfile(wrapped_func)to get the source path stackoverflow.com/questions/3718657/…