Search code examples
pythonimportmodulemaya

Problems importing a python module in Maya and it being recognized


Still learning certain things about Python... I am having issues recognizing my Python script in my scripts dir. First, I checked to see that my path is set correctly:

import sys
for pythonPath in sys.path:
    print pythonPath

And C:/Users/..../Documents/maya/2014-x64/scripts is listed, which is where I am placing swap.py

In Maya's script editor I am typing the following:

import swap
reload(swap)
swap.printSomething()

I get:

Error: AttributeError: file line 3: 'module' object has no attribute 'printSomething' #

If I take the same code and throw it into a package...

C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py

And then call this, it works...

import swapPackage.swap as swap
reload(swap)
swap.printSomething()

Why? I am totally confused. Mel scripts even run fine from this location as well. I just can't get a simple python script to import and run.

Also something I noticed. Even though I can get this script to run in a package, the package name must be totally different than the module name. I can't have a package named this:

C:/Users/..../Documents/maya/2014-x64/scripts/swap/swap.py

but I can have one where the package name is different:

C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py

Solution

  • Ok folks, I was able to solve this by executing a print of my file, only to find out that it was sourcing a totally different version someone copied elsewhere. ARGH. This solves both issues, and makes sense why changing the package name from the module worked.

    import swap
        reload(swap)
        print swap.__file__