Is it possible to import a Python module exclusively for its effect?

Viewed 61

Essentially, I'm wondering if it's possible to do something like from .my_script import None in my package's __init__.py to execute my_script.py without introducing undesired variables into the namespace. I know you can do from . import my_script; del my_script - I thought it was possible there was a shortcut for this.

1 Answers

Consider Local import statements.

You could do the import foo inside a function, which means it will not clutter the global namespace. However, there is an option that does not require a function def:

import foo is roughly equivalent to this statement:

foo = __import__('foo', globals(), locals(), [], -1)

That is, it creates a variable in the current scope with the same name as the requested module, and assigns it the result of calling __import__() with that module name and a boatload of default arguments.

However, specifying that -1 there seems to no longer be supported in python3. According to the docs, the level indicates

0 means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module calling __import__().

So we will have to go with 0... or if it needs to search in a parent directory, a corresponding positive number.


As an example, I have two files in the same folder.

foo.py

print("Foo loaded")

bar.py

print("Hello")
__import__('foo', globals(), locals(), [], 0)
print("World")

output

> python bar.py
Hello
Foo loaded
World

If you want this to be a shortcut because you do this often, you could define your own, easier to use, function that takes the module name and internally just does this.

Related