繁体   English   中英

如何阻止pydoc在f字符串上出错?

[英]How to stop pydoc from erroring out on f-strings?

我正在使用pydoc自动生成文档。 我的代码是为Python 3.6编写的。

它会因以下错误而出错: <type 'exceptions.SyntaxError'>: invalid syntax 每当我使用f-string时,它就会发生。

你能帮我弄清楚为什么吗?

当我运行which pydoc我得到: /usr/local/bin/pydoc

样例代码

这是一些示例代码,称为test.py

"""This is a docstring."""


def my_method():
    """Another docstring."""
    print(
        f"This is a really really really really really really really really "
        f"really really really really really long string."
    )
    pass

结果

当我在上面运行pydoc test时,输出为: problem in ./test.py - <type 'exceptions.SyntaxError'>: invalid syntax (test.py, line 7) 注意:此行是f-string

当(从ipython内部)我说: help('test') ,这是输出:

Help on module test:

NAME
    test - This is a docstring.

FUNCTIONS
    my_method()
        Another docstring.

FILE
    /Users/james.braza/code/wpi-aladdin-syringe-pump/test.py

因此,自动文档适用于Python的help()但不适用于pydoc

pydoc可执行文件是一个脚本,可能正在调用其他版本的Python。 (运行which pydoc并检查脚本可能会帮助您确认这一点。)为确保您使用python3.6 +运行pydoc,请尝试

/path/to/python3.6 -m pydoc test

好的,事实证明我的问题很琐碎。 @unutbu在评论中指出了它,然后在这里回答 我需要通过Python 3.6或更高版本运行pydoc脚本。

对我来说: python3 -V返回Python 3.7.3

因此运行: python3 -m pydoc test效果很好!

Help on module test:

NAME
    test - This is a docstring.

FUNCTIONS
    my_method()
        Another docstring.

FILE
    /Users/james.braza/code/wpi-aladdin-syringe-pump/test.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM