How to add external python modules to the BGE so they work in the exported run time.

How the heck do you add a python module (psutil) to blender so I can use it in BGE. I can get it to work in the editor but not in the exported run time I don’t mean to complain but geez adding external methods should be like step 2 in your wiki or on the forums for python scripting, not how to make a rpg or how do I make the next greatest game. I know there are references to adding external modules, The more experienced users say it can be done, but alas not a single walk through on how to do it. I have no problem adding modules to python, adding them to the BGE should be trivial. I may have missed something but searching blender wiki, google and the blender artist forums I could find nothing that worked.

So here is my challenge to anyone and every one.

Create a blendfile with a simple scene that uses any nonblender external python module.
Export the runtime and verify that it works on another pc that does not have blender installed.
Tell me how the heck you did it.
Post your example files in your reply please!!!

Note. text based answers are not helpful unless your explain what you did in detail. So no generic answers like “Just import you module like you would in python” or “just copy your python libs folder over to the blender libs” or " You need to append your sys path".

Teaching works best by example hence the reason for teachers and home work in school.


This is a joke, isn’t it?

If you know a bit of Python you should know how to find other python modules. It does not matter if run it from bge, blender or directly.

No offense monster but show me in an example if it is so trivial. BTW support should not be considered a jole

In my signature (guides) you can find the BGE guide to Python coding. Here you can find the Basics of the relation between BGE and Python. It does not describe your specific problem. but I’m sure it will help you.

To find a solution to your question check the thread regarding script organization (at the same guides page).

To simply answer your question: place your module in the python search path.
You see this is a general Python thing and is not bge related.

Thank you for your reply Monster.

I had tried that and it worked but only in the embedded player, not in the standalone player or in the exported runtime.

I had also already read your script folder organizer looking for help but It looked to me to only apply to single scripts though and not entire external modules, but I will try again and see if I can figure it out.

An example blend would really really help though.

BTW I am using 2.66/5 on Ubuntu 12.10 and Windows 8

@josephbreiting - Hello, and welcome to the forums.

If you’re trying to simply use an external Python file with the BGE, you can just:

Set up a Python controller to run in Module mode, and for the function use the file’s name with the name of the function that you want to run (i.e. Player.Run).

If you’re trying to install an external Python library for use with the BGE (one that’s not just ‘drag and drop’, but rather needs to actually install into the BGE’s Python distribution), then it’s more complex.

EDIT: After looking at psutil, it looks like it needs to actually install, which is more difficult than just plopping a Python file or directory of Python files into the same directory as the blend file. I think Monster may have thought that you were just trying to run an external Python file, not install a Python module into Blender’s Python distribution.

Thank you solarlune you know what I’m talking about.

I am trying to install an external python library (psutil) into the BGE python distro. Do you know where would I could go to get information on how to do that.

BTW: how your blender game coming I’ve been following your blog and your indiedb page since it first appeared on indiedb. Hoping you’ll have another demo soon.

It is the same you place the packages/modules in the python search path.

I guess you want to distribute it with your game. If you have for example a folder called “python” to contain your python modules, you surely want to have it in your python search path. You can do that as described in the above thread.

To distribut psutils you can place them into folder “python” too .

Edit: you are right the topic with python search path is not documented that well. You really need to know what to look for.
Btw. The bge adds the folder where the starting .blend file resides to the python search path. That is the reason why you can place python files next to the blend.

Just to make sure I’m not confusing anyone.

This is not for loading a single py file in module mode, but to actually install a library for python in blender’s python

In normal python I install the library via commandline with “python install” while inside the downloaded psutil folder. Now I can import the module with “import psutil” while in python. In blender there is no way I found to install libraries. I did try copying the resulting files from my python3.3 /lib/site-packages to the blenders python /lib/site-packages folder. That didn’t work. I tried erasing my entire python folder in blender and replaced it with my python3.3 installations contents. That broke blender in windows so it wouldn’t even start. The only thing that did work was to copy my egg file and such for psutil into blenders /script folder but that only worked in the embedded player. I also tried to copy the psutil install to blenders root folder, my exported runtime root folder and still nothing.

Any help on how to do this?

It sounds like you want to be able to pack an external module into an exported runtime. I’ve been wondering that as well. It would be a lot nicer to have just one file for your game instead of a .exe and dozens of Python files.

@Monster: I don’t think that’s quite what he’s looking for. You mention placing the module in the Python search path, but then it won’t be packed in when you export the runtime. It’ll still remain separate.

Got it to work in Linux, Still not working on Windows, I get this error…

>>> import psutil
Traceback (most recent call last):
File “<blender_console>”, line 1, in <module>
File “C:\Users\Joseph\Desktop\blender-2.66-windows64\2.66\scripts\modules\”, line 76, in <module>
import psutil._psmswindows as _psplatform
File “C:\Users\Joseph\Desktop\blender-2.66-windows64\2.66\scripts\modules\”, line 16, in <module>
import _psutil_mswindows
ImportError: DLL load failed: %1 is not a valid Win32 application.

Getting it to work in linux required me to copy the contents of the linux folder in the build dir for psutil, not the contents of site-packages from the python3.3 install. It seems to require the .so files which do not get installed to the python dir when you install psutil. And windows throws an error about no valid dlls but I there are no dlls in the psutils download or in my windows python install.

I tested removing the *.so files from my runtime location and it breaks the import so its a problem with missing *.so files and had nothing to do with the python path (Sorry Monster)

So if anyone knows why I get the above error on windows or which dll files are missing for psutil, their help would be most appreciated.

So the psutils comes with binary files? (… I see it does)

This means it is not a Python but a OS dependent library with Python binding. Please be aware that only the Python binding is platform independent. You need the platform specific version of this library.

I guess there are several options to deal with that:
A) Tell the user your game requires this library to run, and he needs to install it.
You can indeed tell the user how to install it, or you deliver the installation files too. I guess it would force the user to install Python as well. I do not know if you can bundle that into a separate installer as some other applications do.

B) You deliver a platform dependent version of your game with already installed library. (as you describe in post #11)

C) a variant of B) you deliver all platform versions, and let the system choose what to use.

Problem solved. Turns out the 64-bit psutil module and 64-bit blender don’t work together for some reason. Change to 32-bit versions and now it loads and works perfectly, I would have much rather used the 64-bit versions but at least it runs. I would have never figured it out if it wasn’t for a Linux version of blender which at least in my case worked pretty much out of the box.

Things I learned for anyone who may have the same problem.

1.) Copy your external module from your /python3.3/Lib/site-packages folder to the same folder that blender resides in. If you put the module in your scripts/modules or any other folder listed by sys.path will only work in the viewport and not the standalone player.

2.) Blender or rather the export addon will NOT copy your added modules for you it only copies the python folder and if you put your module in the scripts folder Blender will be able to use it internally but the standalone player and your exported runtimes will NOT work. Simply copying your scripts folder will not work. It seems to me that even though sys.path show your scripts folders listed in the blenders python console your exported runtime will NOT look in the same places. The runtime only looks in the root path of your exe for modules by default. So the best method I found it to just put you module in the same location as blender.exe and your module will work for both the viewport AND the standalone player, then when your ready to export copy your module from the blender.exe folder to the same folder as your runtime, this way the embedded player, the standalone player, and your exe will all work correctly. If your still having problems keep reading.

3.)If your having problems with your module loading in blender but it works in your local python3.3 environment. Try another architecture of blender and the module. In my case 64-bit blender and 64-bit psutil for windows did not work together, but the 32-bit versions did. Linux 64-bit combo did work though.