NameError:名称未定义.循环进口 [英] NameError: name is not defined. Circular Importing
问题描述
我在通报进口问题上遇到困难.我有2个这样的类文件:
I am having difficulty with a circular import problem. I have 2 class files like so:
--/service
service_module.py
settings.py
service_module导入整个项目中使用的其他各种文件,并充当整个项目中各种功能的容器.我想在我的settings.py文件中断言它已正确传递了service_module父类的实例.删除settings.py中的assert语句可以解决此问题,并且我可以正确调用service_module类中的方法.对于代码完成和错误检查,这使我的生活更容易断言.
service_module imports other various files used throughout the project and acts as a container for various functions throughout the project. I want to assert in my settings.py file that it is properly passed an instance of the service_module parent class. Removing the assert statement in settings.py fixes the issue and I am able to properly call methods in the service_module class. For code completion and error checking it makes my life easier to assert.
我一直在努力理解python导入,但这是处理我的特殊情况的正确方向吗?
I have always struggled with understanding python imports but is this the right direction to handle my particular case?
service_module.py
service_module.py
from PyQt5.QtCore import QObject
from sqlalchemy import *
from sqlalchemy.orm import scoped_session, Session, sessionmaker
from service.logger import logger
from sqlalchemy.orm.exc import NoResultFound
from database.tables import *
from database.load_db import load_db
from service.settings import settings
from service.web_listener import web_listener
from service.character_manager import character_manager
class Service_Module(QObject):
def __init__(self):
super(Service_Module, self).__init__()
load_database = load_db()
self.sc_session: scoped_session = load_database.get_scoped_session()
tb_scopes.make_default_scopes(service_module=self)
self.logger = logger(service_module=self)
self.settings = settings(service_module=self)
self.characters = character_manager(service_module=self)
self.callback_listener: web_listener = web_listener(service_module=self)
self.callback_listener.start()
assert isinstance(self.sc_session, scoped_session)
assert isinstance(self.logger, logger)
assert isinstance(self.settings, settings)
assert isinstance(self.callback_listener, web_listener)
settings.py
settings.py
from service.service_module import *
class settings(QObject):
def __init__(self, service_module):
super(settings, self).__init__()
self.service = service_module
assert isinstance(self.service, Service_Module) ##raises NameError: name 'Service_Module' is not defined
因此,尽管我觉得这有点老套,而且某种程度上是不正确的,但更改为它可以解决我的问题.
So changing to this solves my issue although I feel like it's kind of hacky and somehow incorrect.
from service.service_module import *
import service.service_module
class settings(QObject):
def __init__(self, service_module):
super(settings, self).__init__()
self.service = service_module
assert isinstance(self.service, service.service_module.Service_Module)
推荐答案
您需要延迟一个(或两个)文件的导入,以中断循环导入.您可以通过将import语句从file-scope(在导入模块后立即在其中执行)移动到函数或方法的执行范围中,直到调用该方法才执行.即)
You need to delay the importing of one (or both) of the files, to break the circular import. You can do this by moving the import statement from file-scope (where it is executed as soon as the module is imported) into the execution scope of a function or method, where is not executed until that method is called. Ie)
class setting(QObject):
def __init__(self, service_module):
from service.service_module import * # <-- import is here
assert isinstance(self.service, Service_Module)
当然,这可能会影响从该模块导入到文件中的其他符号的使用,因此您可能需要在多个位置指定导入.
Of course, this may affect other usages of the imported symbols from that module into this file, so you may need to specify the import in more than one place.
这篇关于NameError:名称未定义.循环进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!