【问题标题】:AttributeError while using plac: 'Namespace' object has no attribute使用 plac 时出现 AttributeError:“命名空间”对象没有属性
【发布时间】:2020-05-17 05:32:39
【问题描述】:

试图写一个命令行函数,我被这个 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'

我真的不知道出了什么问题,这让我把头发扯下来了。任何帮助都非常感谢,谢谢。

【问题讨论】:

    标签: python namespaces argparse


    【解决方案1】:

    如果你将其替换为:

    @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)
    

    在这里你可以看到你的参数命名空间被etf替换,这表明在缩写中加入-实际上替换了参数名称(这只是我的猜测,但它变成了是正确的)。

    我认为这是 plac 的一个错误——它非常令人困惑,并且文档没有说明任何关于此的内容。

    【讨论】:

    • 哇,你是救生员——它有效!我也是对 pdb 的第一次介绍;否则我永远不会抓住这个。非常感谢!
    • 我总是尝试将 pdb 推给初学者,因为你越熟悉它就越容易调试这样的问题,即使在其他人的代码中也是如此。
    • 看起来plac 试图对选项过于“聪明”。几年前(Py 2.4 天)我通过plac 被介绍给argparse,所以它已经存在了很长时间。但这是一个人的项目。对于argparse,我鼓励人们在调试过程中加入print(args),这样他们就可以在没有假设的情况下看到Namespace;但这里不能直接访问。
    • click 似乎是argparse 最受欢迎的替代品,至少在那些想要更友好的前端的人中。
    猜你喜欢
    • 2015-04-09
    • 2017-11-30
    • 2021-11-14
    • 2018-09-03
    • 2020-10-01
    • 2021-03-11
    • 2019-05-17
    • 2017-01-12
    • 2020-05-19
    相关资源
    最近更新 更多