Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Import a Python module without adding it to the local namespace

What I’d like to do

I’d like to import a Python module without adding it to the local namespace.

In other words, I’d like to do this:

import foo
del foo

Is there a cleaner way to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why I want to do it

The short version is that importing foo has a side effect that I want, but I don’t really want it in my namespace afterwards.

The long version is that I have a base class that uses __init_subclass__() to register its subclasses. So base.py looks like this:

class Base:
    _subclasses = {}

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        cls._subclasses[cls.__name__] = cls

    @classmethod
    def get_subclass(cls, class_name):
        return cls._subclasses[class_name]

And its subclasses are defined in separate files, e.g. foo_a.py:

from base import Base

class FooA(Base):
    pass

and so on.

The net effect here is that if I do

from base import Base

print(f"Before import: {Base._subclasses}")

import foo_a
import foo_b

print(f"After import: {Base._subclasses}")

then I would see

Before import: {}
After import: {'FooA': <class 'foo_a.FooA'>, 'FooB': <class 'foo_b.FooB'>}

So I needed to import these modules for the side effect of adding a reference to Base._subclasses, but now that that’s done, I don’t need them in my namespace anymore because I’m just going to be using Base.get_subclass().

I know I could just leave them there, but this is going into an __init__.py so I’d like to tidy up that namespace.

del works perfectly fine, I’m just wondering if there’s a cleaner or more idiomatic way to do this.

>Solution :

If you want to import a module without assigning the module object to a variable, you can use importlib.import_module and ignore the return value:

import importlib

importlib.import_module("foo")

Note that using importlib.import_module is preferable over using the __import__ builtin directly for simple usages. See the builtin documenation for details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading