Import Modules in Motionbuilder give importError

For making one of my tools easier to work with, I’ve divided it into different files for each class. they are all in the same folder

Python documentation states:

When a module named spam is imported,
the interpreter searches for a file named spam.py in the directory containing the input script
and then in the list of directories specified by the environment variable PYTHONPATH.

but when i try to import my different files as modules inside of motionbuilder, it fails:

ImportError: No module named baseClass

trying to do os.getcwd() to make sure i’m in the same folder as the script, it returns the correct folder.

I don’t want to add the folder to my PATH env variable just to import it.

Trying this in IDLE IDE it works…

Any suggestions?

I Ended doing somthing like This :


imp.load_source('classSetup', imppath + "/classSetup.py")

from classSetup import ClassSupport

is this acceptable? :slight_smile:

Might be a bit simpler to just put imppath on your sys.path:

import sys
sys.path.append(imppath)

I also had trouble importing modules from the same folder as the script I was running. The usual methods to find out what the directory of the script you are running is, did not seem to work.

To avoid explicitly defining that path to our python script folders in each and every one of our modules, we just sys.path.append() them to the python path, in a module that’s sits in the python startup folder.

Btw. this is a limitation of the boost wrapper, if memory serves me right

The current working directory is not used at all for imports. Nor is the Windows system or user paths (except when binary modules load DLLs they’re dependent on, but that’s another story).

As the docs you quoted state, it looks in the folder where the script (doing the importing) sits, then searches down the folders in sys.path.

That all said, sounds like Mobo isn’t playing by the usual Python import rules for some reason.

[QUOTE=djTomServo;11250]Might be a bit simpler to just put imppath on your sys.path:

import sys
sys.path.append(imppath)

[/QUOTE]

correct me if i’m wrong but won’t this store that path “forever” in the sys.path?

It will not.

It doesn’t work as expected in MB, is there an API method for MB