Contents:
What import does
Temporarily add a new directory to import from
Permanently add a new directory to import from
Make import work like MEL source
Since importing modules is important to understand and anyone first learning how to use Python will need to do this. I figured I would go over how to properly import python modules in Maya. I will go through examples of how to temporarily add a path to your sys.paths so that Python can find it and I will show how to add the path permanently. Last I give an example of a function I made so you can just pass it a path and it returns the module that was imported for you to work with.
First thing you learn with Python scripts is you can not use the menu item file/source script in the Maya script editor. I dont know why Maya shows you both .mel files and .py files since sourcing a .py file will only give you errors. Basically the point here is you dont source Python files. It wont work.
The way you import a python script is admittedly a bit more cumbersome than sourcing a MEL file. Python does not allow you to simply source a given path name to the file. This is not a Maya specific issue, but how python is designed.
Also importing a python script or module is not the same as sourcing in other ways. When you use import, Python checks to see if the module is loaded and if it is not it will read the module and create the data in a namespace. Python also compiles a .pyc file. So thats why you see extra files in your folder after importing a module. If the module has been changed after it was imported and you want to “source” that version then the correct code to use is:
reload( module )
When you import a python script you are to simply do this:
# import the filename into its own namespace with the same name import module # import the filename into its own namespace with a specified name import module as newname # Import all the definitions and variables of the module into the global namespace # This defeats the purpose of Python making your code less modular so it should rarely be used from module import * # Reference specific variables and definitions into the global namespace from the module from module import varA, varB # Allows a variable to be passed to import and returns the module to a variable you specify # The way this works is a bit odd since even if foo is imported you cant call foo.defname() # you must in this case use the variable mod to call mod.defname() mod = __import__( module )
It is not ok to type a file extension or a path name. You can not even use a variable with the correctly named string. You can however use __import__() if you want to give it a variable. I show an example of this later. When you import, Python will look for the script in a set of system paths that Python finds. Python looks in its standard module library paths and also includes the PYTHONPATH environment variable paths.
Maya also automatically adds these paths by default:
C:/Documents and Settings/Ryan Trowbridge/My Documents/maya/2008/prefs/scripts
C:/Documents and Settings/Ryan Trowbridge/My Documents/maya/2008/scripts/
C:/Documents and Settings/Ryan Trowbridge/My Documents/maya/scripts
It is important to note that Maya will not find any scripts in your /My Documents/maya/2008/scripts/ folder. Note that the path has an extra forward slash therefore Python finds nothing. I think the Maya programmers are to blame for that one. This might be fixed in Maya 2009. If you place your script in the /My Documents/maya/scripts path Python files will import fine. From reading on the boards some people seem to be confused about this and I think rightfully so. Thankfully you can fix this with the environment variable as I show later.
First lets look at what paths Python is looking for using the script editor. Open up your Maya script editor and in a python tab execute this code:
import sys # sys.path is simply an array of system paths that gets created when you first start up maya syspaths = sys.path for path in syspaths: print path
Recent Comments