Python“ ImportError:无法导入名称”问题

ZaxR

我无法从config/__init__.py文件中导入变量

我的目录如下:

.
├── src
│   ├── config
│   │   ├── __init__.py
│   │   ├── base_config.py
│   │   ├── log_config.py
│   │   └── output_config.py
│   ├── script.py

__init__.py我有from config.output_config import output_data该文件还包含一行BUCKET = ...

在我的output_config.py文件from config import BUCKET中,函数中使用了它们output_data()

script.py我有import config一个电话config.output_data()

当我运行script.py时,出现以下错误:

Traceback (most recent call last):
  File ".../src/config/output_config.py", line 15, in <module>
    from config import BUCKET
  File ".../src/config/__init__.py", line 7, in <module>
    from config.output_config import output_data
  File ".../src/config/output_config.py", line 15, in <module>
    from config import BUCKET
ImportError: cannot import name 'BUCKET'

看来我的问题可能是在污染名称空间(并进行循环引用。),但是我不确定如何解决该问题。据我了解,该__init__.py文件最适合用于导入其他配置文件,但是我不确定如何将变量BUCKET放入output_config.py需要的位置。希望能帮助您解决问题并了解与导入相关的项目结构最佳实践。谢谢!

龙猫

我要说的是,将变量BUCKET设置为其他python文件或settings.py,如果它是设置或常量,则因为它是循环导入。

评论更新:

创建一个settings.pyconstants.py文件,然后将设置/常量放在此处,然后尝试避免进行任何导入,__init__.py除非它是标准库导入,以避免对导入进行任何潜在的循环引用。

要记住的一件事是,__init__.py每当导入该包中的任何模块时,都会加载您放入的任何内容,因此您需要注意放置在其中的内容并尽量使其简短。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python TwitterAPI问题... ImportError:无法导入名称ReadTimeout

Python请求ImportError:无法导入名称HeaderParsingError

Python ImportError:无法导入名称datafunc [PyML]

Python,ImportError:无法导入名称AbstractLazySequence

ImportError:无法在Python中导入名称....

Python 3.5“ ImportError:无法导入名称'SomeName'

Python-ImportError:无法导入名称“ HelloW”

Python ImportError:无法导入名称“ __version__”

ImportError:无法导入名称is_python_keyword

python setuptools:ImportError:无法导入名称库

Python Flask蓝图-ImportError:无法导入名称应用

statsmodels ImportError中的python 3.5:无法导入名称“ _representation”

web2py python-ImportError无法导入名称

Django ImportError:无法导入名称“ python_2_unicode_compatible”

PIP(Python):ImportError:无法导入名称_remove_dead_weakref

Python /烧瓶错误:“ ImportError:无法导入名称_compare_digest”

Python PIP引发错误-ImportError:无法导入名称主要

ImportError:无法在python-social-auth中导入名称迁移

ImportError:无法在python瓶中导入名称路由

python-pip:ImportError:无法导入名称主要

Python故障排除:ImportError:无法导入名称“ main”

Python3 ImportError:无法从“ http”导入名称“ cookies”

[Python] [cx-freeze] ImportError:无法导入名称“ ExcelFormulaParser”

Python cx_Freeze ImportError:无法导入名称“ idnadata”

如何解决ImportError:无法在python中导入名称splitfn

Python ImportError:无法从“包装”(变压器)导入名称“版本”

来自django.contrib.auth.views的Python导入注销注销ImportError:无法导入名称'注销'

如何在Python中从Cleverhans.compat导入标志时修复“ ImportError:无法导入名称'flags'”

我该如何解决无法ImportError:无法在python3中导入名称