进行动态导入,这在Python中仍然是显式的

时间:2014-02-01 02:54:53

标签: python intellisense dynamic-import

好的,这有点复杂。

假设我在包中有一个模块:

a_package
 |-- __init__.py
 |-- a_module.py

a_module.py内,我声明A_Class

# file location: a_package/a_module.py
class A_Class():
    def say(self):
        print ("cheese")

我可以通过这样做来创建A_Class的实例并调用say方法:

from a_package.a_module import A_Class
my_object = A_Class()
my_object.say() # this will display 'cheese' as expected

但是,我想制作一个更动态的方法(我计划有很多包和类,并希望使代码更容易编写)。所以,我创建了一个名为load_class

的函数
def load_class(package_name, module_name, class_name)
    result = None
    try:
        exec('from ' + package_name + '.' + module_name + ' import ' + class_name)
        exec('result = '+class_name)
    except:
        raise ImportError('Unable to load the class')
    return result

# Now, I can conveniently do this:
A_Class = load_class('a_package', 'a_module', 'A_Class')
my_object = A_Class()
my_object.say()

# or even shorter:
load_class('a_package', 'a_module', 'A_Class')().say()

该程序按预期工作,但IDE(我使用pydev)不理解我的代码,也不能执行intellisense(自动完成代码)。

如果我使用第一种方法,智能感知显然有效:

from a_package.a_module import A_Class
my_object = A_Class()
my_object. # when I press ".", there will be a popup to let me choose "say" method

但如果我使用第二种方法,智能感知不能为我完成:

load_class('a_package', 'a_module', 'A_Class')(). # when I press ".", nothing happened

我知道,这是在Python中进行动态导入的权衡。但是,我想知道是否有一些替代方法让我做第二种方法(可能不使用exec)仍然可以让通用IDE(如Pydev)的intellisense猜测类中的方法?

编辑:为什么我需要这样做? 假设我有这样的目录结构

fruit
 |-- strawberry.py
 |-- orange.py

chocolate
 |-- cadbury.py
 |-- kitkat.py

need_dynamic.py

need_dynamic.py中,我有这个脚本:

food_list = ['fruit', 'chocolate']
subfood_list = [['strawberry', 'orange'],['cadbury', 'kitkat']] 
# show food list and ask user to choose food
for i in xrange(len(food_list)):
    print i + " : " + food_list[i]
food_index = int(raw_input('chose one'))
# show subfood list and ask user to choose subfood 
for i in xrange(len(subfood_list[food_index])):
    print i + " : " + subfood_list[food_index][i]
subfood_index = int(raw_input('chose one'))
# init the class
my_class = load_class(food_list[food_index], subfood_list[food_index, subfood_index])
# the rest of the code

这只是为了简化,实际上,我计划通过获取目录自动填充food_listsubfood_list

想象一下,您有一个数据分类框架,并希望让用户选择他们想要使用的方法。用户还应该能够通过简单地添加python包模块来扩展框架。

我希望这个例子是合理的。

再次编辑接受的答案无法解决智能感知问题。但它显示了如何更好地编码。我认为这是IDE问题而不是python问题。我将发布另一个问题。

2 个答案:

答案 0 :(得分:1)

您想要使用内置的__import__

def load_class(package, mod_name, cls_name):
    mod = __import__('.'.join((package, mod_name)))
    return getattr(mod, cls_name)

当然,如果你愿意的话,你可以把你的错误处理放回去,老实说,我不确定为什么你想要这样做首先。动态导入似乎是一种代码味道"给我大多数的东西。在你开始使用它之前,请评估你是否真的需要

答案 1 :(得分:0)

好的,这是您的解决方案:

使用name作为字符串导入模块:

__import__(modulename, globals(), locals(), ['*'])

从模块路径加载类:

cls = getattr(sys.modules[modulename], classname)

目录结构:

:/tmp/dynamic_import:~ ls
chocolate   fruit       need_dynamic.py
:/tmp/dynamic_import:~

need_dynamic.py

fruit
  |-- strawberry.py
  |-- orange.py

chocolate
 |-- cadbury.py
 |-- kitkat.py

这是水果中的模块之一,我将类名称与模块名称一起命名为姓名缩写:

:/tmp/dynamic_import:~ cat orange.py
class Orange(object):
    def say(self):
        return "Say cheese from class: %s" % __name__
:/tmp/dynamic_import:~

这是您的主要脚本:

#!/usr/bin/env python

import os
import sys
import inspect

def load_modules_from_path(path):
    """
    Import all modules from the given directory
    """
    # Check and fix the path
    if path[-1:] != '/':
        path += '/'

    # Get a list of files in the directory, if the directory exists
    if not os.path.exists(path):
         raise OSError("Directory does not exist: %s" % path)

    # Add path to the system path
    sys.path.append(path)
    # Load all the files in path
    for f in os.listdir(path):
        # Ignore anything that isn't a .py file
        if len(f) > 3 and f[-3:] == '.py':
            modname = f[:-3]
            # Import the module
            __import__(modname, globals(), locals(), ['*'])

def load_class_from_name(fqcn):

    # fqcn = fully qualified classname
    # Break apart fqcn to get module and classname

    paths = fqcn.split('.')
    modulename = '.'.join(paths[:-1])
    classname = paths[-1]

    # Import the module
    __import__(modulename, globals(), locals(), ['*'])

    # Get the class
    cls = getattr(sys.modules[modulename], classname)
    # Check cls
    if not inspect.isclass(cls):
        raise TypeError("%s is not a class" % fqcn)

    # Return class
    return cls

def main():

    food_list = ['fruit', 'chocolate']
    subfood_list = [['strawberry', 'orange'],['cadbury', 'kitkat']]

    # show food list for users to select.
    for i in xrange(len(food_list)):
        print '%d: %s' % (i, food_list[i])

    food_index = int(raw_input('Choose one: '))

    for i in xrange(len(subfood_list[food_index])):
        print '%d: %s' % (i, subfood_list[food_index][i])

    subfood_index = int(raw_input('Chose one: '))
    pkg = food_list[food_index]
    module = subfood_list[food_index][subfood_index]
    class_name = module.title()

    load_modules_from_path(pkg)
    new_class = load_class_from_name('%s.%s' % (module, class_name))

    # instantiation
    obj = new_class()
    print obj.say()

if __name__ == '__main__': main()

以下是输出:

:/tmp/dynamic_import:~ python need_dynamic.py
0: fruit
1: chocolate
Choose one: 0
0: strawberry
1: orange
Chose one: 0
Say cheese from class: strawberry
:/tmp/dynamic_import:~ python need_dynamic.py
0: fruit
1: chocolate
Choose one: 1
0: cadbury
1: kitkat
Chose one: 0
Say cheese from class: cadbury
:/tmp/dynamic_import:~

如果有效,请告诉我。