from pack.mod import f
How to get from object f information about import - 'pack.mod'
I can get it using f.__module__
but if function def in module where i get this attribute (f.__module__
) it return '__main__'
. But i need real path here - 'pack.mod'
I found this way to get this information:
inspect.getmodule(f).__file__
then i can sub start path from sys.path
, replace /
on .
and get path like - 'pack.mod'
But may be exist some more convenient way?
In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.
append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.
Packages support one more special attribute, __path__ . This is initialized to be a list containing the name of the directory holding the package's __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.
What inspect.getmodule(f)
does internally, per inspect.py's sources, is essentially sys.modules.get(object.__module__)
-- I wouldn't call using that code directly "more convenient", though (beyond the "essentially" part, inspect
has a lot of useful catching and correction of corner cases).
Why not call directly inspect.getsourcefile(f)?
Edit: reading between the lines it seems the OP is trying to do something like
python /foo/bar/baz/bla.py
and within bla.py
(which is thus being run as __main__
) determine "what from
or import
statement could a different main script use to import this function from within me?".
Problem is, the question is ill-posed, because there might not be any such path usable for the purpose (nothing guarantees the current main script's path is on sys.path
when that different main script gets run later), there might be several different ones (e.g. both /foo/bar
and /foo/bar/baz
might be on sys.path
and /foo/bar/baz/__init__.py
exist, in which case from baz.bla import f
and from bla import f
might both work), and nothing guarantees that some other, previous sys.path
item might not "preempt" the import attempt (e.g., say /foo/bar/baz
is on sys.path
, but before it there's also /fee/fie/foo
, and a completely unrelated file /fee/fie/foo/bla.py
also exists -- etc, etc).
Whatever the purpose of this kind of discovery attempt, I suggest finding an alternative architecture -- e.g., one where from baz.bla import f
is actually executed (as the OP says at the start of the question), so that f.__module__
is correctly set to baz.bla
.
You want the __name__
attribute from __module__
:
In [16]: import inspect
In [17]: inspect.getmodule(MyObject).__name__
Out[17]: 'lib.objects.MyObject'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With