Maya Environment

Categories: Maya, PyMEL, Python
Tags: ,
Comments: 13 Comments
Published on: March 30, 2011

One of the first things to check when a (python) script is not loading or importing properly is your *pythonpath* environment variable. This is because Python modules are VERY dependent on path location. So if you ever get an error that says something like this:

import myModule
# Error: No module named myModule
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# ImportError: No module named myModule #

or

myModule()
# Error: name 'myModule' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name 'myModule' is not defined # 

then you better check your pythonpaths by using simple code like this:

import sys
for pythonPath in sys.path:
    print pythonPath

You’ll need to do this so much, especially in your Maya session, that you may well want to make a little method and attach a hotkey to it and or put it in a tools menu. While you’re at it, go ahead and list the rest of the environment variables you’ll need to diagnose scripting issues with some code like this:

# replace the ";" with ":" for OSX
# or better yet determine your system and
# automatically do it. I'll leave that up to you.
# (hint: try os.name)

import sys
from pymel.all import *

def getEnvironment():
    scriptPaths = mel.getenv("MAYA_SCRIPT_PATH")
    plugInPaths = mel.getenv("MAYA_PLUG_IN_PATH")
    pythonPaths = mel.getenv("PYTHONPATH")
    iconPaths = mel.getenv("XBMLANGPATH")
    pathPaths = mel.getenv("PATH")
    sysPaths = sys.path
    
    allScriptPaths = scriptPaths.split(";")
    print "\nMAYA_SCRIPT_PATHs are:"
    for scriptPath in allScriptPaths:
        print scriptPath

    allPlugInPaths = plugInPaths.split(";")
    print "\nMAYA_PLUG_IN_PATHs are:"
    for plugInPath in allPlugInPaths:
        print plugInPath
        
    allPythonPaths = pythonPaths.split(";")
    print "\nPYTHONPATHs are:"
    for pythonPath in allPythonPaths:
        print pythonPath
    
    allIconPaths = iconPaths.split(";")
    print "\nXBMLANGPATHs are:"
    for iconPath in allIconPaths:
        print iconPath
        
    allPathPaths = pathPaths.split(";")
    print "\nPATHs are:"
    for pathPath in allPathPaths:
        print pathPath
        
    print "\nsys.paths are:"
    for sysPath in sysPaths:
        print sysPath

The PyMEL import is to take advantage of the handy ‘mel.’ command which lets you call the mel-only “getenv” command as a python method.

It’s worth the effort to put this together as you’ll soon find you’ve run it for the 20th time in the last week, and you’ll be on one of your artists’ machines trying to figure our why the script won’t run for her. The first thing you should do is confirm that her environment is what you think it is by calling this simple method.

Hope it makes your debugging life just slightly less painful. 🙂

Post to Twitter

