3

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 *
6
  • 1
    You need to show us more code. Some other code adds a settings function to that module. The function is not yet there when the module is imported (at least not where you print settings.HOST), but by the time usersettings() is called, settings has been rebound to a function object. Commented Nov 11, 2013 at 12:48
  • 1
    There is probably another settings function in your code, which explains this behaviour. Commented Nov 11, 2013 at 12:49
  • 1
    Can you show user.py? Commented Nov 11, 2013 at 12:49
  • 1
    Please grep through the whole codebase for any use of the string settings and check that there are no def settings(...) lines or settings = something else. Commented Nov 11, 2013 at 12:50
  • 1
    Thanks guys... you all are correct.... by some how my user_settings function's initial name 'user_' was deleted causing this problem...I should have looked inside my user.py before posting query... Commented Nov 11, 2013 at 12:57

1 Answer 1

3

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... you all are correct.... by some how my user_settings function's initial name 'user_' was deleted causing this problem...I should have looked inside my user.py before posting query...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.