Delgan / loguru

Python logging made (stupidly) simple
MIT License
19.99k stars 701 forks source link

Issue when a token.py file exist in the project #1078

Open acpirience opened 9 months ago

acpirience commented 9 months ago

Hello, I don't know it's a know problem or not and if you''l think it's a real bug (or that it is not specific to loguru)

To summarize, when you have a token.py in your project you get an ImportError when importing loguru.

Steps to reproduce:

  1. have a virtualenv activated with loguru installed (tested with version 0.7.2).
  2. create a main.py file containing the following content:
    
    from loguru import logger

logger.info("Test")

3. `python main.py` gives:

$ python main.py 2024-02-08 17:12:31.966 | INFO | main::3 - Test

4. add and empty `token.py` in the same directory, when you run `python main.py` you now obtain:

$ python main.py Traceback (most recent call last): File "D:\repo\test\main.py", line 1, in from loguru import logger File "D:\dev\python310\Lib\site-packages\loguru__init.py", line 10, in from ._logger import Core as _Core File "D:\dev\python310\Lib\site-packages\loguru_logger.py", line 88, in import logging File "D:\dev\python310\Lib\logging\init__.py", line 26, in import sys, os, time, io, re, traceback, warnings, weakref, collections.abc File "D:\dev\python310\Lib\traceback.py", line 5, in import linecache File "D:\dev\python310\Lib\linecache.py", line 11, in import tokenize File "D:\dev\python310\Lib\tokenize.py", line 36, in from token import EXACT_TOKEN_TYPES ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token' (D:\repo\test\token.py)


I solved this error in my project by renaming the token.py but it's quite inconvenient.

Thanks for your time.
Delgan commented 9 months ago

This is not a bug nor something I can avoid. Loguru needs to import and use the token module which exists in the Python standard libraries. When Python resolves imports, your custom token module is conflicting with the built-in one, and Python ends up importing the wrong module for some reason (it depends on import paths resolution order).