469,906 Members | 2,183 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,906 developers. It's quick & easy.

AttributeError with embedded Python

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:

def foo():
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
cat 3
window 6
defenstrate 11
(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?

Jul 1 '08 #1
0 853

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Reinhard Koenig | last post: by
2 posts views Thread by GrelEns | last post: by
13 posts views Thread by Alexander May | last post: by
20 posts views Thread by Jack | last post: by
4 posts views Thread by Nikhil | last post: by
reply views Thread by Peter LaDow | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.