One possible solution for this problem (depending on the exact details) has to do with where one is running code from -- and changing it. For example:
If I have a small utility called test
, that's meant to be run from the command line (it starts with a "shebang" line specifying some python or other), and I put it inside the directory of a python module (let's say pythonX.Y/site-packages/whatever
, for example) that in turn has a logging
module within it (i.e. there's a logging.py
or logging/__init__.py
somewhere within the whatever
directory hierarchy), and then try to run it (via, say, ./test
— again, from within the whatever
directory — or perhaps even elsewhere within the same site-packages
, I'm not sure), I'll get the error you described.
If, however, I copy[0] the exact same file out of the pythonX.Y/site-packages/whatever
directory, and try running it from that somewhere else (e.g. I copy it to my home directory, and run it as ~/test
), then it works. Or vice-versa: a working script in ~
will break if I copy it to pythonX.Y/site-packages/whatever
.
This would presumably also apply to various other ways of invoking things than just shebang scripts (e.g. python -m whatever ...
or such)... bottom line: if there's a logging
module anywhere near where the file you're trying to run code from lives, there's a good chance python is trying to import that version of logging
instead of the usual system one.
For module owners, I'd suggest considering renaming the module's logging
to something else, to avoid creating this problem for your users.
For regular users of a thing, just pay attention to where you're calling your code from (including where the file physically sits on the filesystem, relative to various modules that might get imported).
Hopefully this helps someone get past this error! It confounded me for a bit, and this was the solution that worked for my purposes.
[0] Note: it's not enough to create a symlink... though that can be useful if one does it in the other direction; either way, python finds the absolute path of the regular file, and the rest of this analysis applies to wherever that file is: inside the module, get the module's logging
; outside the module, get the system logging
.