Python 使用plac时的属性错误:';名称空间';对象没有属性
尝试编写命令行函数时,我被这个AttributeError阻止了。我知道其他人也问过类似的问题,但我没有看到任何人在使用plac,所以我想我应该把这个写出来Python 使用plac时的属性错误:';名称空间';对象没有属性,python,namespaces,argparse,Python,Namespaces,Argparse,尝试编写命令行函数时,我被这个AttributeError阻止了。我知道其他人也问过类似的问题,但我没有看到任何人在使用plac,所以我想我应该把这个写出来 @plac.annotations( training_file=("The filename containing the text you wish to annotate", "option", "-tf", Path), entity_type=("The name of the entity you wish to
@plac.annotations(
training_file=("The filename containing the text you wish to annotate", "option", "-tf", Path),
entity_type=("The name of the entity you wish to annotate", "option", "-e", str)
)
def main(training_file=None, entity_type=None):
"""Script to more easily annotate spaCy NER training examples"""
if not training_file:
training_file = input("Please enter the filename of the data you wish to annotate: ")
with open(training_file, 'r') as training_file:
list_to_annotate = training_file.read()
print(list_to_annotate)
以及它运行的位置:
if __name__ == "__main__":
plac.call(main)
我的实际命令还有更多内容,但每当我运行此命令时,都会收到相同的错误消息:
Traceback (most recent call last):
File "C:\Users\Steve\PycharmProjects\GroceryListMaker\model_scripts\training_data_maker.py", line 79, in <module>
plac.call(main)
File "C:\Users\Steve\PycharmProjects\GroceryListMaker\lib\site-packages\plac_core.py", line 367, in call
cmd, result = parser.consume(arglist)
File "C:\Users\Steve\PycharmProjects\GroceryListMaker\lib\site-packages\plac_core.py", line 230, in consume
args = [getattr(ns, a) for a in self.argspec.args]
File "C:\Users\Steve\PycharmProjects\GroceryListMaker\lib\site-packages\plac_core.py", line 230, in <listcomp>
args = [getattr(ns, a) for a in self.argspec.args]
AttributeError: 'Namespace' object has no attribute 'training_file'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Steve\PycharmProjects\GroceryListMaker\model\u scripts\training\u data\u maker.py”,第79行,在
位置呼叫(主)
文件“C:\Users\Steve\PycharmProjects\GroceryListMaker\lib\site packages\plac_core.py”,第367行,在调用中
cmd,result=parser.consume(arglist)
文件“C:\Users\Steve\PycharmProjects\GroceryListMaker\lib\site packages\plac_core.py”,第230行
args=[self.argspec.args中a的getattr(ns,a)]
文件“C:\Users\Steve\PycharmProjects\GroceryListMaker\lib\site packages\plac_core.py”,第230行,在
args=[self.argspec.args中a的getattr(ns,a)]
AttributeError:“命名空间”对象没有属性“training\u file”
我真的不确定到底出了什么问题,这让我在这里毛骨悚然。非常感谢您的帮助。如果您将其替换为:
@plac.annotations(
training_file=("The filename containing the text you wish to annotate",
"option", "tf", Path),
entity_type=("The name of the entity you wish to annotate", "option", "e", str)
)
它可以工作(请注意,我删除了缩写中的-
)
将来,您可以使用pdb更快地跟踪此类问题。以下是我所做的:
$ python -m pdb main.py
> /home/embray/src/junk/so/60005716/main.py(1)<module>()
-> import plac
(Pdb) cont
Traceback (most recent call last):
File "/usr/lib/python3.6/pdb.py", line 1667, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python3.6/pdb.py", line 1548, in _runscript
self.run(statement)
File "/usr/lib/python3.6/bdb.py", line 434, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "/home/embray/src/junk/so/60005716/main.py", line 1, in <module>
import plac
File "/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site-packages/plac_core.py", line 367, in call
cmd, result = parser.consume(arglist)
File "/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site-packages/plac_core.py", line 230, in consume
args = [getattr(ns, a) for a in self.argspec.args]
File "/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site-packages/plac_core.py", line 230, in <listcomp>
args = [getattr(ns, a) for a in self.argspec.args]
AttributeError: 'Namespace' object has no attribute 'training_file'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site-packages/plac_core.py(230)<listcomp>()
-> args = [getattr(ns, a) for a in self.argspec.args]
(Pdb) up
> /home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site-packages/plac_core.py(230)consume()
-> args = [getattr(ns, a) for a in self.argspec.args]
(Pdb) p ns
Namespace(e=None, tf=None)
$python-m pdb main.py
>/home/embray/src/junk/so/60005716/main.py(1)()
->进口许可证
(Pdb)续
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.6/pdb.py”,第1667行,在main中
pdb.\u运行脚本(mainpyfile)
文件“/usr/lib/python3.6/pdb.py”,第1548行,在运行脚本中
self.run(语句)
文件“/usr/lib/python3.6/bdb.py”,第434行,正在运行
执行官(命令、全局命令、本地命令)
文件“”,第1行,在
文件“/home/embray/src/junk/so/60005716/main.py”,第1行,在
进口许可证
文件“/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site packages/plac_core.py”,第367行,调用中
cmd,result=parser.consume(arglist)
文件“/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site packages/plac_core.py”,第230行
args=[self.argspec.args中a的getattr(ns,a)]
文件“/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site packages/plac_core.py”,第230行,在
args=[self.argspec.args中a的getattr(ns,a)]
AttributeError:“命名空间”对象没有属性“training\u file”
未捕获的异常。进入后期调试
运行“cont”或“step”将重新启动程序
>/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site packages/plac_core.py(230)()
->args=[self.argspec.args中a的getattr(ns,a)]
(Pdb)向上
>/home/embray/.virtualenvs/tmp-954ecd64f7669c29/lib/python3.6/site packages/plac_core.py(230)consume()
->args=[self.argspec.args中a的getattr(ns,a)]
(Pdb)PNS
命名空间(e=None,tf=None)
在这里,您可以看到您的参数名称空间被替换为e
和tf
,这表明在缩写中添加-
实际上替换了参数名称(这只是我的猜测,但结果是正确的)
我认为这是PATC部分的一个bug——它非常混乱,文档没有任何说明。哇,你是一个救生员-它工作!我对pdb的第一次介绍也是如此;否则我永远也不会抓到这个。非常感谢你!我总是试着把pdb推给初学者,因为你越喜欢它,你就越容易调试像这样的问题,即使是在其他人的代码中。看起来
plac
试图在选项上过于“聪明”。几年前(在Py 2.4天内),我通过plac
被介绍到argparse
,所以它已经存在很长时间了。但这是一个人的项目。使用argparse
我鼓励人们包括print(args)
调试,这样他们就可以在没有任何假设的情况下查看名称空间
;但是这里无法直接访问。单击
似乎是argparse
最受欢迎的替代方案,至少在那些想要更友好的前端的人中是如此。