I noticed something weird today I would like explained. I wasn't 100% sure how to even phrase this as a question, so google is out of the question. The logging module does not have access to the module logging.handlers for some odd reason. Try it yourself if you don't believe me:
>>> import logging
>>> logging.handlers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'handlers'
>>> import logging.handlers
>>> logging.handlers
<module 'logging.handlers' from '/usr/lib/python2.6/logging/handlers.pyc'>
Can anyone explain why this happens?
I've faced recently the same odd situation. So, I bet you've removed some third-party lib import. That removed lib contained
from logging import handlers
orfrom logging import *
and provided youhandlers
. And in other script you've had something likeimport logging
and just usedlogging.handlers
and you've thought that is a way things work as I did.In Python, modules need to be imported before they're accessible.
import logging
imports just the logging module. It so happens thatlogging
is a package with submodules, but those submodules are still not automatically loaded. So, you need to explicitly importlogging.handlers
before you can access it.If you're wondering why it looks like sometimes you don't need those extra imports: some packages import some or all of their submodules when they are imported -- simply by doing those imports in their
__init__.py
files. In other cases it might be that something else that you import, also importedlogging.handlers
. It doesn't matter which piece of code does the import; as long as something in your process importslogging.handlers
before you access it, it'll be there. And sometimes a module that looks like a package really isn't one, likeos
andos.path
.os
isn't a package, it just imports the correct other module (for your platform) and calls itpath
, just so you can access it asos.path
.I'm also new to python and after having lots of practice now I can differentiate between, package (folder) , module(.py) , classes,variables...etc...
if you want any of your folder to be python package - It must contain
__init__.py
file even empty file will do !!!and as Thomas said, you can import extra module in
__init__.p
y if you want !!! but modules/packages are accessible only after importing it...if you want to import everything from a module you can use
rest you can access the handlers module like below too,