python - 使用 plac 时出现 AttributeError:“命名空间”对象没有属性
问题描述
试图编写一个命令行函数,我被这个 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 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'
我真的不知道出了什么问题,这让我把头发扯掉了。非常感谢任何帮助,谢谢。
解决方案
如果您将其替换为:
@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)
在这里,您可以看到您的参数命名空间被替换为e
and tf
,这表明以某种方式将 a-
放入缩写实际上替换了参数名称(这只是我的猜测,但结果证明是正确的)。
我认为这是 plac 部分的一个错误——它非常令人困惑,并且文档没有说明任何关于此的内容。
推荐阅读
- dialogflow-es - Dialogflow API - 根据请求设置位置
- extentreports - 范围报告 - 线程“主”java.lang.NoSuchFieldError 中的异常:VERSION_2_3_23
- android - Xamarin.Andoid:将 ImageView 与 CanvasView 合并
- javascript - Javascript二进制字符串,转十六进制,转base58,转回十六进制,转回字符串,随机错误
- python - 将 Chrome 浏览器更新到最新版本后 Chrome 网络驱动程序崩溃
- python - 用 0 替换随机列表条目
- codeigniter - 我想将视图传递给一个字符串,并使用 codeigniter 作为电子邮件中的消息正文传递。有什么建议么?
- qt - 使用 QT Designer 控制 QCombobox 中的工具提示
- java - 从模式为 JAXB 绑定类生成 Jackson 属性
- c++ - 创建一个可以接收 board1[{1,1}]='X' 的类;? (方括号内的大括号)