jcubic
jcubic

Reputation: 66590

Is there a way to access parent modules of an imported module object?

I need to know if there is a way to access parent modules from submodules. If I import submodule:

from subprocess import types

I have types - is there some Python magic to get access to subprocess module from types? Something similar to this for classes ().__class__.__bases__[0].__subclasses__().

Upvotes: 13

Views: 19882

Answers (5)

sandejai
sandejai

Reputation: 989

Best way that worked for us was:

Let's say folder structure

src
 |demoproject
 |
 |--> uimodule--> ui.py
 |--> backendmodule --> be.py
 setup.py
                 
  1. Create installable package out of the project
  2. Have __init__.py in all the directory (module)
  3. create setup.py (keep in top level folder, here inside src)

Sample

from setuptools import setup, find_packages

    setup(
        name="demopackage",
        version="1",
        packages=find_packages(exclude=["tests.*", "tests"]),
        author='',
        author_email='',
        description="",
        url="",
        )
        
  1. From src folder, create installable package pip3 install .

  2. this will install a package --> demopackage

  3. Now from any of your module you can access any module, ex

  4. from ui.py to access be.py function calldb(), make below import

from demopackage.backendmodule.be import calldb
  1. and so on, when you a new folder into your project just add __init__.py in that folder and it will be accessible, just like above, but you have to execute pip3 install .

Upvotes: 0

kai
kai

Reputation: 1768

full_module_name = module.__name__
parent, _, sub = full_module_name.rpartition('.')
if parent:
    parent = import(parent, fromlist='dummy')

Upvotes: 0

Dorian B.
Dorian B.

Reputation: 1299

For posterity, I ran into this also and came up with the one liner:

import sys
parent_module = sys.modules['.'.join(__name__.split('.')[:-1]) or '__main__']

The or '__main__' part is just in case you load the file directly it will return itself.

Upvotes: 6

Tom Gruner
Tom Gruner

Reputation: 9635

I assume you are not inside the subprocess module already, you could do

import somemodule
children = dir(somemodule)

Then you could inspect the children of subprocess with the inspect module: http://docs.python.org/library/inspect.html

Maybe the getmodule method would be useful for you? http://docs.python.org/library/inspect.html#inspect.getmodule

import inspect
parent_module = inspect.getmodule(somefunction)
children = dir(parent_module)
package = parent_module.__package__

On my machine __package__ returns empty for 'types', but can be more useful for my own modules as it does return the parent module as a string

Upvotes: -1

Nicholas Riley
Nicholas Riley

Reputation: 44331

If you've accessed a module you can typically get to it from the sys.modules dictionary. Python doesn't keep "parent pointers" with names, particularly because the relationship is not one-to-one. For example, using your example:

>>> from subprocess import types
>>> types
<module 'types' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc'>
>>> import sys
>>> sys.modules['subprocess']
<module 'subprocess' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc'>

If you'll note the presence of types in the subprocess module is just an artifact of the import types statement in it. You just import types if you need that module.

In fact, a future version of subprocess may not import types any more, and your code will break. You should only import the names that appear in the __all__ list of a module; consider other names as implementation details.

So, for example:

>>> import subprocess
>>> dir(subprocess)
['CalledProcessError', 'MAXFD', 'PIPE', 'Popen', 'STDOUT', '_PIPE_BUF', '__all__', '__builtins__', '__doc__',
 '__file__', '__name__', '__package__', '_active', '_cleanup', '_demo_posix', '_demo_windows', '_eintr_retry_call',
 '_has_poll', 'call', 'check_call', 'check_output', 'errno', 'fcntl', 'gc', 'list2cmdline', 'mswindows', 'os',
 'pickle', 'select', 'signal', 'sys', 'traceback', 'types']
>>> subprocess.__all__
['Popen', 'PIPE', 'STDOUT', 'call', 'check_call', 'check_output', 'CalledProcessError']

You can see that most of the names visible in subprocess are just other top-level modules that it imports.

Upvotes: 7

Related Questions