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

时间:2016-07-13 21:37:52

标签: shared-libraries argv python-c-api maple

我试图使用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'

1 个答案:

答案 0 :(得分:1)

cmd_args模块仅解析verbosequietoutputmax-memory个参数。假设只有output与您相关,您可以在Py_Initialize()之后使用PySys_SetArgv设置值。

char **argv = {"", "-o", "path/to/output/file"};
PySys_SetArgv(3, argv);