简体   繁体   English

settings.py文件中的Django AttributeError

[英]Django AttributeError in settings.py file

I've been stuck all day on what seems to be a very silly import problem. 我整天被困在一个似乎很愚蠢的进口问题上。 From my Django project directory, I can import a module and run a function just fime: 从我的Django项目目录中,我可以导入一个模块并运行一个函数,该函数只不过是:

(msg-gw)slashingweapon:~/msg-gw/www$ python 
>>> import snpp
>>> snpp.config_url('snpp://server.name.com:1234?user=me&pass=whatever')
{'host': 'server.name.com', 'pass': 'whatever', 'port': 1234, 'user': 'me'}

But when I try to run my app, either through manage.py or by gunicorn , I get an attribute error: 但是,当我尝试通过manage.pygunicorn运行我的应用程序时,出现属性错误:

(msg-gw)slashingweapon:~/msg-gw/www$ python manage.py runserver 8000
  File "/home/slashingweapon/msg-gw/www/project/settings.py", line 26, in <module>
    SNPP = snpp.config_url('snpp://server.name.com:1234?user=me&pass=whatever')
AttributeError: 'module' object has no attribute 'config_url'

The two relevant lines in my settings.py file are exactly what you would expect. 我的settings.py文件中的两条相关行正是您所期望的。 Notice that I can import the module just fine, but the config_url() function isn't found. 注意,我可以很好地导入模块,但是找不到config_url()函数。

import snpp
SNPP = snpp.config_url('snpp://server.name.com:1234?user=me&pass=whatever')

The directory layout is exactly what you would expect: 目录布局正是您所期望的:

www
  |
  +-project
  |   +-__init__.py
  |   +-settings.py
  |   +-urls.py
  |   +-views.py
  |   +-wsgi.py
  |
  +-snpp
      +-__init__.py
      +-protocol.py
      +-views.py
      +-urls.py

The config_url() function is defined inside snpp/__init__.py snpp/__init__.py config_url()函数在snpp/__init__.py定义

I have tried all kinds of things: 我尝试过各种方法:

  • from snpp import config_url
  • move config_url to the file snpp/config and then import with 移动config_url到文件snpp/config ,然后用导入
    • import snpp.confg
    • from snpp.config import config_url
    • from snpp import config and then invoke through config.config_url() from snpp import config ,然后通过config.config_url()调用

The __init__.py file is nothing special. __init__.py文件没有什么特别的。 It just lets you encode some server information as a string, so you can stick your SNPP config into the environment: 它只是让您将一些服务器信息编码为字符串,因此可以将SNPP配置粘贴到环境中:

import urlparse

def config_url(urlStr):

    config = {
        'host':None,
        'port':444,
        'user':None,
        'pass':None,
    }

    url = urlparse.urlparse(urlStr)
    if url.scheme == 'snpp':
        locParts = url.netloc.split(":")
        config['host'] = locParts[0]
        if len(locParts) > 1:
            port = int(locParts[1])
            if port > 0:
                config['port'] = port

        args = urlparse.parse_qs(url.query)
        config['user'] = args.get('user', [None])[0]
        config['pass'] = args.get('pass', [None])[0]

    return config

I am using Python 2.7, django 1.5.1, and virtualenv. 我正在使用python 2.7,Django 1.5.1和virtualenv。

Other parts of my project work well. 我项目的其他部分运行良好。 When I print out the path in my browser, it looks correct. 当我在浏览器中打印出路径时,它看起来是正确的。 Importing snpp should not be a problem, since snpp is in the www directory: 导入snpp应该不会有问题,因为snppwww目录中:

  • /home/slashingweapon/msg-gw/www /家庭/ slashingweapon / MSG-GW / WWW
  • /home/slashingweapon/msg-gw/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg /home/slashingweapon/msg-gw/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
  • /home/slashingweapon/msg-gw/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg /home/slashingweapon/msg-gw/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg
  • /home/slashingweapon/msg-gw/lib/python2.7/site-packages/django_json_rpc-0.6.2-py2.7.egg /home/slashingweapon/msg-gw/lib/python2.7/site-packages/django_json_rpc-0.6.2-py2.7.egg
  • /home/slashingweapon/msg-gw/lib/python27.zip /home/slashingweapon/msg-gw/lib/python27.zip
  • /home/slashingweapon/msg-gw/lib/python2.7 /home/slashingweapon/msg-gw/lib/python2.7
  • ... etc ... ...等等...

It doesn't matter if the snpp module is in my INSTALLED_APPS list or not. snpp模块是否在我的INSTALLED_APPS列表中并不重要。 I get the same result. 我得到相同的结果。

Solved 解决了

With the help of SO denizens, I found the problem. 在SO居民的帮助下,我发现了问题所在。

I had refactored my application by moving some reusable code pieces from the project directory to the new snpp directory. 我通过将一些可重用的代码段从project目录移到新的snpp目录来重构了我的应用程序。 When I did that, I neglected to move or delete the *.pyc files. 当我这样做时,我忽略了移动或删除*.pyc文件。

The value of snpp.__file__ was: snpp.__file__的值为:

/home/slashingweapon/msg-gw/www/project/snpp.pyc

instead of the expected: 而不是预期的:

/home/slashingweapon/msg-gw/www/snpp/__init__.pyc

During the import process, Python was looking in project/ before snpp/ and finding an old snpp.pyc file. 在导入过程中,Python会先在project/查找snpp/然后查找一个旧的snpp.pyc文件。 It would import the old pyc file and be satisfied, thus ignoring the entire snpp/ dir. 它将导入旧的pyc文件并得到满足,因此将忽略整个snpp/目录。

Had I been a little sharper (or a little more experienced with Python) I might have noticed that I was getting some strange import behavior in general whenever I tried to import anything from snpp/ . 如果我变得更加精明(或者对Python有更多的经验),我可能会注意到,每当我尝试从snpp/导入任何内容时,我都会得到一些奇怪的导入行为。 It should have occurred to me that the whole module was wonky, and not just the one function I was trying to use at the moment. 我应该想到,整个模块都是很奇怪的,而不仅仅是我目前正在尝试使用的一个功能。

Check what exactly is being imported by using 通过使用检查确切导入了什么

snpp.__file__

right after import snpp statement. import snpp语句之后。

Actually import might not be from the path you are expecting to see. 实际上,导入可能不是您期望看到的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM