pythonmaya

why is maya failing to import my python libarary and run a function defined within it


my library is saved at C:\Users\user1\Documents\maya\scripts\test_lib.py, and its entire content is:

def my_func():
   print("hello world!")

when I run the following in the script editor, I get an error:

import test_lib
my_func()
# Error: NameError: name 'my_func' is not defined

I did restart Maya to make sure the changes were picked up, yet the error persists. I dont understand what is causing this issue, I have other libraries and scripts in the same script folder and they import just fine, some example files in my script folder:

C:\Users\user1\Documents\maya\scripts\test_lib.py
C:\Users\user1\Documents\maya\scripts\grid_fill.py
C:\Users\user1\Documents\maya\scripts\userSetup.py
C:\Users\user1\Documents\maya\scripts\component.py
C:\Users\user1\Documents\maya\scripts\quickMat.mel
...

Running just the import grid_fill in the script will successfully import grid_fill.py, and running its corresponding functions does not give me a "not defined" error.

What could this be down to? Am on Maya 2026


Solution

  • Importing a module does not automatically make all of its functions visible in the current context.

    Either you need to import that function specifically:

    from test_lib import my_func
    

    Or you need to tell python where the function is located:

    test_lib.my_func()