Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access parent modules in Python

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__().

like image 452
jcubic Avatar asked Mar 12 '11 23:03

jcubic


People also ask

How to import a module from parent directory in Python?

Once the parent directory is declared a package, we can import the module using the relative package approach. Suppose we have the following directory tree. parent_parent_directory/ parent_directory/ mymodule.py __init__.py current_directory/ currentmodule.py mymodule.py __init__.py

How to access the methods of a parent class in Python?

This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class. Note: For more information, refer to Inheritance in Python. An inner class or nested class is a defined inside the body of another class.

How to find the parent pointer of a module in Python?

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: If you'll note the presence of types in the subprocess module is just an artifact of the import types statement in it.

What names should I import into a Python module?

You should only import the names that appear in the __all__ list of a module; consider other names as implementation details.


2 Answers

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.

like image 151
Nicholas Riley Avatar answered Sep 22 '22 09:09

Nicholas Riley


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.

like image 40
Dorian B. Avatar answered Sep 22 '22 09:09

Dorian B.