【发布时间】:2015-01-24 01:08:46
【问题描述】:
我试图在 Flask 应用程序中以 url 路由为基础导入文件。几天前我开始编写 python 代码,所以我不知道我是否做得好。我把这个写在:
@app.route('/<file>')
def call(file):
__import__('controller.'+file)
hello = Example('Hello world')
return hello.msg
我还有一个名为 example.py 的文件到包含以下内容的控制器文件夹中:
class Example:
def __init__(self, msg):
self.msg = msg
所以我从终端应用程序开始并尝试输入localhost:5000/example。
我试图在屏幕上显示 Hello world 但给我下一个错误:
NameError: global name 'Example' is not defined
谢谢大家!
【问题讨论】:
-
您使用
__import__(<module>)而不是import module有什么具体原因吗?