python - Sphinx autodoc 使用 mock 卡在 random.choice()
问题描述
这是我最初在这篇文章中提出的一个问题的延续:random.choice error due to np.linspace and np.logspace
在这个问题中,我已将问题简化到最低限度,并且能够重现 Sphinx 挂在 random.choice() 函数上的问题。
这是文件 randor_test.py 中的 Python 代码;它在 PyCharm 中运行:
import random
import numpy as np
def rand_test(svr_C, svr_gamma):
"""This is test docstring
#. item one
#. item two
"""
ml_params = {'C': random.choice(svr_C), 'gamma': random.choice(svr_gamma)}
return ml_params
svr_C = list(np.linspace(50, 300, 10))
svr_gamma = list(np.logspace(-4, -2, 3))
rand_result = rand_test(svr_C, svr_gamma)
for i in rand_result:
print(i, rand_result[i])
我设置了 Sphinx 目录并按照这篇文章中的所有说明进行操作:Sphinx 入门...
运行后make html
我收到以下错误:
WARNING: autodoc: failed to import module 'randor_test'; the following exception was raised:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sphinx/ext/autodoc/importer.py", line 32, in import_module
return importlib.import_module(modname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/kellihed/PAC_projects/basic_0629/src/randor_test.py", line 19, in <module>
rand_result = rand_test(svr_C, svr_gamma)
File "/Users/kellihed/PAC_projects/basic_0629/src/randor_test.py", line 10, in rand_test
ml_params = {'C': random.choice(svr_C), 'gamma': random.choice(svr_gamma)}
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/random.py", line 261, in choice
raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence
我的文件结构遵循建议的布局:
|--docs
| |
| |--Makefile
| |--build
| | |
| | |--doctrees
| | |--html
| | |--_static
| | |--genindex.html
| | |--index.html
| | |--modules.html
| |--make.bat
| |--source
| |
| |--_static
| |--_templates
| |--conf.py
| |--index.rst
| |--modules.rst
| |--randor_test.rst
|
|--src
| |__pychache__
| | |
| | |--randor_test.cpython-37.pyc
| |
| |--randor_test.py
我在 conf.py 中有以下忽略导入语句:
autodoc_mock_imports = ["random", "numpy"]
下面是我的 conf.py 文件:
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join('..', '..', 'src')))
# -- Project information -----------------------------------------------------
project = 'random_test'
copyright = '2020, DK'
author = 'DK'
# The full version, including alpha/beta/rc tags
release = '0.1'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc']
autodoc_mock_imports = ["random", "numpy"]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
解决方案
您拥有在 Sphinx 导入时将始终执行的模块级代码randor_test
。svr_C = list(np.linspace(50, 300, 10))
不会直接导致错误,但结果是svr_C
一个空列表。
执行时发生错误random.choice(svr_C)
。我认为random
inautodoc_mock_imports
没有任何区别(它是一个始终可用的内置标准模块)。我认为最好的解决方案是将模块级代码放在一个if __name__ == '__main__'
块中。
推荐阅读
- docker - Windows Server 2016 上的容器到容器访问 Windows 容器问题
- javascript - 打字稿:合并由 Object.defineProperty 创建的对象
- microsoft-cognitive - 在 js 中使用来自 microsoft-cognitiveservices-speech-sdk 的 tts
- python - 带有 SQS 代理和死信队列的 Celery
- angular - 如何更改 angular-cli css bundle 生成的资源路径?
- matlab - 无法在 Matlab 中使用“datetime”数据类型绘制“errorbar”
- powershell - 调用全局函数
- javascript - 在不影响源映射的情况下操作 JavaScript AST
- sapui5 - SAPUI5 上传集合删除未触发(UI5 lib 1.44)
- java - 如何将谷歌地图标记的纬度和经度发送到 Firebase - android?