简体   繁体   English

导入和构建python模块/类

[英]import & structure python modules / classes

im working on some basic python stuff within the google app engine and I was unable to figure out the correct way to structure my handlers. 我在google app引擎中处理一些基本的python东西,我无法弄清楚构造处理程序的正确方法。

  • /main.py /main.py
  • /project/handlers/__init__.py /project/handlers/__init__.py
  • /project/handlers/AccountHandler.py /project/handlers/AccountHandler.py

the AccountHandler is basically a class AccountHandler基本上是一个类

class AccountHandler(webapp.RequestHandler):

when im using from project.handlers import AccountHandler python always give me a 当我从project.handlers导入AccountHandler使用python时,总是给我一个

TypeError: 'module' object is not callable TypeError:“模块”对象不可调用

how do i have to name/import/structure my classes? 我该如何命名/导入/构造我的班级?

cheers, Martin 马丁,干杯

To quote from the docs : 引用文档

A module is a file containing Python definitions and statements. 模块是包含Python定义和语句的文件。 The file name is the module name with the suffix .py appended. 文件名是模块名称,后缀.py

The AccountHandler you are importing is the module /project/handlers/AccountHandler.py in this case. AccountHandler要导入的模块/project/handlers/AccountHandler.py在这种情况下。 The file AccountHandler.py is not callable, and the interpreter tells you this. 文件AccountHandler.py是不可调用的,解释器会告诉您。 To call the class you defined in your file just use: 要调用您在文件中定义的类,只需使用:

from project.handlers.AccountHandler import AccountHandler
# Alternately
# from project.handler import AccountHandler
# AccountHandler.AccountHandler() # will also work.

您需要将init.py重命名为__init__.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM