django 中 一个奇怪问题

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

django 中 一个奇怪问题

1234-2
在 manage.py shell中

使用  from django import db 没有问题

但是使用  getattr(django,'db') 就显示 NameError: name 'django' is not defined

>>> from django import db
>>> getattr(django,'db')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'django' is not defined
>>>

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: django 中 一个奇怪问题

limodou
On Jan 8, 2008 12:09 PM, 1234 <[hidden email]> wrote:

> 在 manage.py shell中
>
> 使用  from django import db 没有问题
>
> 但是使用  getattr(django,'db') 就显示 NameError: name 'django' is not defined
>
> >>> from django import db
> >>> getattr(django,'db')
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> NameError: name 'django' is not defined
> >>>
>
使用from这种导入方式会将对象直接注入当前的namespace,所以它是不需要django前缀的。

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: django 中 一个奇怪问题

1234-2
>>> import wedding
>>> hasattr(wedding.SitesModule,'weddingmo')
False
>>> from wedding.SitesModule import weddingmo
>>> hasattr(wedding.SitesModule,'weddingmo')
True
>>>
谢谢 limodou ~~~  你看看为什么会这样? 为什么 要import weddingmo以后 hasattr 就为True了? 后面的这个 weddingmo 是根据条件动态调用的模块

在08-1-8,limodou <[hidden email]> 写道:
On Jan 8, 2008 12:09 PM, 1234 <[hidden email]> wrote:
> 在 manage.py shell中
>
> 使用  from django import db 没有问题
>
> 但是使用  getattr(django,'db') 就显示 NameError: name 'django' is not defined
>
> >>> from django import db
> >>> getattr(django,'db')
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> NameError: name 'django' is not defined
> >>>
>
使用from这种导入方式会将对象直接注入当前的namespace,所以它是不需要django前缀的。

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: django 中 一个奇怪问题

limodou
On Jan 8, 2008 1:44 PM, 1234 <[hidden email]> wrote:

> >>> import wedding
> >>> hasattr(wedding.SitesModule,'weddingmo')
> False
> >>> from wedding.SitesModule import weddingmo
> >>> hasattr(wedding.SitesModule,'weddingmo')
> True
> >>>
> 谢谢 limodou ~~~  你看看为什么会这样? 为什么 要import weddingmo以后 hasattr 就为True了? 后面的这个
> weddingmo 是根据条件动态调用的模块
>
在另一邮件中已经回答,关键是看包下的__init__.py是否做了特殊的处理。

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese