Python - From parent directory Import module

I thought of posting this in the Python support section, but this is more related to game development rather than Blender Plugins.

I’m working setting up a package of modules for my game, rather than keeping all of the files in Blender’s text editor. I have run into a problem with importing modules from above directories with the relative method:

from . import module

The structure of the package is like this:

Package/
   gamefile.blend
   __init__.py
   game.py
   toolkit.py

   interface/
      __init__.py
      core.py
      elements.py
      functions.py

There’s a bit of code in the functions.py module in the interface folder that needs to import toolkit.py from the above directory. I tried relative importing, but it still isn’t able to import the toolkit module. The init.py files are empty at the moment. Perhaps I’m missing something here.

I get an error message in Blender’s console saying “ImportError: cannot import name toolkit”

Any tips for setting up a package like this?

init.py in Package should initialize toolkit.py, shouldn’t it?

did you try to google your problem?

I have tried googling the problem, and I found that same stackoverflow page (among other results). The bottom response that uses the os module seems like a bit of a stretch to import something. I tried the other methods of relative importing, but they didn’t seem to work and gave the same error as above.

I have only stored my python files in Blender’s text editor up until now, so I’m not too familiar with how packages work. Perhaps I’m just making some stupidly simple mistake.

I would not mix Python files and Blender files at all. Better keep your python files consistent within a subdirectory.

From my experience I can say the best is to add the Python file directory to the Python search path (e.g. with sys) see Prof. Monster’s script folder organizer.

Thanks Monster. I think that fixed the problem.