外部程序调用C共享库的argc和argv

Argc and argv for external program calling C shared library

我正在尝试使用 Maple 来调用调用 Python 的 C 共享库。通常我需要在 main 中设置 argc 和 argv,但由于这是调用共享库的另一个程序(Maple),我没有 main 函数(或者我应该有一个?)。那argc和argv应该怎么设置呢?

这是我得到的错误:

Traceback (most recent call last):
  File "/home/shiningsun/maple/rhf.py", line 9, in function
    mol.build()
  File "/share/apps/pyscf/v11/pyscf/gto/mole.py", line 1638, in build
    return self.build_(*args, **kwargs)
  File "/share/apps/pyscf/v11/pyscf/gto/mole.py", line 1533, in build_
    _update_from_cmdargs_(self)
  File "/share/apps/pyscf/v11/pyscf/gto/mole.py", line 2297, in _update_from_cmdargs_
    opts = cmd_args.cmd_args()
  File "/share/apps/pyscf/v11/pyscf/gto/cmd_args.py", line 25, in cmd_args
    (opts, args_left) = parser.parse_args()
  File "/share/apps/anaconda2/lib/python2.7/optparse.py", line 1382, in parse_args
    rargs = self._get_args(args)
  File "/share/apps/anaconda2/lib/python2.7/optparse.py", line 1364, in _get_args
    return sys.argv[1:]
AttributeError: 'module' object has no attribute 'argv'

cmd_args 模块仅解析 verbosequietoutputmax-memory 参数。假设只有 output 与您相关,您可以在 Py_Initialize().

之后使用 PySys_SetArgv 设置值
char **argv = {"", "-o", "path/to/output/file"};
PySys_SetArgv(3, argv);