Yarin
Yarin

Reputation: 183499

Module throws name error when imported from package

I'm trying to import a module that contains a single line: import logging, into my web2py project. When I try to import the module from within a package called mymodules, I get a <type 'exceptions.NameError'> name 'logging' is not defined error, but When I take the module out of the package and import it directly, it works fine.

mymodule.py:

import logging

Importing it from top-level modules directory:
import mymodule - works fine

Importing from a package:
from mymodules import mymodule - get the name error

There's nothing wrong with the package - I have other modules in there that I import with no problem. Don't have a clue as to why this is happening.

Upvotes: 1

Views: 644

Answers (3)

Yarin
Yarin

Reputation: 183499

This was happening because of some old .pyc files that were still in the package- they must have been causing the bad logging references. Once I cleared them my problem was solved. :S

Upvotes: 0

MattoTodd
MattoTodd

Reputation: 15209

First off, you dont need to add the file extension .py to your import statement. So it should be:

import mymodule
mymodule.logging.info("Just some info")

Anthony is correct though, if you want to be able to use logging from there, you would have to call it via mymodule.logging You could also use a wildcard import but you just need to be a little careful of what you then import in mymodule.

from mymodule import *
#now you can use logging directly
logging.info("Just some info")

Upvotes: 0

Anthony
Anthony

Reputation: 25536

Once you import mymodule, you then have to refer to mymodule.logging rather than just logging.

Upvotes: 1

Related Questions