user2670917
user2670917

Reputation: 1

Python relative import fails in Maya

I am defining a new Python pipeline for Maya 2012 and I am having difficulty setting up package properly.

I have:

Prod
    __init__.py
        PackA
            __init__.py
            PAMod1.py

        PackB
            __init__.py
            PBMod2.py

PAMod1
    def a():pass

PAMod2
    def b():pass

How do I call PAMod1.b in PAMod2.b?

How do I call module with the whole name Prod.PackA.PAMod1.a() inside PAMod2.b?

Upvotes: 0

Views: 328

Answers (1)

theodox
theodox

Reputation: 12218

as long as Prod is on the python path, you can do this - the safest way, using absolute paths

# in PAMod2.py

import Prod.PackA.PAMod1

Prod.PackA.PAMod1.Function()

or (still safe, easier to type):

# in PAMod2.py

import Prod.PackA.PAMod1 as Mod1 

Mod1.Function()

You can reference a sibling package as well. This only works inside a module - you can'd do it interactively ( ie, from the maya script editor) . It's also a going to break if eather PackA or PackB moves

# in PAMod2.py

from .. import PAMod1

PAMod1.Function()

The python docs are here and this is a good SO question on the same (common) topic

Upvotes: 2

Related Questions