module mode - How to go "up" in directories?

As the topic says, I know the dot (.) defines a directory.
So from game root directory usually goes something like this:
python.props.main

But how do I navigate from subdirectory to main directory and to the python folder?
Lets say I attach a script to a prop item that has some functionality and it resides in /models/props/ folder.

I must admit I can’t figure it out.

I haven’t tested this out in bge before, but in general each dir has 4 things:

  1. All public files
  2. All private/hidden files (Preceded by underscore)
  3. Self (A link to the directory that you are currently in)
  4. Parent (A link to the directory which contains current dir)

Self is “.” and parent is “…” so to get file “foo.py” assuming it is in main directory and currently you are in subdir would be:
…/foo.py

I tried …/ but it doesn’t work. It just says:
“No module named ‘.’”

Yeah I tried it out and it isn’t working for me either.

Out of all the hacks that I can think of, the best is having a module (in the dir with your props) that has 1 method for each call you want to make, and it calls those methods itself. Essentially it’s an interface to your actual code.

It’s an interesting question, I wish I had a better answer :frowning:

The relationship between modules is not necessarily based on file structure, more so an object-oriented one.

http://docs.python.org/3.3/tutorial/modules.html

For example, given a ‘sound’ module with this structure:


sound/                          Top-level package
      __init__.py               Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

First off, folders containing init.py are considered modules by Python. This allows it to distinguish which folders are modules.

When you do ‘import sound’, this executes init.py in the sound directory. Generally, init.py will import more modules in the current directory or sys.path (see later), and thus execute the init.py in each of the respective directories (formats, effects, filters). These modules are imported into the name space of whomever called it, in this case ‘sound’. They are then accessed with the dot operator. So notice the relationship.

Now init.py could be setup so that after importing sound, you’d access submodules like ‘sound.filters.equalizer.*’.

However, init.py could also could import everything from the filters folder into the namespace of ‘sound’, and you’d access it like ‘sound.equalizer.*’. And of course, this does not correlate with the file structure.

The init.py in filters could import stuff from the effects subfolder, and so on.

About sys.path:
When you import scripts, Python looks for the existence of that module in any of the directories specified in sys.path. By default, the directory of the initial blend file that the BGE is started with is added to sys.path. This directory you can get by using bge.logic.expandPath(’//’).

Relative imports instead of relative file paths in python require you to import relative to the main module. You can use… Instead of…/ to find modules in the above directory. You can also use explicit access it you have included the directory in your python path and it includes an init python file at every level.

Sent from my Nexus 5 using Tapatalk