Mambo4
July 27, 2018, 3:44pm
1
I can build a test package
/testPackage
__init__.py (blank)
a.py (contains SomeClass)
b.py (imports from a)
and within b I can write:
import a
or
from a import SomeClass
and this works outside of Maya, in pycharm, using mayapy.exe
but it fails in the maya script editor.
Why?
Is there a fix?
Mambo4
July 27, 2018, 4:05pm
2
As soon as I ask the question…
The fix appears to be to import with the full module path to a.py
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 b.py seems to verify this
import sys
sys.path.append("C:/mystudio/python/techart/testPackage")
from a import SomeClass
does that sound correct?
bob.w
July 27, 2018, 4:16pm
3
in b.py
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.
Mambo4
July 27, 2018, 4:56pm
4
when I try from .a import SomeClass
I get
ValueError: Attempted relative import in non-package
in Maya or Pycharm
bob.w
July 27, 2018, 6:12pm
5
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.
Mambo4
July 27, 2018, 6:33pm
6
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
bob.w
July 27, 2018, 8:14pm
7
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.