Python newbie trying to embed in C++

M

Marwan

Hello all,

I'm new to Python and just starting to learn it. For he needs of my
project, I need to call some specific methods in Python scripts from C++.

For now, I just compiled the example in the Python documentation about
Pure Embedding to try it out (
http://docs.python.org/2/extending/embedding.html#pure-embedding ).

I'm trying to test it on an extremely simple script called test.py which
contains the following:

def testPY( value ):
print('You input ', value )

def Hello():
print('Hello')


When I run the generated exe, I get errors about the functions not
existing...

TestPython.exe test Hello
AttributeError: 'module' object has no attribute 'Hello'
Cannot find function "Hello"


My Python version is 2.7.3 because that's the version used in the module
we need to access. And I'm using VS2010 SP1 for compiling my C++ because
that's the version used to generate our DLLs and EXEs.

Could anyone tell me why this is happening?

And I'd appreciate it if you could give me pointers to how to easily
call Python from C++.

Thanks in advance for your help,

Marwan
 
I

Ian Kelly

When I run the generated exe, I get errors about the functions not
existing...

TestPython.exe test Hello
AttributeError: 'module' object has no attribute 'Hello'
Cannot find function "Hello"

"test" is the name of a module in the standard library. My guess
would be that for some reason it's importing that module rather than
your test.py. Make sure that test.py can be found on your sys.path,
and perhaps change the name of your module to reduce confusion.
 
T

Terry Reedy

Hello all,

I'm new to Python and just starting to learn it. For he needs of my
project, I need to call some specific methods in Python scripts from C++.

For now, I just compiled the example in the Python documentation about
Pure Embedding to try it out (
http://docs.python.org/2/extending/embedding.html#pure-embedding ).

I'm trying to test it on an extremely simple script called test.py which
contains the following:

def testPY( value ):
print('You input ', value )

def Hello():
print('Hello')

I hope that the second def is not really indented in your original ;-).
When I run the generated exe, I get errors about the functions not
existing...

TestPython.exe test Hello
AttributeError: 'module' object has no attribute 'Hello'
Cannot find function "Hello"

My Python version is 2.7.3 because that's the version used in the module

The windows 2.7 on python.org is compiled with VS2008.
we need to access. And I'm using VS2010 SP1 for compiling my C++ because
that's the version used to generate our DLLs and EXEs.

Mixing VS compilers can be a problem, though I don't know if that is the
case here. Even if not, it might be for your real application.

You can try compiling 2.7.3 (or later from repository) with vs2010. I
know people have tried it. I presume it has been done. I don't know if
there is an (unofficial) vs2010 project file in the repository.

Python.org 3.3 *is* compiled with 2010. You can also try running the
module with that, possibly with the help of 2to3. It might not take too
work. The author of the module might be interested in a port anyway,
though maybe not. Or maybe extract just the part of the module you need
for conversion.

You might start with 3.3 for your tests and initial learning to make
sure that compiler mismatch is not a factor. When you get that to work,
then decide what to do.

I suppose the worst alternative might be to regenerate all the needed
dlls and exes with 2008.
 
M

Marwan Badawi

"test" is the name of a module in the standard library. My guess
would be that for some reason it's importing that module rather than
your test.py. Make sure that test.py can be found on your sys.path,
and perhaps change the name of your module to reduce confusion.
I just noticed that my reply went to the message sender and not to the
newsgroup, so I'm posting again: thanks, that was it. Just renaming my
test.py file solved the problem.

%M
 
G

Gisle Vanem

Marwan Badawi said:
I just noticed that my reply went to the message sender and not to the
newsgroup, so I'm posting again: thanks, I'll look into that.

Yes, I often do that too; i.e. I'm subscribed to (e-mail address removed)
and get all messages from comp.lang.python mirrored to the ML a bit later.
I prefer the mailing-list over comp.lang.python since my NNTP server
(eternal-september.org) is rather slow and my ISP has deprecated NNTP
long time ago.

I saw you uses Thunderbird on Windows. I'm not sure how it by default handles
a reply-to when there is no "Reply-to" field in the header. To the address in
"From" / "Sender" or what?

I wish the NNTP-mailing list gateway could add a "Reply-to: <[email protected]>".
Since I'm getting the messages via the ML, I think it would be logical that the replies
should by default go to the ML too.

--gv
 
M

Michael Torrie

I saw you uses Thunderbird on Windows. I'm not sure how it by default handles
a reply-to when there is no "Reply-to" field in the header. To the address in
"From" / "Sender" or what?

Thunderbird has a handy, "reply to list" button that works every time no
matter what the rely-to field is set to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,984
Latest member
PeakCBDGummiesReview

Latest Threads

Top