Search code examples
pythonpython-importimporterror

Can not import 'Logger' from 'utils' error


Can't import Logger module from utils, Here is what my code looks like:

import requests
from lxml import html
from utils import Logger

Error is:

Traceback (most recent call last):
  File "C:/Users/Salvatore/Desktop/Python/Preme", line 4, in <module>
    from utils import Logger
ImportError: cannot import name 'Logger' from 'utils' (C:\Users\Salvatore\AppData\Local\Programs\Python\Python38-32\lib\site-packages\utils\__init__.py)

These are a list of my imports, and showing that Logger would not import.


Solution

  • Utils doesn't have a module named logger. Use python_utils instead

    Run pip uninstall utils

    Then pip install python_utils

    Finally

    # Example 1
    
    from python_utils.logger import Logged
    
    class MyClass(Logged):
        def __init__(self):
            Logged.__init__(self)
    
    
    my_class = MyClass()
    my_class.error('error')
    

    Here is more info on the python_utils package, python_utils

    Depending on your need, it might also be easier to simply use Python's built in logging capabilities with:

    # Example 2
    
    import logging
    
    logging.error('error')
    

    Here is a link to the learn more about logging, python builtin logging