Dynamically import a method in a file, from a string

PythonDynamicImportDynamic Import

Python Problem Overview


I have a string, say: abc.def.ghi.jkl.myfile.mymethod. How do I dynamically import mymethod?

Here is how I went about it:

def get_method_from_file(full_path):
    if len(full_path) == 1:
        return map(__import__,[full_path[0]])[0]
    return getattr(get_method_from_file(full_path[:-1]),full_path[-1])


if __name__=='__main__':
    print get_method_from_file('abc.def.ghi.jkl.myfile.mymethod'.split('.'))

I am wondering if the importing individual modules is required at all.

Edit: I am using Python version 2.6.5.

Python Solutions


Solution 1 - Python

From Python 2.7 you can use the importlib.import_module() function. You can import a module and access an object defined within it with the following code:

from importlib import import_module

p, m = name.rsplit('.', 1)

mod = import_module(p)
met = getattr(mod, m)

met()

Solution 2 - Python

You don't need to import the individual modules. It is enough to import the module you want to import a name from and provide the fromlist argument:

def import_from(module, name):
    module = __import__(module, fromlist=[name])
    return getattr(module, name)

For your example abc.def.ghi.jkl.myfile.mymethod, call this function as

import_from("abc.def.ghi.jkl.myfile", "mymethod")

(Note that module-level functions are called functions in Python, not methods.)

For such a simple task, there is no advantage in using the importlib module.

Solution 3 - Python

For Python < 2.7 the builtin method __ import__ can be used:

__import__('abc.def.ghi.jkl.myfile.mymethod', fromlist=[''])

For Python >= 2.7 or 3.1 the convenient method importlib.import_module has been added. Just import your module like this:

importlib.import_module('abc.def.ghi.jkl.myfile.mymethod')

Update: Updated version according to comments (I must admit I didn't read the string to be imported till the end and I missed the fact that a method of a module should be imported and not a module itself):

Python < 2.7 :

mymethod = getattr(__import__("abc.def.ghi.jkl.myfile", fromlist=["mymethod"]))

Python >= 2.7:

mymethod = getattr(importlib.import_module("abc.def.ghi.jkl.myfile"), "mymethod")

Solution 4 - Python

from importlib import import_module
  
name = "file.py".strip('.py')
# if Path like : "path/python/file.py" 
# use name.replaces("/",".")

imp = import_module(name)

# get Class From File.py
model = getattr(imp, "classNameImportFromFile")

NClass = model() # Class From file 

Solution 5 - Python

It's unclear what you are trying to do to your local namespace. I assume you want just my_method as a local, typing output = my_method()?

# This is equivalent to "from a.b.myfile import my_method"
the_module = importlib.import_module("a.b.myfile")
same_module = __import__("a.b.myfile")
# import_module() and __input__() only return modules
my_method = getattr(the_module, "my_method")

# or, more concisely,
my_method = getattr(__import__("a.b.myfile"), "my_method")
output = my_method()

While you only add my_method to the local namespace, you do load the chain of modules. You can look at changes by watching the keys of sys.modules before and after the import. I hope this is clearer and more accurate than your other answers.

For completeness, this is how you add the whole chain.

# This is equivalent to "import a.b.myfile"
a = __import__("a.b.myfile")
also_a = importlib.import_module("a.b.myfile")
output = a.b.myfile.my_method()

# This is equivalent to "from a.b import myfile"
myfile = __import__("a.b.myfile", fromlist="a.b")
also_myfile = importlib.import_module("a.b.myfile", "a.b")
output = myfile.my_method()

And, finally, if you are using __import__() and have modified you search path after the program started, you may need to use __import__(normal args, globals=globals(), locals=locals()). The why is a complex discussion.

Solution 6 - Python

This website has a nice solution: load_class. I use it like this:

foo = load_class(package.subpackage.FooClass)()
type(foo) # returns FooClass

As requested, here is the code from the web link:

import importlib

def load_class(full_class_string):
    """
    dynamically load a class from a string
    """

    class_data = full_class_string.split(".")
    module_path = ".".join(class_data[:-1])
    class_str = class_data[-1]

    module = importlib.import_module(module_path)
    # Finally, we retrieve the Class
    return getattr(module, class_str)

Solution 7 - Python

Use importlib (2.7+ only).

Solution 8 - Python

The way I tend to to this (as well as a number of other libraries, such as pylons and paste, if my memory serves me correctly) is to separate the module name from the function/attribute name by using a ':' between them. See the following example:

'abc.def.ghi.jkl.myfile:mymethod'

This makes the import_from(path) function below a little easier to use.

def import_from(path):
    """
    Import an attribute, function or class from a module.
    :attr path: A path descriptor in the form of 'pkg.module.submodule:attribute'
    :type path: str
    """
    path_parts = path.split(':')
    if len(path_parts) < 2:
        raise ImportError("path must be in the form of pkg.module.submodule:attribute")
    module = __import__(path_parts[0], fromlist=path_parts[1])
    return getattr(module, path_parts[1])


if __name__=='__main__':
    func = import_from('a.b.c.d.myfile:mymethod')
    func()

Solution 9 - Python

How about this :

def import_module(name):

    mod = __import__(name)
    for s in name.split('.')[1:]:
        mod = getattr(mod, s)
    return mod

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlprsdView Question on Stackoverflow
Solution 1 - PythonfrmView Answer on Stackoverflow
Solution 2 - PythonSven MarnachView Answer on Stackoverflow
Solution 3 - PythongeccoView Answer on Stackoverflow
Solution 4 - PythonAlaa AqeelView Answer on Stackoverflow
Solution 5 - PythonCharles MerriamView Answer on Stackoverflow
Solution 6 - PythonkingajView Answer on Stackoverflow
Solution 7 - PythonDaniel RosemanView Answer on Stackoverflow
Solution 8 - PythonKris HardyView Answer on Stackoverflow
Solution 9 - PythonAymen AlsaadiView Answer on Stackoverflow