简体   繁体   中英

How do I replicate “from module import class” with __import__() or django.utils.importlib?

I want to import a class from python module. The module contains multiple classes and which one I need is determined by a string at runtime.

Edit: I'm using python 2.6.5, django 1.3.

明智地使用getattr()

MyClass = getattr(mymodule, myclass_name)

或者导入 (module,globals(),locals(),['classname',], - 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM