Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'open' is not defined When trying to log to files

So I chose to implement logging in my discord.py bot which is fine and dandy and works alright. but once I add logging to files, be it through using a filehandler

handler = logging.FileHandler(
    filename="../logs/bot.log",
    mode="a")
formatter = logging.Formatter("%(asctime)s %(name)-30s %(levelname)-8s %(message)s")
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logging.getLogger().addHandler(handler)

Or through the basicConfig

logging.basicConfig(filename="../logs/bot.log", filemode="a", format="%(asctime)s %(name)-30s %(levelname)-8s %(message)s", level=logging.DEBUG)

I always get this Traceback on exit

Exception ignored in: <function ClientSession.__del__ at 0x7fe1330b9790>
Traceback (most recent call last):
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/client.py", line 314, in __del__
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1740, in call_exception_handler
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1182, in emit
  File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
NameError: name 'open' is not defined
Exception ignored in: <function ClientResponse.__del__ at 0x7fe13300b430>
Traceback (most recent call last):
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 757, in __del__
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/connector.py", line 177, in release
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/connector.py", line 629, in _release
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/client_proto.py", line 62, in close
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 690, in close
  File "/usr/lib/python3.8/asyncio/base_events.py", line 719, in call_soon
  File "/usr/lib/python3.8/asyncio/base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed

I'm utterly stumped as to what did wrong as the Traceback doesnt include any of my own files. I'm suspecting I might have to do some manual cleanup but even if I do logging.shutdown() I get the same Traceback

like image 264
Voreck Avatar asked Nov 04 '20 11:11

Voreck


People also ask

How do you solve NameError name is not defined?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

How do you overcome NameError in Python?

To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.

Why am I getting a NameError in Python?

What Is a NameError in Python? In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.


Video Answer


1 Answers

That is because something (asyncio in this case) tries to log during the shutting down phase of the interpreter.

This is a known problem. A workaround for the logging module has been implemented in python 3.10

It happens because the name open has already been deleted by the garbage collector before the file handler got the time to use it.

You can avoid this problem by trying to explicitly close/release your resources before your program exit.

like image 107
dragon2fly Avatar answered Oct 17 '22 01:10

dragon2fly