Eddy
Eddy

Reputation: 6861

Python C-API equivalent of "python -m module_name" (python 2.7)

Having looked the Python C-API documentation, I cannot figure out a way to execute a python script given just the name of the module (from c or c++ code).

Is there an equivalent C-API function for python -m module_name?

I am using Python 2.7 embedded in a C++ executable.

Upvotes: 1

Views: 67

Answers (1)

DavidW
DavidW

Reputation: 30912

This is done with the standard library runpy module (documentation; code). You just need to call the (undocumented) function runpy._run_module_as_main(module_name).

The C API code used by the Python interpreter just imports that module, gets the _run_module_as_main attribute and calls it (i.e. uses runpy as a Python module).

Upvotes: 1

Related Questions