[英]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.py
或gunicorn
运行我的应用程序时,出现属性错误:
(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
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
应该不会有问题,因为snpp
在www
目录中:
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.