Rahul Shelke
Rahul Shelke

Reputation: 2166

AttributeError: 'function' object has no attribute 'HOST'

I have settings file defined as follows (config/settings.py):

HOST='localhost'
PORT='9001'

When I import settings in view and print the value of each one as follows, it prints correctly (views/user.py):

from ..config import settings

print settings.HOST
print settings.PORT

But when I use or print the same values from inside def it gives error (views/user.py):

from ..config import settings

print settings.HOST
print settings.PORT

@handle_error
def usersettings():
   print settings.HOST
   print settings.PORT

The function def when called in the above file gives error as follows:

ERROR:root:'function' object has no attribute 'HOST'
Traceback (most recent call last):
  File "/home/rahul/mywebapp/webapp/views/utils.py", line 36, in decorated_view
    return_value = func(*args, **kwargs)
  File "/home/rahul/mywebapp/webapp/views/utils.py", line 27, in decorated_view
    return func(*args, **kwargs)
  File "/home/rahul/mywebapp/webapp/views/user.py", line 344, in usersettings
    print settings.HOST
AttributeError: 'function' object has no attribute 'HOST'

utils.py has decorated view named handle_error

My Package structure is as follows:

mywebapp/
   run.py
   webapp/
      __init__.py
      views/
         __init__.py
         utils.py
         user.py
      config/
         __init__.py
         settings.py

FYI: This use to work till last night and suddenly it has started behaving weird with the above error. Whats is a wrong that I am doing here?

EDIT:

My init.py are as follows:

from .utils import *
from .user import *

Upvotes: 3

Views: 4301

Answers (1)

aIKid
aIKid

Reputation: 28312

After your import statement, there's probably a function named settings declared, or imported before usersettings. Python now recognizes the name settings as that function, not the module you imported.

To make sure this doesn't happen, you can put the part you needed straight after you import it:

from ..config import settings

HOST = settings.HOST
PORT = settings.PORT

...

And you can access it later when you like:

def usersettings():
    print HOST, PORT

Hope this helps!

Upvotes: 3

Related Questions