Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Is it possible to make the imports within `__init__.py` visible for python `help()` command?

Suppose I have a module:

mymodule/example.py:

def add_one(number):
    return number + 1

And mymodule/__init__.py:

from .example import *

foo = "FOO"

def bar():
    return 1

Now I see the function at the root of mymodule:

>>> import mymodule
>>> mymodule.add_one(3)
4
>>> mymodule.foo
'FOO'

Also, I see imported add_one through dir along with example:

>>> dir(mymodule)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'add_one', 'bar', 'example', 'foo']

But when I type help(mymodule) I see only example, foo and bar, but not the imported add_one:

Help on package mymodule:

NAME
    mymodule

PACKAGE CONTENTS
    example

FUNCTIONS
    bar()

DATA
    foo = 'FOO'

But I can call add_one() as the root function of mymodule. Is it possible to see it in help as root function?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From the source code of help() (docmodule under pydoc.py)

        for key, value in inspect.getmembers(object, inspect.isroutine):
            # if __all__ exists, believe it.  Otherwise use old heuristic.
            if (all is not None or
                inspect.isbuiltin(value) or inspect.getmodule(value) is object):
                if visiblename(key, all, object):
                    funcs.append((key, value))

The important part is inspect.getmodule(value) is object), this is where values that are not directly part of the object itself are dropped

You can add this line

__all__ = ['bar', 'add_one', 'FOO']

to your __init__.py file, this way the help function will make sure to include these objects in help

Keep in mind that if you do this anything you don't include in this list will not be included

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda