Maya/Python - how to interact between py files of same level?

Hey,

I have some troubles in how to interact with python files on the same level.

for example I have a root directory with 2 python files

/myTool
- toolA.py
- toolB.py

I would like to be able that : toolA imports toolB
so that it can use its functionality.

what is the best way to handle this?

of course i would like to be able to use it like this : ‘import toolB’
but in the sys.path the full path of myTool is not added. so this can not work.

is there a way to easily add the path of the current dir?
preferable in one line code :smiley: (of course)

Turn it into a package by adding an init.py

though not really recommended content in your module files, before importing B you can do it with:
import sys; sys.path.append(your_additional_python_path)

you’ll need to use advice above to turn your dir into a package beforehand.

…though the best approach would be importing your toolA in first place, so both A and B assume that python path is set up correctly in advance; you may also want to move both into a package of some sort, so you can have a reliable namespace for the future. When it comes to setting the path, you could use the sys.path.append() method from script editor once per maya session (I use that for projects I don’t want permanently in my path), or add path to python path permanently via maya.env.

it is my understanding that if you have two python files in the same folder, they should import fine.

for example:

dir/
-fileA.py
-fileB.py

fileA can have:


import fileB
fileB.class().method()
fileB.func()

Python should just be adding the current directory, but if you manually want to do it


import sys,os
path = os.path.realpath(__file__)  #optionA
path = sys.argv[0].rpartition('/')[0] #optionB
sys.path.append(path)

The other option is adding the init.py file to turn it into a package, but that package still needs to reside within a python path.

You want to be using the standard python mechanisms to make things available on the path. That’s what they are there for. You could use

import site
site.addsitedir(’/mytool’)
import toolA
import toolB

In general you don’t want to organize your code around tools. What you should be thinking of is function specific modules that are imported into task specific tools

/myLib
geometry.py
database.py
textures.py
/myTools
toolA.py
toolB.py

where toolA and toolB import from the files in libs, not from each other. Keep your dependencies simple – you never want to get in the situation where a imports b, b imports c, but c imports a… strange things can happen depending on how the modules are written.

I know masters and geeks have given you the answer but as far my learning experience have taught me to read the docs carefully/ or reread lookem into again if you have forgot or missed, that comes with Python (the docs folder)

also, try dissecting the existing packages that exist to learn.

hmm I guess I was a bit hasty with my reply didn’t read carefully and just assumed it from a first glimps. Anyway I have to vouch for the approach suggested by Theodox for module structure! There is also pth files you can use if you look them up in the documentation for making folders available!