From: Christopher Lloyd on
Hello all,

I'm new to Python and new to this list, although I've done some digging in the archives and already read up on the problem I'm about to describe.

I'm a relatively inexperienced programmer, and have been learning some basic C++ and working through the demos in Ron Penton's "MUD Game Programming" book. In it, Python modules are run from inside a C++ program.

The problem that I'm having is making the Python part work. Take the following code:

// This program shows you how to integrate Python in a very basic manner

#include <iostream>
#include <string>
#include "Python.h"

int main()
{
std::cout << "Starting Python Demo Test" << std::endl;

Py_Initialize(); // initialize python

std::string str;
std::getline( std::cin, str );
while( str != "end" )
{
PyRun_SimpleString( const_cast<char*>( str.c_str() ) );
std::getline( std::cin, str );
}

Py_Finalize(); // shut down python

std::cout << "Demo Complete!" << std::endl;

return 0;
}

If I try to compile this in MS Visual C++ 2008 (debug mode), I get the following error:

LINK : fatal error LNK1104: cannot open file 'python26_d.lib'

>From my reading, it looks like there's a problem with compiling in release mode or debug mode either in C++ or in Python. At the moment, I'm using the Python Windows .exe download. I'm not using version 2.6 for any particular reason - I'm also trying 2.2 and 2.3.

So I don't have the Python source code downloaded (and I'm not entirely sure what to do with it if I do download it, since the instructions for the .tar file are for Linux, not windows). The Windows executables for 2.2 and 2.3 came on a CD with the book, so it seems clear that the author thought that the source code wasn't required anyway.

So, what happens if I try compiling the above C++ in release mode? Actually, nothing - It compiles just fine. However, upon running the resulting program, my command line box displays the following:

> Starting Python Demo Test

That's all. The program has hung halfway through and the test isn't completed.

It's been suggested that I replace the first part of my C++ code with the following, and then try to compile in release mode:

#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif

I've tried this, and it compiles successfully but when run, the program is the same - It doesn't work.

I've correctly set up all my library files and link (at least, lets assume its not that, since I've already spent several hours checking and re-checking that).

I'd be very grateful for any help or asvice people might have on this.

Thanks,

Chris Lloyd

--


From: Irmen de Jong on
Christopher Lloyd wrote:
> Hello all,
>
> I'm new to Python and new to this list, although I've done some digging in the archives and already read up on the problem I'm about to describe.
>
> I'm a relatively inexperienced programmer, and have been learning some basic C++ and working through the demos in Ron Penton's "MUD Game Programming" book. In it, Python modules are run from inside a C++ program.
>
> The problem that I'm having is making the Python part work. Take the following code:
[...]

Can't help you on your immediate problem, but is there any reason why you would go the
route of embedding python in C++ ? Why not just stick to (pure) Python?
Embedding C or C++ stuff as extension modules in Python (if you really need to do this)
is easier than the other way around, in my experience.

--irmen
From: Gabriel Genellina on
En Tue, 13 Oct 2009 18:08:53 -0300, Christopher Lloyd
<llocr(a)btinternet.com> escribi�:

> #include <iostream>
> #include <string>
> #include "Python.h"
>
> int main()
> {
> std::cout << "Starting Python Demo Test" << std::endl;
>
> Py_Initialize(); // initialize python
>
> std::string str;
> std::getline( std::cin, str );
> while( str != "end" )
> {
> PyRun_SimpleString( const_cast<char*>( str.c_str() ) );
> std::getline( std::cin, str );
> }
>
> Py_Finalize(); // shut down python
> std::cout << "Demo Complete!" << std::endl;
> return 0;
> }
>
> If I try to compile this in MS Visual C++ 2008 (debug mode), I get the
> following error:
>
> LINK : fatal error LNK1104: cannot open file 'python26_d.lib'

For a debug build of your program, you require a debug build of Python. A
release build should compile and link OK with the pre-built Python
libraries you got, as you already noticed.

> So, what happens if I try compiling the above C++ in release mode?
> Actually, nothing - It compiles just fine. However, upon running the
> resulting program, my command line box displays the following:
>
>> Starting Python Demo Test
>
> That's all. The program has hung halfway through and the test isn't
> completed.

Hung? Or waiting for you to input some Python expression to be evaluated?
That's what the std::getline( std::cin, str ) line does. Type 2+3 for
example - you should get the answer.

> It's been suggested that I replace the first part of my C++ code with
> the following, and then try to compile in release mode:
>
> #ifdef _DEBUG
> #undef _DEBUG
> #include <Python.h>
> #define _DEBUG
> #else
> #include <Python.h>
> #endif

No, don't do that. Just compile your application in release mode.

--
Gabriel Genellina

From: Ulrich Eckhardt on
Irmen de Jong wrote:
> [...] is there any reason why you would go the route of embedding python
> in C++ ? Why not just stick to (pure) Python? Embedding C or C++ stuff
> as extension modules in Python (if you really need to do this) is easier
> than the other way around, in my experience.

If you want to offer scriptability to your application, you obviously need
to embed a scripting language into your application, not the other way
around.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

From: Ulrich Eckhardt on
Christopher Lloyd wrote:
> I'm a relatively inexperienced programmer, and have been learning some
> basic C++ and working through the demos in Ron Penton's "MUD Game
> Programming" book. In it, Python modules are run from inside a C++
> program.
[...]
> If I try to compile this in MS Visual C++ 2008 (debug mode), I get the
> following error:
>
> LINK : fatal error LNK1104: cannot open file 'python26_d.lib'

My installation of Python 2.6 doesn't have the debug-compiled Python
libaries, I guess yours doesn't either. Simple solution: don't link to
them, link to the release version instead, even in your debug build.
Unfortunately it requires this the hack with #undefing/#defining _DEBUG.

If you want, you can get the sourcecode for Python, compile it and then use
the generated debug libraries, but I would say it isn't worth the hassle
for now.

> So, what happens if I try compiling the above C++ in release mode?
> Actually, nothing - It compiles just fine. However, upon running the
> resulting program, my command line box displays the following:
>
>> Starting Python Demo Test
>
> That's all. The program has hung halfway through and the test isn't
> completed.

If I understand the program correctly, it is waiting for you to enter
something. Each line is then given to Python to interpret, unless the line
is just the simple string "end". IOW, this isn't hanging or in any way
broken.

That said, it would hang if you feed it a file via input redirection, unless
the file ends with "end". I'd write the loop like this:

Py_Initialize();
while(getline(std::cin, line)) {
if(line=="end")
break;
Py_RunSimpleString(line.c_str()); // use line
}
Py_Finalize();

....and possibly add some error handling to the Py_* calls.


> I've correctly set up all my library files and link (at least, lets assume
> its not that, since I've already spent several hours checking and
> re-checking that).

Note about the linker setup: You don't have to specify the library to link,
that is done in pyconfig.h with 'pragma comment(lib,"python26.lib")'. This
is MSVC-specific though.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932