Python Tornado AttributeError: module 'test' has no attribute '__path__'

Python Tornado AttributeError: module 'test' has no attribute '__path__'

我正在尝试 运行 来自 Tornado docs

Hello World 代码
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

除了我收到一个错误:AttributeError: module 'test' has no attribute '__path__'

我只是在使用 IDLE 运行 test.py

我认为这是由于我的 Windows 10 计算机无法 Python 访问 PATH 但即使将 python 3.6 添加到 PATH 我仍然遇到同样的错误。有什么想法吗?

屏幕截图是我将 python 添加到 PATH 的方式,我想我做对了..

------编辑------

我将添加一些 errors/tracebacks 我正在 运行 的屏幕截图。第一个是下面的命令提示符,当 test.py 是 运行 in IDLE 3.6 in Windows 10.

如果有导入错误,我可以通过 IDLE 解释器导入 Tornado。

我也在 IPython 3.7 中尝试了 运行 宁这个 hello World 代码,但我得到这个错误:

解决方法: 运行 你的文件没有 -m 参数。

另一个解决方案是提供不带 .py 扩展名的文件名:

python -m test

这也行。


解释:

-m 参数告诉 Python 运行 存在于 Python 路径中的模块(文件)。它不采用文件名,而是采用模块名。区别在于文件名包含 .py 后缀,而模块名则没有。

因此您也可以像这样 运行 test.py 文件:python -m test


何时使用 -m 参数:

-m 参数是为了方便。例如,如果你想要 运行 python 的默认 http 服务器(python 附带),你可以编写此命令:

python -m http.server

这将为您启动 http 服务器。 -m 参数给您带来的便利是您可以从系统的任何地方编写此命令,并且 python 会自动在系统的 Path 中查找名为 http 的包.

没有 -m 参数,如果你想 运行 http 服务器,你必须给出它的完整路径,如:

python C:\path\to\python\installation\http\server.py

因此,-m 参数可以很容易地 运行 模块(文件)出现在 Path 中。


With Tornado would you happen to know how to kill the Python interpreter? A CNTRL-C doesn't do anything.

我使用 Linux,Ctrl-C 对我来说效果很好。在 Windows 上,您可以尝试 Ctrl-DCtrl-Z。或者这里有一些答案:Stopping python using ctrl+c