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.