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

The answer is namespaces. If you haven’t already, I would read through the python docs, they will give you a strong understanding of the fundamentals.

1 Like

importing a module or package doesn’t automatically make the functions within available to the global namespace, so you have to do either of the following:

import test_lib
test_lib.my_func()

or

from test_lib import my_func
my_func()

As clamdragon already said, it’s important to properly understand namespaces!

1 Like

That was a sharp cut… I am up to speed on pythonic namespaces now, thank you to both of you.