I'm using an external python module, which is not written by me therefore cannot be changed. This module, called magnum (http://micromagnum.informatik.uni-hamburg.de), processes all optional command line arguments. Here an example to illustrate the behavior:
script1.py
#!/usr/bin/env python
import magnum
executing the script yields:
>>> ./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
Now I want to write a small script that takes its own command line arguments and then uses the magnum module to perform some small calculation. I would like to use argparse to parse the arguments. However, argparse seems to have a lower priority than the argument processing of this external module and my own arguments fail to be recognized:
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
Calling it:
>>>./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
It does not matter if I import argparse before or after magnum. The argparse works if I don'f import magnum:
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
Executing it yields:
>>>./scrip2.py --x 3
3
My question is: How can I stop magnum from processing my command line arguments, without editing magnum?
import.parse_args) and setsys.argvto[]or whatever magnum needsmagnumis meant to be imported. Is it perhaps a script frontend to an importable module?Any script that loads MicroMagnum using the ‘import magnum’ command is subject to these command line arguments:. So thisimportis meant to setup a computing environment.