13 Comments
  1. Rob Gordon says:

    Instead of
    ‘for pythonPath in sys.paths:’

    shouldn’t it be…

    ‘for pythonPath in sys.path:’

    Note the lack of ‘s’ in sys.path.

  2. Hey Jason,

    Thanks for posting this up! Very helpful!

    I have a couple of questions. Obviously, being very new to the whole scripting world, the environment variables like PythonPath have been, confusing to say the least. What should my pythonpath be? Is there a specific folder?

    Whenever I add an system environment variable for windows. Maya runs into issues, telling me it can’t find the site modules, and my older installation of Maya won’t load (not entirely sure why) all these problems go away as soon as I delete my system “pythonpath” variable. So there’s obviously something I’m doing wrong. :/

    Thank you again. I’ve been going through your tutorials and blog posts as I learn more about Python. 😀

    • jason says:

      Raveen,

      pythonPath variable IS your entire python environment. Trying, learning, banging away, and understanding this concept is probably the most important thing about getting python installed and scaling well on your system and your teammates. It is very difficult to debug or help somebody out with this topic remotely, because it is so darn dependent on everybody’s work environment. All I can say, is start with this script I posted above. Run it all the time and start to really understand the pythonpaths and how to add to them when necessary. This issue will not go away, in fact, it will only become more complicated as you progress in your tool making and art pipeline authoring.

      You’re correct in veering away from the windows system environment setting. That is machine specific and you really want your paths to be working for your entire team. I would add to sys.path via scripts.

      Keep hammering away and it will start to become clear how python’s environment references itself over time. Good luck.

      • Jason,

        Thank you so much! I’ve been spending more and more time scripting in Python and I’m loving it so far. You’re right, about understanding Python’s environment the more you use it, it’s making a bit more sense to me now, still trying to wrap my head around it all (as well as trying to understand OOP). Though all of this has been a bit daunting (I’m not very technical by nature. :/). I’m going to stick to it and keep trying! 🙂

        Thank you again!

        p.s. Sorry about the delayed reply!

  3. Ryan says:

    Hi Jason

    Thanks for sharing this I have been scaling the internet looking for a way to fix my UvPlant Script! Thanks for your hard work 🙂

  4. Malcom Armstrong says:

    I am new to python after spending time over the years with MEL, but I am getting lots of issues.
    I use maya 2011 32 bit and have the following installed:

    Python 2.7.4 32 bit
    Qt version 4.8.4
    SIP version 4.14.4
    PyQt version 4.10

    I store my scripts in:
    C:\Documents and Settings\Malcom\My Documents\maya\2011\scripts folder

    I checked the windows environment variable and there was only one location:

    C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages

    However, when I used in maya:
    import sys

    for pythonPath in sys.path:
    print pythonPath

    I got a list that included the path:

    C:\Documents and Settings\Malcom\My Documents\maya\2011\scripts folder

    I have a testWindow.py script in the above folder that has sip reference in it, and I referenced it in the script editor with:

    import os
    import sys
    import testWindow
    reload(testWindow)
    testWindow.open()

    but I keep getting errors that say that there is no module called sip, no module called PyQt4.

    I have also made a UI in QT designer which I can bring in and dock in maya, just cannot get the python commands to link to the ui controls.

    I am sure I may be doing something wrong, but this is quite different to MEL in some respects. I am enjoying the learning, but am having some issues with this call location and variables. If anyone knows what I am doing thats not right, I would appreciate the assistance.
    Thanks

    • jason says:

      Malcom,

      You are butting up against one of the hardest things to configure for Maya. You have to get the exact write builds of sip and PyQt and get them in one of your pythonpath locations.

      I’d recommend working your way up one step at a time:

      First get Nathan’s examples working and get PyQT importing and displaying correctly. Review ALL of his blog posts on the subject, starting here: http://nathanhorne.com/?p=183

      I also use .ui. The first half of this post gives some details on how to load the .ui file: http://www.jason-parks.com/artoftech/?p=439

      As far as connecting the UI buttons to code, there are several different way to do this “signal” connections. I prefer the decorator trick. If your button is called ‘buildButton’, you make a function in your tool’s class like this:

      @PyQt.QtCore.pyQtSlot()
      def on_buildbutton_trigger(self):
      # do something here

      or this variation:

      @PyQt.QtCore.pyQtSlot(“on_buildButton_triggered()”)
      def build_button( self ):
      # do something here

  5. Malcom Armstrong says:

    Thank you for the reply, the info is much appreciated and I will be implementing your examples shortly. I have been using the great tuts on Nathan`s site and have been using the CMIVFX website for assistance too. Just done some checks and discovered that I had not copied the PyQT folder and the sip.pyd files over to the Maya location. Must have been when I originally installed python 3 and then uninstalled and loaded python 2.7.

    So now when I try to load Pyqt4 in maya 2011, its ok, but when I import sip in the script editor, it says:
    Error: ImportError: file: Module use of python27.dll conflicts with this version of python.

    Now, I can see what the issue is, but do I have to uninstall python 2.7 and all the other components, such as pyqt, designer etc and install python 2.6.4? which is what I have found out Maya 2011 uses. I noticed in the sys.path that there is only a reference to a python26.zip, which I am assuming is the version maya is accessing.

    Or is there a way to have multiple installs of python or sip? If so, pointers to how to get it done will be appreciated, though I seem to remember that I was told as a beginner, not to do that. Being that I have python 2.6 as part of the maya install and python 2.7.4 as an installed version, I must have a multiple install, so can I make Maya use 2.7.4?

    Most of the other stuff is ok, its just versions and locations of modules and python is a little confusing. Probably making it harder for myself than it is.

    I have managed to get a .ui file in and working with simple embedded mel as dynamic properties, it is just the versions which are holding me up a bit. Installing Python 2.7.4 was what I was told by a friend who uses it all the time in his vfx work, as I was still discovering stuff, I went with the advice. Its coming along well, just have to get these issues sorted so I can create the plug ins that I need. As some of the syntax on Nathans page uses sip, I cannot get that working.

    Thanks very much for any help.

  6. Malcom Armstrong says:

    Just seen this
    http://stackoverflow.com/questions/14656593/how-do-i-change-the-python-version-in-maya-2013?rq=1

    Is this relevent to my maya 2011 32 bit install. Seems like it should work, but I have been wrong before, but I got a good feeling about this. lol

  7. lala says:

    thanks, very useful information..
    i m curious to know about how can i append some paths to maya_scipt_path via python. from quite some time i wanted to solve this via editing my userSetup.py file but unable to do.
    thanks in advance for help.

    • jason says:

      from pymel.util import putEnv, getEnv
      putEnv(“MAYA_SCRIPT_PATH”, [getEnv(‘MAYA_SCRIPT_PATH’), ‘c:/myNewPath’])

  8. lala says:

    thanks jason, extremely useful..regards,lala

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Welcome , today is Wednesday, December 12, 2018