2

好吧,这有点复杂。

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

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)不理解我的代码,并且无法执行智能感知(自动完成代码)。

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

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)的智能感知猜测类内的方法?

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

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_list填充。subfood_list

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

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

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

4

2 回答 2

1

你想使用__import__内置:

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

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

于 2014-02-01T02:59:06.843 回答
0

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

对于使用名称作为字符串导入模块:

__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

这是fruit内部的模块之一,我用首字母大写的模块名称来命名类名:

:/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:~

请让我知道这是否有效。

于 2014-02-01T04:36:38.227 回答