简体   繁体   English

我该如何解决这个不能 ImportError: cannot import name in python3

[英]How can I solve this cannot ImportError: cannot import name in python3

Repository for reference --> https://github.com/SavSanta/bluinfo参考资料库 --> https://github.com/SavSanta/bluinfo

So, Im trying to import a class BDROM I made in the file called bluinfo.py .所以,我试图导入我在名为BDROM的文件中创建的bluinfo.py (line 27) (第 27 行)

By adding the line from bluinfo import BDROM to the bluinfo-gui.py (line 8)通过将行from bluinfo import BDROM添加到 bluinfo-gui.py(第 8 行)

I am receiving ImportError: cannot import name 'BDROM'我收到ImportError: cannot import name 'BDROM'

I can not figure out how to get this badboy to work.我不知道如何让这个坏小子工作。 Other posts state it may be related to PYTHONPATH (despite similar imports have been working in other parts of the program and all files are in the same directory).其他帖子 state 它可能与 PYTHONPATH 有关(尽管类似的导入已经在程序的其他部分工作并且所有文件都在同一目录中)。


I've tried to alter the import statement in the following ways to no success:我尝试通过以下方式更改导入语句,但没有成功:

  1. from.bluinfo import BDROM从.bluinfo 导入 BDROM
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named '__main__.bluinfo'; '__main__' is not a package
  1. from..bluinfo import BDROM从..bluinfo 导入 BDROM
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: attempted relative import beyond top-level package
  1. Creating an init .py file (which I believe I read is unnecessary in python3.x) and trying the import创建一个init .py 文件(我相信我在 python3.x 中没有必要阅读该文件)并尝试导入
$dev:/tmp/blue/bluinfo$ touch __init__.py
$dev:/tmp/blue/bluinfo$ python3 bluinfo-gui.py 
Traceback (most recent call last):
  File "bluinfo-gui.py", line 8, in <module>
    from bluinfo import BDROM
  File "/tmp/blue/bluinfo/bluinfo.py", line 8, in <module>
    import ts_scanner as ScanTask
  File "/tmp/blue/bluinfo/ts_scanner.py", line 5, in <module>
    import ts_streamtypeclass
  File "/tmp/blue/bluinfo/ts_streamtypeclass.py", line 3, in <module>
    from bluinfo import BDROM
ImportError: cannot import name 'BDROM'

Any ideas appreciated!任何想法表示赞赏!

In your third attempt you have a circular import.在您的第三次尝试中,您有一个循环导入。 Restructure your code to avoid that.重组你的代码以避免这种情况。

For example, you might be able to eliminate one of these imports, and you will get rid of the circular dependency.例如,您可能能够消除这些导入之一,并且您将摆脱循环依赖。

  File "/tmp/blue/bluinfo/bluinfo.py", line 8, in <module>
    import ts_scanner as ScanTask

  File "/tmp/blue/bluinfo/ts_streamtypeclass.py", line 3, in <module>
    from bluinfo import BDROM

PS: Since you have a file called blueinfo.py in a directory called blueinfo , the statement import blueinfo might mean either the package (directory) or the module (file). PS:由于您在名为blueinfo.py的目录中有一个名为blueinfo的文件,因此import blueinfo语句可能意味着 package (目录)或模块(文件)。 You could consider renaming the files inside the blueinfo directory to something more descriptive, to avoid this ambiguity.您可以考虑将 blueinfo 目录中的文件重命名为更具描述性的名称,以避免这种歧义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM