Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Executing .pyc using python c api

110 views
Skip to first unread message

Mrinalini Kulkarni

unread,
Nov 29, 2011, 2:34:01 AM11/29/11
to pytho...@python.org
Hi

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.

thanks,

Stefan Behnel

unread,
Nov 29, 2011, 3:15:41 AM11/29/11
to pytho...@python.org
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

Ulrich Eckhardt

unread,
Nov 29, 2011, 4:02:31 AM11/29/11
to
I don't think what you want is possible, due to a think-o in your
design. Let me explain...
Firstly, .pyc files are basically the same as .py files, only in a
different presentation. Then, PyImport_Import is basically the same as
using "import" in a Python program. Now, and that is where your fault
lies, importing a module actually means executing that module! For
example, the definition of a function is code that when executed will
cause a function to be created and attached to the current scope with
the according name. This is what makes it so easy to implement local
functions that are parametrized by arguments to the outer function.
Still, a function is not something that is "static", like in C or Java,
but rather the result of executing its function definition.

Now, how to get around this? The specialty about the import is that the
__name__ attribute is not set to "__main__", upon which many scripts
already react. So, in order to "prevent execution" (in the sense that
you probably mean), you simply wrap the according code in a function.
The function definition will then be executed, giving you a function
that you can call with the according parameters, but the function itself
will not be executed automatically. If you want that to happen when
executing the .pyc file directly, check the content of __name__ and call
the function if it is "__main__".

Note that another approach would be introspection, traversing through
the namespaces to find out those parameters, but I would consider this
solution as hackish if the one above is feasible.

Good luck!

Uli

88888 Dihedral

unread,
Nov 29, 2011, 9:19:55 AM11/29/11
to
Please use psyco and pyrex and C or whatever that can read saved results in a file, or just learn how to replace a hash or a sort in python's build in library of better speed, don't do reference overheads in
those c type variables that won't overflow and underflow and used by other objects in python. Not trivial but well documented to cheer for a race!


0 new messages