[SOLVED] Is there a way to import all functions(using *) from a file without the imports of that file?

Issue

I have two .py files

  1. Helper.py
  2. SomeClass.py

lets say for the example that both files import sys

I want to import into SomeClass.py the Helper.py like this: from helper.py import * (because I need all functions in it)

but I don’t want to remove from any of the files the import sys because these two files aren’t really go hand by hand.

is there a way to to import * and exclude the imports/from…import of another file?

Solution

You can define the __all__ module.

In your Helper.py file, add a line

__all__ = ["foo", "bar"]  # All the objects you want to export

Then in SomeClass.py just use from helper.py import *, this will only import what is specified in __all__.

Answered By – Raida

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.