简体   繁体   中英

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

Repository for reference --> https://github.com/SavSanta/bluinfo

So, Im trying to import a class BDROM I made in the file called bluinfo.py . (line 27)

By adding the line from bluinfo import BDROM to the bluinfo-gui.py (line 8)

I am receiving 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).


I've tried to alter the import statement in the following ways to no success:

  1. from.bluinfo import 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
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
$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). You could consider renaming the files inside the blueinfo directory to something more descriptive, to avoid this ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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