[英]python-sphinx documenting scripts with no py extension
Apologies - this is a complete n00b question:道歉 - 这是一个完整的 n00b 问题:
I have a couple of python scripts with no .py extension.我有几个没有 .py 扩展名的 python 脚本。
How do I convince Sphinx that it should document that script?我如何说服 Sphinx 它应该记录该脚本? Example error:
示例错误:
/home/XXX/YYYYY/development/dst/build/index.rst:25: (WARNING/2) autodoc can't import/find module 'site_scons.random', it reported error: "No module named random", please check your spelling and sys.path /home/XXX/YYYYY/development/dst/build/index.rst:25: (WARNING/2) autodoc can't import/find module 'site_scons.random',它报告错误:“No module named random”,请检查您的拼写和 sys.path
In order for your script to be a module, it needs to include the .py
suffix. 为了使您的脚本成为模块,它需要包含
.py
后缀。 From the Python docs : 从Python文档 :
A module is a file containing Python definitions and statements.
模块是包含Python定义和语句的文件。 The file name is the module name with the suffix
.py
appended.文件名是附加后缀
.py
的模块名称。
Without giving it the suffix, Sphinx won't be able to import it to generate documentation using automodule
. 如果不给它后缀,Sphinx将无法导入它以使用
automodule
生成文档。
If you don't want your script to end with a .py
file extension you can also try the following.如果您不希望脚本以
.py
文件扩展名结尾,您还可以尝试以下操作。
You can write your original script into a .py
file and create another executable (bash script) file that just executes your .py
file.您可以将原始脚本写入
.py
文件并创建另一个仅执行.py
文件的可执行(bash 脚本)文件。 That way, you can document the script (the .py
) file using sphinx and still execute it through the other executable file.这样,您可以使用 sphinx 记录脚本(
.py
)文件,并仍然通过其他可执行文件执行它。
As mentioned by @dirn, a module requires a .py
extension to be considered as such.正如@dirn 所提到的,一个模块需要一个
.py
扩展名才能被视为这样。
An alternate solution that I think is cleaner, though it requires more work:我认为更干净的替代解决方案,尽管它需要更多的工作:
links
on your Sphinx root folder (that is, it will be a sibling to source
and build
)links
目录(也就是说,它将是source
和build
的同级目录).py
to their names.py
conf.py
, under Path setup
: sys.path.insert(0, os.path.abspath('../links'))
conf.py
上的 Python PATH Path setup
: sys.path.insert(0, os.path.abspath('../links'))
.. automodule:: my_command
to have your script read as a module and documented... automodule:: my_command
东西将您的脚本作为模块读取并记录下来。 A sample project would look like this:示例项目如下所示:
proj_root/
proj_root/doc # Sphinx root
proj_root/doc/build
proj_root/doc/links # Remember to version this
proj_root/doc/links/my_command.py -> ../../bin/my_command
proj_root/doc/source
proj_root/doc/source/conf.py
proj_root/bin
proj_root/bin/my_command # Actual code
The advantage I see for this method is that you do not polute your bin
directory with .py
files that are just duplicates of the actual scripts.我认为这种方法的优点是您不会用
.py
文件污染您的bin
目录,这些文件只是实际脚本的副本。
One could probably also try to hack this through the imp
module to get it, but I think that would be uglier.人们可能也可以尝试通过
imp
模块破解它以获取它,但我认为那会更丑陋。 I haven't tried that.我没试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.