Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a module given its name as string?

I'm writing a Python application that takes as a command as an argument, for example:

$ python myapp.py command1 

I want the application to be extensible, that is, to be able to add new modules that implement new commands without having to change the main application source. The tree looks something like:

myapp/     __init__.py     commands/         __init__.py         command1.py         command2.py     foo.py     bar.py 

So I want the application to find the available command modules at runtime and execute the appropriate one.

Python defines an __import__ function, which takes a string for a module name:

__import__(name, globals=None, locals=None, fromlist=(), level=0)

The function imports the module name, potentially using the given globals and locals to determine how to interpret the name in a package context. The fromlist gives the names of objects or submodules that should be imported from the module given by name.

Source: https://docs.python.org/3/library/functions.html#import

So currently I have something like:

command = sys.argv[1] try:     command_module = __import__("myapp.commands.%s" % command, fromlist=["myapp.commands"]) except ImportError:     # Display error message  command_module.run() 

This works just fine, I'm just wondering if there is possibly a more idiomatic way to accomplish what we are doing with this code.

Note that I specifically don't want to get in to using eggs or extension points. This is not an open-source project and I don't expect there to be "plugins". The point is to simplify the main application code and remove the need to modify it each time a new command module is added.

like image 872
Kamil Kisiel Avatar asked Nov 19 '08 06:11

Kamil Kisiel


People also ask

How do I import a module into a string?

To import module from string variable with Python, we can use the importlib. imnport_module method. to call importlib. import_module with the module name string to import the module.

What is __ import __ in Python?

__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.

How do you import a module from a package?

Importing module from a package We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it. Now we can directly call this function.


1 Answers

With Python older than 2.7/3.1, that's pretty much how you do it.

For newer versions, see importlib.import_module for Python 2 and Python 3.

You can use exec if you want to as well.

Or using __import__ you can import a list of modules by doing this:

>>> moduleNames = ['sys', 'os', 're', 'unittest']  >>> moduleNames ['sys', 'os', 're', 'unittest'] >>> modules = map(__import__, moduleNames) 

Ripped straight from Dive Into Python.

like image 115
Harley Holcombe Avatar answered Sep 28 '22 08:09

Harley Holcombe