AttributeError with embedded Python
pladow at gmail.com
Wed Jul 2 17:16:05 CEST 2008
I posted this on c.l.p, but with the high level of traffic there, I
thought I'd try to post here as well.
I've embedded Python in an extension for a program we are using here
at work. And I'm a bit stumped as to why I am getting an
AttributeError only in the embedded Python.
First, a bit of what I am doing. We use a simulator for a
microprocessor we are using in our systems. Our simulator allows for
extensions that can be loaded as shared libraries. Rather than code
the entire extension in C/C++, I would like to make use of Python to
script the extension.
So, I first initialize Python (examples below leave out the error
checking, but it is there):
And then I make sure the script directory is in the path with
Py_GetPath() and PySys_SetPath().
Finally, I import the script (say it is in a local file 'script.py'):
pName = PyString_FromString("script");
pModule = PyImport_Import(pName);
Once the module is imported, I get objects to the functions in the
script I want to call later, which I do with:
pName = PyString_FromString("foo");
pFunc = PyObject_GetAttr(pModule, pName);
Later, I come back and call the function:
pResult = PyObject_CallObject(pFunc, NULL);
And it always fails whenever I iterate of a list. Say for example my
Python function is:
a = ['cat', 'window', 'defenstrate']
for x in a:
print x, len(x)
Now, the function runs, i.e. I see the output ("cat 3\nwindow
6\ndefenstrate 11\n"), but I always get the following error message:
Traceback (most recent call last):
File "./script.py", line 3, in foo
for x in a:
AttributeError: 'module' object has no attribute 'reset'
Now, if I run this exact same script using Python standalone, i.e.:
Python 2.3.4 (#1, Nov 20 2007, 15:18:15)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import script
(I know, and old version of Python. But we are stuck with it because
our processes require us to validate a newer version if we change.)
This works great. What is the difference? Why does it work in one
context but not the other?
"To love for the sake of being loved is human; to love for the sake
of loving is Angelic." -- Alphonse de Lamartine
More information about the Python-list