当 Python 文件无法编译时如何使用 Sphinx 自动文档

发布于 2024-08-06 05:04:38 字数 528 浏览 10 评论 0原文

今天这个问题更加困难,因为我今天没有使用 Sphinx 主页上的搜索功能。

我有一组模块想要从文档字符串中记录下来。然而,这些不是纯 Python 脚本。它们不会按原样编译,因为它们是从在执行范围中创建新变量的 C# 应用程序运行的。

对于 Python 编译器来说,我似乎有一个未定义的方法(从技术上讲,我确实如此,直到 C# 创建 IronPython 脚本引擎并创建该方法)。

当我运行:

sphinx-build -b html output/html

我得到:

NameError: name 'injected_method' is not defined

如何让 Sphinx 忽略编译错误并生成我的文档?

编辑:

如果有人知道 Sphinx 的替代品(如 Epydoc)是否不必编译 Python 脚本来获取函数签名和文档字符串,那也会很有帮助。 Sphinx 是最好看的文档生成器,但如果必须的话我会放弃它。

This question is even harder today because I haven't had any luck using the search function on the Sphinx homepage today.

I have a group of modules that I want to be documented from the docstrings. However, these are not pure Python scripts. They won't compile as is, because they are run from a C# application that creates a new variable in the executing scope.

To the Python compiler, it looks like I have an undefined method (which, technically I do, until C# creates the IronPython script engine and creates the method).

When I run:

sphinx-build -b html output/html

I get:

NameError: name 'injected_method' is not defined

How do I get Sphinx to ignore compilation errors and just generate my documentation?

EDIT:

If anybody knows if an alternative to Sphinx (like Epydoc) does not have to compile the Python script to get the function signatures and docstrings, that would be helpful as well. Sphinx is the best looking documentation generator, but I'll abandon it if I have to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟凡古楼 2024-08-13 05:04:38

好吧,你可以尝试:

  • 将injected_method 的用法包装在try/ except 中。
  • 编写一个脚本,过滤掉导入时运行的所有 python 代码,并将结果输入 Sphinx。
  • 你可以……好吧,我没有更多的想法了。 :)

Well, you could try:

  • Wrapping the usage of injected_method in a try/except.
  • Writing a script that filters out all python-code that is run on import time, and feeds the result into Sphinx.
  • You could....ok, I have no more ideas. :)
世界等同你 2024-08-13 05:04:38

也许您可以将injected_method定义为空函数,以便文档正常工作。您需要确保您要注入的injected_method 的定义发生在新的injected_method 存根之后。

#By empty function I mean a function that looks like this
def injected_method():
  pass

Perhaps you could define injected_method as a empty function so that the documentation will work. You'll need to make sure that the definition of injected_method that you're injecting happens after the new injected_method stub.

#By empty function I mean a function that looks like this
def injected_method():
  pass
谁的新欢旧爱 2024-08-13 05:04:38

好的,我找到了解决错误的方法。

在设置嵌入式脚本环境时,而不是使用:

ScriptScope.SetVariable("injected_method", myMethod);

我现在使用:

ScriptRuntime.Globals.SetVariable("injected_method", myMethod);

然后,在脚本中:

import injected_method

然后我在搜索路径中创建了一个虚拟的injected_method.py 文件,该文件是空白的。我在构建 C# 项目期间删除了虚拟文件以避免任何冲突。

Okay, I found a way to get around the Errors.

When setting up the embedded scripting environment, instead of using:

ScriptScope.SetVariable("injected_method", myMethod);

I am now using:

ScriptRuntime.Globals.SetVariable("injected_method", myMethod);

And then, in the script:

import injected_method

Then I created a dummy injected_method.py file in my search path, which is blank. I delete the dummy file during the build of my C# project to avoid any conflicts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文