By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,359 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,359 IT Pros & Developers. It's quick & easy.

AttributeError with embedded Python

P: n/a
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):

Py_Initialize();

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
script.foo()
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?

Thanks,
Pete
Jul 1 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.