【问题标题】:Importing main.py into another module (contained in the same directory)将 main.py 导入另一个模块(包含在同一目录中)
【发布时间】:2017-10-02 22:04:24
【问题描述】:

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

文件结构:

Project
    |-- main.py
    |-- first_file.py
    |-- second_file.py

ma​​in.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)

first_file.py

import main

checkpoint = main.ClassX()
print checkout

输出:

AttributeError: 'module' object has no attribute 'ClassX'

我使用过的资源:

  1. How to import the class within the same directory or sub directory?
  2. AttributeError: 'module' object has no attribute

我可以将first_file.py 导入main.py 以及second_file.py。但我无法将main.py 导入到其他文件中。

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

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])

ImportError: No module named main

我有哪些选择?我该如何解决这个问题?

编辑:

完整追溯:

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 -

【问题讨论】:

  • first_file.py 是如何被执行的(当你得到AttributeError 时)?
  • Class ClassX(): -> class ClassX::小写 c 并且(可选)没有大括号。
  • 这实际上只是输入错误,但在我的代码中是小写:class 所以问题不在于。
  • @martineau first_file.py 根本没有运行。当我在 localhost 中运行应用程序时,结果是:This page isn’t working 127.0.0.1 is currently unable to handle this request. HTTP ERROR 500
  • 在这种情况下,请再次edit您的问题并添加出现在AttributeError: 'module' object has no attribute 'ClassX'异常之前的完整回溯。

标签: python python-2.7 webapp2


【解决方案1】:

问题是我遇到circular dependency

File "/Users/mycomputer/Documents/project_folder/main.py", line 34, in <module>
from first_file import ClassY

请注意,main.py 是从 first_file 导入的,然后 first_file 是从 main.py 导入的,这导致了 circular import gotcha

解决方案:

first_file.py

import second_file

checkpoint = second_file.class_x()
    print checkout

second_file.py

import main

def class_x():
    item = main.ClassX()
    return item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2018-12-27
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多