1

When debugging, from module import * comes in handy. However, it only seems to import the public functions. Functions that start with _ are not imported but I would like them to be.

How do I get all private functions to also be imported?

Edit I'm fully aware import * is bad practice in general, but sometimes I need to identify and fix an urgent production issue and using import * when debugging interactively saves crucial time.

1
  • 5
    The solution is not to use import * at all, but import the module, then refer to whatever you need as module._foo. Commented Mar 22, 2017 at 11:28

3 Answers 3

2

I'd suggest

import module
globals().update(vars(module) | globals())

Of-course that's Python 3.9 syntax, in Python 3.5+ one can use:

globals().update({**vars(module), **globals()})

Note that I'm not just using globals().update(vars(module)), which will overwrite all global variables, including import-related module attributes like __file__, __loader__, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

from module import * defaults to only importing all public names. Specify an __all__ sequence consisting of names (as strings) in your module globals if you need to explicitly control what names are imported. You can add private names to that list:

__all__ = ['PublicClass', 'public_function', '_private_function']

However, a better alternative is not to use from module import * at all. Code that relies on private names of a sibling module should just import those explicitly.

Note that using from module import * is generally frowned upon, as this makes it much harder to verify your code, both by human readers and by linters (static code checkers), as you can't determine what names are imported and which ones are missing.

7 Comments

Thanks, but what if I don't have write access to the source code?
@mchen: then just use explicit imports. from module import _private_name.
@mchen: if you must you can set module.__all__, but is adding one import line really that tedious?
This answer assumes that there's 'just one single function' that needs to be imported. In reality one may want to import all private names, even those that weren't yet invented. The use case is when creating a pass-through module, that would override some functions, but keep everything else intact. Code that's out of ours control may depend on those private names. Also add forward compatibility and DRY to the mix so you can't list the private names one-by-one.
@YakovGalka nowhere in my answer is there an assumption about a single import. The point is that private names are excluded for a reason and if the module follows the Python conventions then a proxy module doesn’t need to import those names anyway. A your proxy module would reflect the __all__ list of the proxies module.
|
1

Adding to Martijn's answer... If you want that import * imports all the symbols defined in a module, including private ones, an easy way to do this (without explicitly enumerating the names) is to put this line at the end of the module:

__all__ = list(globals().keys())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.