python:阻止导入的模块解析命令行参数

费利克斯

我正在使用外部python模块,该模块不是我编写的,因此无法更改。这个称为magnum(http://micromagnum.informatik.uni-hamburg.de)的模块处理所有可选的命令行参数。这里有一个例子来说明这种行为:

script1.py

#!/usr/bin/env python
import magnum

执行脚本会产生:

>>> ./script1.py -h
[WARNING] - Python Imaging Library not found!
[WARNING] - -> This means that the ImageShapeCreator and related classes are not available!
[   INFO] - Imported FFTW wisdom from file
Usage: scipt1.py [options]

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit

  Hardware options:
    Options that control which hardware is used.

    -g GPU_ID           enable GPU processing (using 32-bit accuracy) on cuda
                    device GPU_ID. The simulator will fall back to CPU
                    mode if it was not compiled with CUDA support or when
                    no CUDA capable graphics cards were detected.
    -G GPU_ID           enable GPU processing (using 64-bit accuracy) on cuda
                    device GPU_ID. TODO: Describe fallback behaviour.
    -t NUM_THREADS, --threads=NUM_THREADS
                    enable CPU multithreading with NUM_THREADS (1..64)
                    threads. This parameter instructs the fftw library to
                    use NUM_THREADS threads for computing FFTs.

  Logging options:
    Options related to logging and benchmarking.

    -l LEVEL, --loglevel=LEVEL
                    set log level (0:Debug, 1:Info, 2:Warn, 4:Error,
                    5:Critical), default is Debug (0).
    --prof              Log profiling info at program exit.

  Parameter sweep options:
    These options have only an effect when the simulation script uses a
    Controller object to sweep through a parameter range.

    -p RANGE, --param-range=RANGE
                    select parameter set to run, e.g. --param-range=0,64
                    to run sets 0 to 63.
    --print-num-params  print number of sweep parameters to stdout and exit.
    --print-all-params  print all sweep parameters to stdout and exit.

  Miscellanous options:
    --on_io_error=MODE  Specifies what to do when an i/o error occurs when
                    writing an .omf/.vtk file. 0: Abort (default), 1:
                    Retry a few times, then abort, 2: Retry a few times,
                    then pause and ask for user intervention

现在,我想编写一个小的脚本,该脚本带有自己的命令行参数,然后使用magnum模块执行一些小的计算。我想使用argparse解析参数。但是,argparse的优先级似乎比此外部模块的参数处理低,并且我自己的参数无法识别:

script2.py

#!/usr/bin/env python
import argparse
import magnum
parser = argparse.ArgumentParser(
        description='TEST')
parser.add_argument('--x',
        help='test_arg')
args = parser.parse_args()
print args.x

调用它:

>>>./scrip2.py --x 3
[WARNING] - Python Imaging Library not found!
[WARNING] - -> This means that the ImageShapeCreator and related classes are not available!
[   INFO] - Imported FFTW wisdom from file
Usage: test.py [options]

test.py: error: no such option: --x

我在大酒瓶之前或之后导入argparse都没有关系。如果我不导入大酒瓶,则argparse可以工作:

script3.py

#!/usr/bin/env python
import argparse
parser = argparse.ArgumentParser(
        description='TEST')
parser.add_argument('--x',
        help='test_arg')
args = parser.parse_args()
print args.x

执行它会产生:

>>>./scrip2.py --x 3
3

我的问题是:如何在不编辑magnum的情况下停止magnum的命令行参数处理?

光学错觉

虽然我认为没有任何“好的”解决方案,但您可以通过猴子补丁argparse使之成为nop:

class EmptyParser():
    def parse_args():
        return
    ... (more redirects for add_argument)
argparse.ArgumentParser = EmptyParser
import magnum

另外,您可以将导入的numnum包装在一个函数中,并通过该函数创建一个接口,并在magnum解析它们之前创建参数。

def magnum(param1, param2):
    sys.argv = [param1, '--x', param2]
    import magnum

不过,这两种方法都是超级hacky。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从具有位置命令行参数的Python模块导入函数

来自分类Dev

在Python中,没有导入的命令行参数?

来自分类Dev

解析带引号的命令行参数

来自分类Dev

使用argparse解析命令行参数

来自分类Dev

Windows命令行参数解析

来自分类Dev

可以从命令行导入Python模块,但不能从PyCharm导入

来自分类Dev

Sublime Text 3无法导入python模块,但可以从命令行导入吗?

来自分类Dev

可以从命令行导入Python模块,但不能从PyCharm导入

来自分类Dev

从命令行导入Python

来自分类Dev

如何阻止用户查看命令行参数?

来自分类Dev

在命令行中启动脚本时导入模块

来自分类Dev

Python不解析命令行

来自分类Dev

Python不解析命令行

来自分类Dev

Python命令行参数:调用函数

来自分类Dev

Python中的多个命令行参数

来自分类Dev

Python中的迭代命令行参数

来自分类Dev

Python命令行(参数太少)

来自分类Dev

Python命令行参数Linux

来自分类Dev

Python:命令行参数未读?

来自分类Dev

Python optparse命令行参数

来自分类Dev

Python命令行(参数太少)

来自分类Dev

Python中的迭代命令行参数

来自分类Dev

使用python的命令行参数

来自分类Dev

Python命令行参数Try / Except

来自分类Dev

--debug Python中的命令行参数

来自分类Dev

Python中的命令行参数

来自分类Dev

如何从 Python 读取命令行参数?

来自分类Dev

截断 Python 命令行参数

来自分类Dev

内联python脚本的命令行参数?