Cannot access python module in subdirectory under windows?

Hi,
I have the following folder structure:

/toto/game.blend
/toto/scripts/init.py

And I have referenced in a controller a script module as follow:
scripts.init.main

But I get the following error message:

Python module not found - …
ImportError:: No module named scripts

If I place the python file directly in toto/ directory and reference it as init.main in the controller, everything is fine.

So is there some limitations under Windows to access modules in subdirectory?

For python to understand that a directory is a module. You need to place a file called init.py in that directory. You use that file to perform any initialization commands for that module, such as, for example, importing it’s submodules.

So in your case, simply add a init.py file to the scripts folder and on this file you add this line:
import init

This will import init.py to the scripts module when init.py is run. Which would make the function you’re trying to access available to the BGE.

You can read more about how python modules work in the official tutorial:
http://docs.python.org/tutorial/modules.html

Thanks a lot for this quick reply.
My (stupid) problem is solved!

Don’t worry, probably everyone who have tried using modules on GE got stuck on that for some time. This isn’t the first and won’t be the last thread with the same question :slight_smile:

If you wanted scripts to be a sub folder, not a module that you can that directory the list that python checks when importing modules. If you do that than run this as the very very very very first thing you run.

import site
site.addsitedir(GameLogic.expandPath("\scripts"))

use

import site
site.addsitedir(GameLogic.expandPath("//scripts"))

// is a special string blender replaces with the blender base path. \scripts isnt so portable, (dont think it works on unix)

Some of the recent game competition games didnt work on my linux system because // wasnt used.

That would of been one of mine then because I copied that code straight from my entry :stuck_out_tongue:
Thanks for the tip.