[CentralOH] Dynamic Importing

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Fri Mar 15 03:26:51 CET 2013


On Thu, 14 Mar 2013 21:37:10 -0400, John Santiago <jdsantiagojr at gmail.com> wrote:

> ... __import__('controllers.%s' % path, fromlist=[path])

That __ stuff gives me the creeps. 
Does importlib.import_module() work for you? 

test at test:~$ python
Python 2.7.3 (default, Sep 26 2012, 21:53:58) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help(__import__)

Help on built-in function __import__ in module __builtin__:

__import__(...)
    __import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module
    
    Import a module. Because this function is meant for use by the Python
    interpreter and not for general use it is better to use
    importlib.import_module() to programmatically import a module.

...

>>> help(importlib.import_module)

Help on function import_module in module importlib:

import_module(name, package=None)
    Import a module.
    
    The 'package' argument is required when performing a relative import. It
    specifies the package to use as the anchor point from which to resolve the
    relative import to an absolute import.




More information about the CentralOH mailing list