【问题标题】:Circular import error with some not familar error带有一些不熟悉的错误的循环导入错误
【发布时间】:2021-05-15 07:02:40
【问题描述】:

/usr/local/bin/python3: Error while finding module specification for 'system.__main__' (ImportError: cannot import name 'LANG' from partially initialized module 'system' (most likely due to a circular import) (/Black-Lightning/system/__init__.py))

首先我不熟悉第一个错误,我没有同名系统的模块 而且我没有从一个系统导入到另一个系统,docker文件或shell文件有什么问题吗? (学习)

外壳

#!/bin/bash

if [ -e exconfig.py  ]
then
    a="""
LOADING USERBOT  - KINDLY WAIT 5min. 
INITIALIZING - Self Hosting Setup 
© Black-Lightning 2021"""
    


else
    a =     """LOADING USERBOT  - KINDLY WAIT 5min. 
USERBOT -  Black-Lightning
© Black-Lightning 2021"""


fi


echo a
git clone https://github.com/KeinShin/Black-Lightning -b rebirth 
chmod +x /usr/local/bin/*


cd Black-Lightning

echo "Installing Requirements"

pip3 install -r requirements.txt && pip3 install --no-cache-dir -r requirements.txt







echo "INITIALIZING System"
python3 -m system```

【问题讨论】:

    标签: python import specifications circular-dependency


    【解决方案1】:

    能否请您分享您在 requirements.txt 中的内容以及在 Black-Lightning 项目中导入的所有库。这个问题似乎是循环导入的问题。 我将尝试向您概述什么是循环导入,以及您可以在代码中避免或调试这种情况的方法。

    当两个模块相互依赖时会发生循环导入。假设您有两个模块(可以来自您使用的外部库或来自您的代码库) 它们如下所示:-

    # module1
    import module2
    
    def function1():
        module2.function2()
    
    def function3():
        print('Goodbye, World!')
    

    # module2
    import module1
    
    def function2():
        print('Hello, World!')
        module1.function3()
    

    假设你有一个 init.py 正在调用 module1.function1(),在这种情况下,将会发生的是在调用 function1() 时,它会看到一个函数来自 module2 的名称被称为 function2()。所以它将导入到function2()。但请记住,python 还没有到达 module1 的 function3()。所以当module1的function3()被module1的function2()调用时,会报错。

    这是循环导入的一个实例,因为您的代码中存在模块。使用不同版本的不同库时可能会出现相同的场景。

    为了避免这种问题,我强烈建议两点:-

    1. 永远不要尝试像 "from xyz import *"

      这样的导入
    2. 如果您的代码中有多个模块,请尝试提及 导入它们时的确切路径。 不要 :-> from xyz import a, b, c

      :-> from /xyz import a, b, c

    3. 要调试循环导入的问题,您可以尝试使用Module Finder。这可以帮助您列出项目中的所有依赖项,然后您可以查看哪些是冲突的

    4. 调试此问题的另一种明显方法是使用类似于 python -v .py

      的命令以详细模式运行应用程序

    希望这些步骤可以帮助您解决问题。如果需要任何其他说明,请在 cmets 中联系。

    参考:- Python Circular Imports

    【讨论】:

    • 你能不能用逗号分隔它们,这有点令人困惑。你也可以分享你项目中的导入吗
    • 你的解释很好,现在我知道该怎么做了谢谢哥们!
    • heroku3, art, bs4, html5lib, requests, GitPython, holidays, schedule, pycountry, better_profanity, translate,e textblob, telegraph
    • /usr/local/bin/python3: Error while finding module specification for 'system.__main__ 是否与循环导入有关?
    • 是的,因为有循环导入,有些依赖无法识别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2016-12-01
    • 2023-02-10
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多