Unable to import package: ImportError: cannot import name 'UnExpectedError'

Asked 2 years ago, Updated 2 years ago, 39 views

Hello, I am currently worried about how to write the package import.
_init__.py is written incorrectly
cmd.py cannot call the error class for errors.py

I looked it up on the Internet, but I couldn't understand it, so I would like to ask you a question.
If anyone knows, please let me know

main.py

 from Utilities.output import Logs

Utilities.__init__.py

#-*-coding:utf-8-*-

from Utilities import errors
from Utilities import cmd
from Utilities import output
from Utilities importouts


from Utilities.errors import BaseError
from Utilities.errors import UnusableError
from Utilities.errors import UnExpectedError

from Utilities.errors import InputError
from Utilities.errors import StatusError

from Utilities.output import Logs

from Utilities.cmd import Command

from Utilities.outs import trading
from Utilities.outs import initialize

errors.py

#-*-coding:utf-8-*-

from Utilities.output import Logs

log = Logs()

# all underlying errors
Class BaseError (Exception):
    def__init__(self, message):
        log.Error(message)

# US>Unavailable error
class UnusableError (BaseError): pass

# unexpected error
classUnExpectedError (BaseError): pass

# input format error
class InputError (BaseError): pass
hereinafter abbreviated

TrackBack

Traceback (most recent call last):
  File "c:\user\dev\main.py", line 1, in <module>
    from Utilities.output import Logs
  File "c:\user\dev\Utilities\_init__.py", line 3, in <module>
    from Utilities import errors
  File "c:\user\dev\Utilities\errors.py", line 3, in <module>
    from Utilities.output import Logs
  File "c:\user\dev\Utilities\output.py", line 7, in <module>
    from Utilities.cmd import Command
  File "c:\user\dev\Utilities\cmd.py", line 3, in <module>
    from Utilities.errors import UnExpectedError, InputError
ImportError: cannot import name 'UnExpectedError'

Tree

python python3

2022-09-30 21:31

2 Answers

For the time being, _init__.py doesn't really matter this time, because the three modules -- errors, output, and cmd -- are cyclically referenced and are in a three-way state.

  • errors:dependent on output (using output.Logs)
  • output:dependent on cmd (using cmd.Command from error message)
  • cmd:Dependent on errors (using errors.UnExpectedError from error message)

Imported dependency diagram

When cmd tries to import errors, the errors import is still incomplete, so UnExpectedError is not visible.

To solve this problem, we have to cut off this three-way circular reference somewhere.
For example, if you stop using cmd.Command on output.py, you will be able to import.
(It's about how to design a module.)


2022-09-30 21:31

The Python manual hardly says anything about the package, so you shouldn't think about it too hard.

_init__.py must be created because it treats the directory where this file resides as a package, but it can be empty.Typically, you write the code for initializing the package.This time, it should be an empty file for now.

cmd.py to errors.py error classes can be called by writing a improt statement as follows:

 from Utilities.errors import BaseError, UnusableError, UnExpectedError

Class Command:
    def test():
        # processing
        BaseError ('message')
        # processing
        UnusableError ('passage')
        # processing


2022-09-30 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.