简体   繁体   English

ImportError:无法从“模块”导入名称“变量”

[英]ImportError: cannot import name 'variable' from 'module'

I am using Flask for my web framework. 我将Flask用于我的Web框架。 I am having an issue with imports. 我的进口商品有问题。 I am not understanding why can't I import my variable when I declare it within my my_app/__init__.py : 我不明白为什么在my_app/__init__.py声明变量时为什么不能导入变量:

from flask import Flask
from flask_login import LoginManager
from my_app.some_module.my_class.py import auth


app = Flask(__name__)
login_manager = LoginManager()

class Config:
    def __init__(self):
        pass

config = Config()

My conflictuous imports are present in my_app/some_module/my_class.py : 我的冲突导入出现在my_app/some_module/my_class.py

from flask import Blueprint
from my_app import login_manager  # this one works fine
from my_app import config

auth = Blueprint('auth', __name__)

I run the app with run.py : 我使用run.py运行该应用程序:

from my_app import app
app.run(debug=True)

I then get the error: 然后我得到错误:

Traceback (most recent call last):
  ...
  File ".../my_app/some_module/my_class.py", line 1, in <module>
    from my_app import login_manager, config
ImportError: cannot import name 'config' from 'my_app' (.../my_app/__init__.py)

Project structure is: 项目结构为:

my_app
  + __init__.py
  some_module
    + __init__.py
    + my_class.py
+ run.py

The problem is that you have a cyclic dependency. 问题是您具有循环依赖性。 By the time you import auth from my_app.some_module.my_class.py your config is not set yet. 到从my_app.some_module.my_class.py导入auth ,您的config尚未设置。 Try moving that import to the end of the my_app/__init__.py file like: 尝试将导入导入移动到my_app/__init__.py文件的末尾,如下所示:

from flask import Flask
from flask_login import LoginManager


app = Flask(__name__)
login_manager = LoginManager()

class Config:
    pass

config = Config()

from my_app.some_module.my_class.py import auth

You have a cyclic import: my_app.some_module -> my_app.some_module.my_class -> my_app.some_module . 您有一个循环导入: my_app.some_module > my_app.some_module.my_class > my_app.some_module

You can fix this by moving both Config and config to a separate module my_app.some_module.config . 您可以通过将Configconfig都移动到单独的模块my_app.some_module.config来解决此my_app.some_module.config

# my_app.some_module.my_config
class Config:
    pass

config = Config()

# my_app.some_module.my_class
from .my_config import config

# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass

This means that every import does not depend on previous imports: 这意味着每次导入都不依赖于先前的导入:

my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
\-> my_app.some_module.my_config

Doing imports this way instead of moving the import for .my_class to the end of __init__.py is more robust. 以这种方式进行导入,而不是将.my_class的导入移动到__init__.py的末尾,会更可靠。 You can freely reorder the imports of .my_class and .my_config at the top of files. 您可以在文件顶部随意重新排列.my_class.my_config的导入。

暂无
暂无

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

相关问题 ImportError:无法从“模块”导入名称“类” - ImportError: cannot import name 'Class' from 'Module' 来自。 导入[模块]-&gt; ImportError:无法导入名称[模块] - from . import [module] -> ImportError: cannot import name [module] 导入错误:无法从部分初始化的模块“tkinter”导入名称“Frame” - ImportError: cannot import name 'Frame' from partially initialized module 'tkinter' ImportError:无法从部分初始化的模块 Django 导入名称 - ImportError: cannot import name from partially initialized module Django ImportError:无法从部分初始化的模块“电报”导入名称“机器人” - ImportError: cannot import name 'Bot' from partially initialized module 'telegram' Pyomegle:ImportError:无法从部分初始化的模块中导入名称“OmegleClient” - Pyomegle: ImportError: cannot import name 'OmegleClient' from partially initialized module ImportError:无法导入模块 - ImportError: cannot import module 文件“/usr/bin/pip”,第 9 行,在<module> from pip import main ImportError: 无法导入名称 main - File “/usr/bin/pip”, line 9, in <module> from pip import main ImportError: cannot import name main ImportError:无法从部分初始化的模块“gi”导入名称“_gi”(很可能是由于循环导入) - ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) Python ImportError:无法从部分初始化的模块“..”导入名称“..”(很可能是由于循环导入) - Python ImportError: cannot import name '..' from partially initialized module '..' (most likely due to a circular import)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM