简体   繁体   English

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

[英]How to use Sphinx auto-documentation when Python file won't compile

This question is even harder today because I haven't had any luck using the search function on the Sphinx homepage today. 今天的问题更加棘手,因为我今天在Sphinx主页上使用搜索功能还没有运气。

I have a group of modules that I want to be documented from the docstrings. 我有一组要从文档字符串记录的模块。 However, these are not pure Python scripts. 但是,这些不是纯Python脚本。 They won't compile as is, because they are run from a C# application that creates a new variable in the executing scope. 它们不会按原样编译,因为它们是从C#应用程序运行的,该C#应用程序在执行范围内创建了一个新变量。

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). 对于Python编译器,看起来我有一个未定义的方法(从技术上讲,我一直这样做,直到C#创建IronPython脚本引擎并创建该方法)。

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? 如何使Sphinx忽略编译错误并仅生成我的文档?

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的替代方案(例如Epydoc)是否不必编译Python脚本来获取函数签名和文档字符串,那也将有所帮助。 Sphinx is the best looking documentation generator, but I'll abandon it if I have to. Sphinx是外观最好的文档生成器,但是如果需要的话,我会放弃它。

Well, you could try: 好吧,您可以尝试:

  • Wrapping the usage of injected_method in a try/except. 在try / except中包装injected_method的用法。
  • Writing a script that filters out all python-code that is run on import time, and feeds the result into Sphinx. 编写一个脚本,以过滤掉在导入时运行的所有python代码,并将结果输入Sphinx。
  • You could....ok, I have no more ideas. 您可以...。好吧,我没有其他想法了。 :) :)

Perhaps you could define injected_method as a empty function so that the documentation will work. 也许您可以将Injected_method定义为空函数,以便文档可以正常工作。 You'll need to make sure that the definition of injected_method that you're injecting happens after the new injected_method stub. 您需要确保要注入的injected_method的定义在新的injected_method存根之后发生。

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

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. 然后,我在搜索路径中创建了一个虚拟的Injection_method.py文件,该文件为空白。 I delete the dummy file during the build of my C# project to avoid any conflicts. 我在构建C#项目的过程中删除了虚拟文件,以避免发生任何冲突。

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

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