Importing a module in the same package fails in the Maya script editor


I can build a test package

/testPackage (blank) (contains SomeClass) (imports from a)

and within b I can write:

import a
from a import SomeClass

and this works outside of Maya, in pycharm, using mayapy.exe

but it fails in the maya script editor.
Is there a fix?


As soon as I ask the question…
The fix appears to be to import with the full module path to

import mystudio.python.techart.testPackage.a
from mystudio.python.techart.testPackage.a import SomeClass

must be because it’s executing “in the directory” in the case of pycharm testing
but when run in maya it needs to be relative to something in sys.path

testing by adding to sys.path in seems to verify this

import sys

from a import SomeClass

does that sound correct?


in you should be able to do a from .a import SomeClass without having to place testPackage directly into sys.path

Though really, from a import SomeClass should also work, unless a is a really generic package name and it exists somewhere else on sys.path as that kind of thing can conflict.


when I try from .a import SomeClass
I get
ValueError: Attempted relative import in non-package in Maya or Pycharm


Yeah, you’ll get that error if you’re attempting to run the file directly, because at that point you’re not actually in a package.

Its very likely that I am confused as to what exactly you’re attempting.


Ok that makes sense. I’m running the file directly (pycharm send to maya) while iterating on my code.

I tend to test with functions in main() , perhaps a bad habit


Naw, I often do the same thing, though maybe we both have terrible habits?

Which is why I’ve run into this problem myself in the past.