Hide external modules when importing a module (e.g. regarding code-completion)

后端 未结 4 395
慢半拍i
慢半拍i 2020-12-09 09:11

I have several modules in one package (a kind of a toolkit), which I use in my projects. The structure looks like this:

the_toolkit:
    __init__.py
    basi         


        
相关标签:
4条回答
  • 2020-12-09 09:30

    An alternative solution I came up with is to implement a short API class in the __init__ file which can make use of the __all__ variable (which may already be defined for import * purposes). Something like this in __init__.py:

    import matrix_kit
    
    class _matrix_kit_API:
        def __init__(self):
            for func in matrix_kit.__all__:
                setattr(self, func, eval(f"matrix_kit.{func}"))
    
    matrix_kit = _matrix_kit_API()
    
    0 讨论(0)
  • 2020-12-09 09:38

    I had the same problem, and solved it by adding a leading underscore to all my imports. It doesn't look brilliant, but it achieves what you're after.

    from __future__ import division as _division
    import numpy as _np
    import pandas as _pd
    

    Only the stuff that starts without an underscore is imported when you import the module using from .. import *. or when you use tab-completion in IPython or spyder.

    0 讨论(0)
  • 2020-12-09 09:39

    you may get some mileage out of __all__

    0 讨论(0)
  • 2020-12-09 09:46

    I've come to like and use this approach. Starting with your project structure, we will hide all the content of matrix_kit that you don't want to show.

    the_toolkit:
        __init__.py
        basic_io.py
        simple_math.py
        matrix_kit.py
        ...
    

    Move matrix_kit.py into a package with the same name, and place one underscore in the beginning of the module's name:

    the_toolkit:
        __init__.py
        basic_io.py
        simple_math.py
        matrix_kit:
            __init__.py
            _matrix_kit.py
        ...
    

    Now in _matrix_kit.py add a global variable __all__ = [...] listing the names of all functions/classes/variables you want to be visible. Then, add the following line to matrix_kit/__init__.py:

    from ._matrix_kit import *
    

    It will only import the stuff listed in the __all__ attribute of the module. Then, while importing matrix_kit like you've done before, you will actually import the __init__.py and its content.

    0 讨论(0)
提交回复
热议问题