Python NoneType' 对象没有属性 '__getitem__'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24119731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoneType' object has no attribute '__getitem__'
提问by user3170122
I am getting the following error while trying nova-list cli command in an openstack setup. NoneType' object has no attribute 'getitem'
在 openstack 设置中尝试 nova-list cli 命令时出现以下错误。NoneType' 对象没有属性 ' getitem'
DEBUG (shell:777) 'NoneType' object has no attribute '__getitem__'
Traceback (most recent call last):
File "/opt/stack/python-novaclient/novaclient/shell.py", line 774, in main
OpenStackComputeShell().main(map(strutils.safe_decode, sys.argv[1:]))
File "/opt/stack/python-novaclient/novaclient/shell.py", line 685, in main
self.cs.authenticate()
File "/opt/stack/python-novaclient/novaclient/v1_1/client.py", line 169, in authenticate
self.client.authenticate()
File "/opt/stack/python-novaclient/novaclient/client.py", line 382, in authenticate
auth_url = self._v2_auth(auth_url)
File "/opt/stack/python-novaclient/novaclient/client.py", line 469, in _v2_auth
return self._authenticate(url, body)
File "/opt/stack/python-novaclient/novaclient/client.py", line 484, in _authenticate
return self._extract_service_catalog(url, resp, respbody)
File "/opt/stack/python-novaclient/novaclient/client.py", line 307, in _extract_service_catalog
self.auth_token = self.service_catalog.get_token()
File "/opt/stack/python-novaclient/novaclient/service_catalog.py", line 29, in get_token
return self.catalog['access']['token']['id']
TypeError: 'NoneType' object has no attribute '__getitem__'
ERROR: 'NoneType' object has no attribute '__getitem_
_'
_'
What does that mean?Is there any problem with my openstack setup or it's some python related error?
这是什么意思?我的 openstack 设置有问题还是与 python 相关的错误?
回答by Bryan Oakley
Literally, 'NoneType' object has no attribute...
means that you are trying to access an attribute or call a method on something that has the value None
.
从字面上看,'NoneType' object has no attribute...
意味着您正在尝试访问属性或调用具有 value 的内容的方法None
。
In practical terms, this means you likely have a bug somewhere that is using a variable before it is assigned a value, or using the value from a function that is returning None
. The first step in debugging this problem is to ask yourself "why is this variable set to None
?".
实际上,这意味着您可能在某处存在错误,即在为变量赋值之前使用了变量,或者使用了返回 的函数的值None
。调试这个问题的第一步是问自己“为什么这个变量设置为None
?”。
In this specific case, either self.catalog
, self.catalog['access']
or self.catalog['access']['token']
is None
.
在这种特定情况下,要么self.catalog
,self.catalog['access']
要么self.catalog['access']['token']
是None
。
回答by Blendouble
Error Message
错误信息
TypeError: 'NoneType' object has no attribute '__getitem__'
Cause
原因
Calling a void method in an argument list of another function call, caused the "NoneType" error for me.
在另一个函数调用的参数列表中调用 void 方法,导致我出现“NoneType”错误。
Solution
解决方案
To solve, I just made the void method return the value I needed in the other function's argument list.
为了解决这个问题,我只是让 void 方法返回我在另一个函数的参数列表中需要的值。