Python 将main.py导入另一个模块(包含在同一目录中)

Python 将main.py导入另一个模块(包含在同一目录中),python,python-2.7,webapp2,Python,Python 2.7,Webapp2,如何将类从main.py导入同一目录中的另一个文件 文件结构: AttributeError: 'module' object has no attribute 'ClassX' 项目 |--main.py |--first_file.py |--second_file.py main.py: class ClassX(): def random1(self): .... return stuff def get(self):

如何将类从
main.py
导入同一目录中的另一个文件

文件结构:

AttributeError: 'module' object has no attribute 'ClassX'
项目
|--main.py
|--first_file.py
|--second_file.py
main.py

class ClassX():
    def random1(self):
        ....
        return stuff

    def get(self):
        class_x_stuff = self.random1()
        print class_x_stuff

app = webapp2.WSGIApplication([
    ('/classw', ClassW),
    ('/classx', ClassX)
], debug=True)
import main

checkpoint = main.ClassX()
print checkout
第一个文件.py

class ClassX():
    def random1(self):
        ....
        return stuff

    def get(self):
        class_x_stuff = self.random1()
        print class_x_stuff

app = webapp2.WSGIApplication([
    ('/classw', ClassW),
    ('/classx', ClassX)
], debug=True)
import main

checkpoint = main.ClassX()
print checkout
输出:

AttributeError: 'module' object has no attribute 'ClassX'
我使用的资源:

  • 我能够将
    first_file.py
    导入
    main.py
    以及
    second_file.py
    。但是我无法将
    main.py
    导入其他文件

    重命名
    main.py
    文件(例如,
    proj_main.py
    )会导致错误:

    回溯(最近一次呼叫最后一次):
    文件“/usr/local/google\u appengine/google/appengine/runtime/wsgi.py”,第240行,在句柄中
    handler=\u config\u handle.add\u wsgi\u中间件(self.\u LoadHandler())
    文件“/usr/local/google\u appengine/google/appengine/runtime/wsgi.py”,第299行,在\u LoadHandler中
    处理程序,路径,err=LoadObject(self.\u处理程序)
    LoadObject中的文件“/usr/local/google\u appengine/google/appengine/runtime/wsgi.py”,第85行
    obj=\uuuuuuuuuuuuuu导入(路径[0])
    ImportError:没有名为main的模块
    
    我有什么选择?我如何解决这个问题

    编辑:

    AttributeError: 'module' object has no attribute 'ClassX'
    
    完全回溯:

    INFO     2017-10-02 22:10:21,744 dispatcher.py:226] Starting module "default" running at: http://localhost:7070
    INFO     2017-10-02 22:10:21,747 admin_server.py:116] Starting admin server at: http://localhost:8000
    ERROR    2017-10-02 22:10:24,017 wsgi.py:263] 
    Traceback (most recent call last):
      File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
      File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
      File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
      File "/Users/mycomputer/Documents/project_folder/main.py", line 34, in <module>
    from first_file import ClassY
      File "/Users/mycomputer/Documents/project_folder/first_file.py", line 20, in <module>
        checkpoint = main.ClassX()
    AttributeError: 'module' object has no attribute ‘ClassX’
    INFO     2017-10-02 22:10:24,023 module.py:832] default: "GET / HTTP/1.1" 500 -
    
    INFO 2017-10-02 22:10:21744 dispatcher.py:226]启动运行在以下位置的模块“默认值”:http://localhost:7070
    信息2017-10-02 22:10:21747管理服务器。py:116]正在以下位置启动管理服务器:http://localhost:8000
    错误2017-10-02 22:10:24017 wsgi.py:263]
    回溯(最近一次呼叫最后一次):
    文件“/usr/local/google\u appengine/google/appengine/runtime/wsgi.py”,第240行,在句柄中
    handler=\u config\u handle.add\u wsgi\u中间件(self.\u LoadHandler())
    文件“/usr/local/google\u appengine/google/appengine/runtime/wsgi.py”,第299行,在\u LoadHandler中
    处理程序,路径,err=LoadObject(self.\u处理程序)
    LoadObject中的文件“/usr/local/google\u appengine/google/appengine/runtime/wsgi.py”,第85行
    obj=\uuuuuuuuuuuuuu导入(路径[0])
    文件“/Users/mycomputer/Documents/project_folder/main.py”,第34行,在
    从第一个文件导入ClassY
    文件“/Users/mycomputer/Documents/project_folder/first_File.py”,第20行,在
    checkpoint=main.ClassX()
    AttributeError:“模块”对象没有属性“ClassX”
    INFO 2017-10-02 22:10:24023 module.py:832]默认值:“GET/HTTP/1.1”500-
    
    问题是我遇到了:

    second_file.py

    import second_file
    
    checkpoint = second_file.class_x()
        print checkout
    
    import main
    
    def class_x():
        item = main.ClassX()
        return item
    

    first_file.py
    如何执行(当您获得
    AttributeError
    时)?
    ClassX():
    ->
    ClassX:
    :小写c和(可选)没有大括号。这实际上只是键入时的一个错误,但在我的代码中它是小写的:
    class
    ,所以问题不是这样。@martineau
    first\u file.py
    根本没有运行。当我在localhost中运行应用程序时,结果是:
    此页面无法工作127.0.0.1当前无法处理此请求。HTTP错误500在这种情况下,请再次回答您的问题,并添加出现在
    AttributeError之前的完整回溯:“module”对象没有属性“ClassX”
    异常。