gpt4 book ai didi

python 狮身人面像 "the module executes module level statement and it might call sys.exit()."

转载 作者:行者123 更新时间:2023-12-03 17:18:27 26 4
gpt4 key购买 nike

我尝试使用 Sphinx 来记录我的代码。但我看到了错误:

the module executes module level statement and it might call sys.exit().



我发现这个错误与代码有关:
import argparse
# parse command line arguments
parser = argparse.ArgumentParser(description='AWS VPN status checker.')
parser.add_argument('account', type=str, help='AWS account name.')
parser.add_argument('region', type=str, help='region of VPN tunnel.')
parser.add_argument('ipaddress', type=str, help='Tunnel IP address.')
parser.add_argument("-d", "--debug", help="debug", action="store_true")
args = parser.parse_args()

我认为它与导入模块时的“副作用”有关。

为什么它不好,我该如何解决这个问题?

最佳答案

Sphinx 警告是一种效果,由前面的 parse_args() 引起。错误。 arg_parse()函数被调用,在它预期解析的参数中发现一个正常错误,并且存在。

Invalid arguments

While parsing the command line, parse_args() checks for a variety of errors, including ambiguous options, invalid types, invalid options, wrong number of positional arguments, etc. When it encounters such an error, it exits and prints the error along with a usage message:


最可能的原因 parse_args()退出 在生成 Sphinx 文档时 , 是因为你真正调用的是 sphinx-buildmake html .因此,在您的 python shell 上执行的是以下签名:

sphinx-build

Synopsis

sphinx-build [options] < sourcedir > < outputdir > [filenames …]


这意味着您可能没有使用您编码的参数执行脚本 ArgumentParser要求。要么运行 sphinx-buildmake html包括命令行参数 arg_parse()需要 ArgumentParser() ,或者不要调用 arg_parse()生成文档时。

How to fix this?


一种可能的方法如下: entry_script.py :
from sys import argv
from pathlib import Path

import cmd_line_module

# Checking what the command line contains can be useful.
print(argv)

EXAMPLE_ARGS = ['-i', '../in_dir_test', '-o', 'out_dir_test']

# Script.
if __name__ == "__main__":

# default Namespace
print(cmd_line_params.args)

# command-line
cmd_line_module.set_args()
print(cmd_line_params.args)

# test case
cmd_line_module.set_args(EXAMPLE_ARGS)
print(cmd_line_params.args)

# Sphinx-build or make.
elif Path(argv[0]).name == "sphinx-build" or Path(argv[0]).name == "build.py":

cmd_line_module.set_args(EXAMPLE_ARGS)

# Module only.
else:
cmd_line_module.set_args(EXAMPLE_ARGS)

cmd_line_module.py :
import argparse


_DEFAULT = argparse.Namespace(in_dir=None, out_dir=None)
args = _DEFAULT


def command_line_args():

parser = argparse.ArgumentParser(prog='entry_script', description='Does magic :) .')
parser.add_argument("-i", "--in_dir", help="Input directory/file. Use absolute or relative path.")
parser.add_argument("-o", "--out_dir", help="Output directory. Use absolute or relative path.")

return parser


def set_args(cmd_line=None):

parser = command_line_args()
global args
args = parser.parse_args(cmd_line)

关于解决方案的一些注释可能对读者有用:
1. cmd_line_module.py维护 args作为模块级别的变量,类似于 argparse tutorial尽可能的例子。 argparse 的特定 Sphinx 扩展可以找到 on this thread .
2. 使用默认 Namespace对于 args可能很方便,它作为建议包含在内。 (预期的默认值可以帮助测试导入模块)。
3. 测试 __main __sphinx-build可能没有必要,具体取决于 3 if包括测试只是为了给问题添加上下文。
4. 使用 DEFAULT_ARGS显示如何使用 parse_args()无需阅读 sys.argv ,以及如何运行 sphinx-build分配使用 if __name __ == "__main __": (如果您觉得方便,出于任何原因)...
5. sphinx-build 的名称和路径脚本可能因操作系统而异。
最后说明:如果您喜欢编写自初始化其变量的模块(如我),请特别注意运行 parse_args()在导入可能具有依赖于它的变量的模块之前。

6.1. More on Modules

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. (They are also run if the file is executed as a script.)

关于 python 狮身人面像 "the module executes module level statement and it might call sys.exit().",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com