Executing .pyc using python c api

Stefan Behnel stefan_ml at behnel.de
Tue Nov 29 03:15:41 EST 2011


Mrinalini Kulkarni, 29.11.2011 08:34:
> I need to run .pyc files using python c api. if i do PyImport_Import it
> executes the script. However, i need to pass a set of variables and their
> values which will be accessed from within the script. How can this be done.

Assuming you have the source as well, you should change your script to be 
usable as an importable module as well as an executable script. Here is how:

http://docs.python.org/tutorial/modules.html#executing-modules-as-scripts

If you do not have the source, however, you are a bit out of luck. In that 
case, what so you mean by "pass a set of variables"? Do you mean command 
line arguments, or do you want to pass in global names that the module can 
use? In the first case, you may need to modify sys.argv. If the latter, you 
could change the globals() of the imported module. However, both are clumsy 
solutions, and if possible at all, you should change the module source code 
instead.

Stefan




More information about the Python-list mailing list