Making print compatible between Python 2 and 3 (BGE 2.49 and 2.5x)

If you read down to moerdns thread you will see that print() works in both versions. So my workaround is obsolete.

Hi there,

I know some scripters are trying to write scripts that runn in 2.49 and 2.5x.
One of the problems is that print with 2.49 (Python 2.x) is a keyword while in BGE 2.53 (Python 3.x) print is a function.

That means all prints work work in one version but not the other.

There is a workaround. Unfortunatly need to replace all print statements :(. But you get a function that works in both versions.

Add this code to the initialization of your script:

# to make print available to Python 2 and 3
# just use cprint(...) instead of print ... or print(...)
cprint = globals()["__builtins__"]["print"]

this estalishes a new function cprint() (which means compatible print). This function is a reference to the builtin function (or keyword). It works with Python 2.x and Python 3.x making cprint running with both versions.

All you need to do is to replace

print "Hello world!"


cprint("Hello world")

By the way:

  • you can call the function as you want, except “print”. This is a keyword in Python 2 and can’t be redefined there.

If I know a better method please post it here.
If you know methods to make other syntax changes compatible, let me know (here or in a separate thread), thanks :D.

I hope it helps


if you use blender 2.49 with python 2.6, than you can also use print as function.


works for me in 2.49 AND 2.5

greetings, moerdn

hehe, I think print is the least of the problems :slight_smile:

I’m thinking if there would be a way of making 1 version of a blender game without having to change the
python scripts at all, just write the same code for both 2.49b and 2.5, it would make it easier for all users when distributing their blender game.

Everything seems to work perfectly from transfer, except for the “OB” part when finding an objects in .objects. For that you need to check the version of blender to verify weather it needs it or not.

There’s only a few things that 2.5 and python 3.x has changed. Well, at least to my knowledge and considering the things I need, it might not be much. Moguri’s How-To on how to port 2.49b to 2.5, showed me that not much of the basic API has been changed.

See ya!

ps: I confirm the post above

Hi moerdn,

You are rigth. I never tried print() in 2.49. So this thread is quite useless :S. I can use this syntax directly :).


@Linkxgl: I know it is a small problem, but it is a solvable problem :smiley:

Not useless! We could think of a general abstraction to get rid of version differences. My code already runs on both 2.5 AND 2.49 but I don’t think this is the way it should be done…

best regards, moerdn.

This could still be handy for builds of 2.49 that aren’t using Python 2.6. For example, taking a peek at, I see that the OSX packages are using either Python 2.3 or Python 2.5, which do not support print as a function.