ImportError: cannot import name 'getLogger'

Viewed 6361

This question already has an answer here: python & suds “ImportError: cannot import name getLogger”

But it seems that i encountered a situation which cannot be explained by the answer.

Here is the detail:

I have a file named logging.py . Codes are below:

import sys
print(sys.path)
from logging import getLogger

I knew if i run python3 logging.py ,it won't work. Because python will try to search directories in sys.path to find logging module. In this way, sys.path[0] will be the directory of the script so that python will load the module logging from the directory of the script directly instead of searching python standard lib.

Here is output:

# chuck @ LAPTOP-RN92LIAI in ~/editable_package/test_py [12:01:43]
$ python3 logging.py
['/home/chuck/editable_package/test_py', '/usr/lib/python36.zip', 
'/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', 
'/home/chuck/.local/lib/python3.6/site-packages', 
'/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
.....
ImportError: cannot import name 'getLogger'

So i tried to run the file as python3 -m logging , i expected sys.path didn't have the directory of the module , so that python will search the directories of standard lib in sys.path.

However, The result turned out that sys.path is expected but the module that python searched was not:

:!python3 -m logging
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib- 
dynload', '/home/chuck/.local/lib/python3.6/site-packages', 
'/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
...
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
  "__main__", mod_spec)
 File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
 File "/home/chuck/editable_package/test_py/logging.py", line 3, in <module>
    from logging import getLogger
  File "/home/chuck/editable_package/test_py/logging.py", line 3, in 
  <module>
   from logging import getLogger
 ImportError: cannot import name 'getLogger'

I'm confused by this. Just don't know why. Any suggestions will be appreciate.

5 Answers

The problem is that you've named your file 'logging.py' which makes it look like the Python native 'logging' module. When you run the file, it tries to import getLogger from itself which doesn't work because it's not defined there. Rename your file and it will work.

log = logging.getLogger(name)
log.info("this is info log")

This how we have to initialize a variable to be able to log in our application. 'name' usually refers to the module.

when you write python3 -m module_name python search the module_name in folder with standart libraries and finds there a not yours logging module, but other - from standart library.

Run it simple:

cd /to/folder/with/your/logging.py
python3 logging.py

or

python3 /path/to/your/logging.py

and rename your file to avoid conflicts with standart library.

python looking logger.py in folder with file with variable __name__ == 'main' it always file which was run directly, but flask do not run loggin.py directly, it import them, and they interpretator run in root of project where it file not placed understand the command from logging import getLogger like -

"import getLogger from current_dir (with __naim__ == 'main' file)

if file is not here, then => from PYTHONPATH"

to ensure with that you can add getLogger in your file and error will disappear:

import sys
print(sys.path)

class getLogger(object):
    """"""

from logging import getLogger    # tryin to import from self

I finally understood what happened , thanks for @Cory Nezin and @Andrey Suglobov.

When python tries to search module, it will search directories in order in sys.path . If sys.path[0] is '' , it doesn't mean python will jump that and search another directories . Instead, it will try to search the current directory.

Here is docs :

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

Hence ,for example ,if you are in /home which contains a file named logging.py and run :python3 -m logging ,the current directory is /home , and python will search all modules in /home first. That's why i get such an error: ImportError: cannot import name 'getLogger'.

However , if you are in /home and run the following command Line:

mkdir test
touch test/__init__.py

and put logging.py into the test dir ,then run : python3 -m test.logging, the current directory will be /home (Make sure no file named logging in /home),and python can't find logging module in that dir. Therefore, python will import standard lib. And it will finally work fine.

Related