0

The origin code is(simplify for example):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from utils.func_a import func_a as _func_a
from utils.func_b import func_b as _func_b
def func_a():
 _func_a()
def func_b():
 _func_b()
if __name__ == '__main__':
 func_a()

now I only called func_a, or maybe func_b, it depends on configuration.

so I want to dynamic do from ... import ..., such as:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def func_a():
 _func_a()
def func_b():
 _func_b()
if __name__ == '__main__':
 keys = ['func_a']
 for k in keys:
 mod_n = func_n = k
 from utils.<mod_n> import <func_n> as _<func_n> # TODO
 func_a()

but I don't know to how to implement it?

What I have thought is do import in func_X():

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def func_a():
 from utils.func_a import func_a as _func_a
 _func_a()
def func_b():
 from utils.func_b import func_b as _func_b
 _func_b()
if __name__ == '__main__':
 func_a()

but this way will do import every time when call function.


Supplement:

I have tried __import__ / importlib, but can't implement this condition

asked Aug 11, 2015 at 9:24
2
  • 2
    With regards to your last example: imports are cached, so the modules you mention will not be imported every time you call the functions. Read more here. Commented Aug 11, 2015 at 9:30
  • Take a look at the importlib module. Commented Aug 11, 2015 at 9:37

2 Answers 2

2

__import__ is a builtin function which takes the module name as a string and returns the module as an object. See the documentation:

__import__(name[, globals[, locals[, fromlist[, level]]]])

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.

answered Aug 11, 2015 at 9:45
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried __import__/importlib, but can't implment this
Can you explain why you can't implement it?
get module by import, and get func by getattr, but func is a variable. It can't implment is the second example
I don't understand what you're saying. Why doesn't it work to use __import__('utils.'+mod_n, fromlist=[func_n])?
thank you, I saw the entire __import__ doc and found the usage.
0

before I ask the question, I have tried __import__ and importlib, but can't write out the code.

thanks for @machine-yearning, I read the entire doc of __import__.

this is my solution:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def func_a():
 _func_a()
def func_b():
 _func_b()
if __name__ == '__main__':
 keys = ['func_a']
 for k in keys:
 mod_n = func_n = k
 _temp = __import__('utils.'+mod_n, fromlist=[func_n])
 _func = getattr(_temp, func_n)
 new_func_n = '_{0}'.format(k)
 setattr(sys.modules[__name__], new_func_n, _func)
 func_a()
answered Aug 11, 2015 at 14:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.