在Python中导入模块
问题描述:
我在理解如何将龙卷风应用程序分发到多个文件中遇到了一些麻烦。我需要一个文件创建应用程序实例,另一个文件处理登录/注销功能,另一个处理配置文件页面视图等等。但我没有得到的是如何做到这一点。 可以说,比如我有两个文件: -app.py(创建应用程序实例) -auth.py(登录/注销功能)在Python中导入模块
app.py
>import tornado
>import auth
> handlers = [
(r"/", MainHandler),
(r"/auth", auth.AuthHandler),
(r"/logout", auth.LogoutHandler),
]
这工作得很好,但是当我有app.py因为这:
>import tornado
>import auth
>import profile
> handlers = [
(r"/", MainHandler),
(r"/auth", auth.AuthHandler),
(r"/logout", auth.LogoutHandler),
(r"/profile", profile.ViewHandler),
]
auth.py
>import tornado
>import app
>class AuthHandler(app.BaseHandler)
> > ...
>class LogoutHandler(app.BaseHandler)
> >...
and in profile.py i have this:
>import app
>import tornado
>class ViewProfile(app.BaseHandler)
---it shows error that in profile.py module app has no attribute BaseHandler
答
如果在auth.py和profile.py中删除“导入应用程序”,会发生什么情况?看来你正在创造循环进口。
它显示名称的应用程序没有被定义为app.BaseHandler是基类,现在的应用程序没有导入的错误。即使我想这样,但不能得到一个解决方案 – tushar
嗯,我认为你应该重组你的文件以避免这些循环引用。也许你可以把Basehandler放在另一个文件(base.py)中,然后从auth.py和profile.py指向它 – RickyA