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'
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.
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.)
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
© 2025 OneMinuteCode. All rights reserved.