tags:

views:

144

answers:

2

I'm having problems with my own modules overriding built in Python ones (specifically the logging module). Here's my project layout:

run.py
package/
        __init__.py
        logging/
                __init__.py
        ...

run.py

from package import main

main()

package/__init__.py

from __future__ import absolute_import
import logging
import logging.config

def main():
    logging.config.fileConfig(...)

package/logging/__init__.py

class Logging(object):
    pass

As it stands right now, the above code works. As soon as I try to import the Logging class from package.logging like so:

from __future__ import absolute_import

import logging
import logging.config
from package.logging import Logging

def main():
    logging.config.fileConfig(...)

I get an error:

AttributeError: 'module' object has no attribute 'config'

I've read the PEP 328 release notes and found absolute imports to be rather straightforward. Unfortunately I haven't been able to figure this one out.

What am I missing here?

+1  A: 

You can use relative imports to force where python looks for the modules first:

in package/__init__.py

from . import logging
Soviut
using your example does appear to work. However, I now have to refer to the Logging class as logging.Logging. Further testing reveals that "from .logging import Logging" does not appear to work as PEP 328 suggests. I still don't understand why "from package.logging import Logging" doesn't work. Isn't it an absolute import?
kierse
what version of python are you using? If its an older version (say 2.4) the relative imports may not work, or at least, not work as expected.
Soviut
I'm running Python 2.6.2
kierse
I just verified that this problem also affects Python 2.5.2
kierse
A: 

I don't see anything creating config

Stefano Borini
you are right, I forgot to import the logging.config module. I've updated the above example and verified that the problem still exists in my test case
kierse