Search code examples
pythonmaya

How do i append to the Maya PYTHONPATH?


I am working through Practical Maya Programming, and trying to set a 'development root' on my PC, I have followed the instructions (below) exactly but it is not working - At the point where I type 'mayapy.exe' I get the warning "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file."

From the book:

Let's decide where we will do our coding. We'll call this location the development root for the rest of the book. To be concise, I'll choose C:\mayapybook\pylib to house all of our Python code.

Create the development root folder, and inside of it create an empty file named minspect.py.

Now, we need to get C:\mayapybook\pylib onto Python's sys.path so it can be imported. The easiest way to do this is to use the PYTHONPATH environment variable. From a Windows command line you can run the following to add the path, and ensure it worked:

> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
> mayapy.exe
>>> import sys
>>> 'C:\\mayapybook\\pylib' in sys.path
True
>>> import minspect
>>> minspect
<module 'minspect' from '...\minspect.py'>

EDIT

This is how it is working for me at the moment:

PS C:\Users\Me> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
C:\mayapybook\pylib : The term 'C:\mayapybook\pylib' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:29
+ set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
+                             ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\mayapybook\pylib:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

So the code from the book is not working but the code from the post by DrHaze seems to:

PS C:\Users\Me> setx PATH "%PATH%C:\mayapybook\pylib\"

SUCCESS: Specified value was saved.

But when I run the Maya Python Interpreter and check if C:\mayapybook\pylib\ is in sys path it returns false:

>>> 'C:\\mayapybook\\pylib' in sys.path
False

Solution

  • If you are using Powershell, there are different commands and strategies for managing environment variables.

    1. You can set a variable permanently with SetEnvironmentVariable
    2. You can set for the current shell session with: $env:VARNAME = VARVALUE
    3. You can put the commands to set variables in a powershell profile file.

    I would go with the third option. All three are detailed below:

    Option 1. To append the directory "C:\mayapybook\pylib\" to the existing PYTHONPATH permanently for your account:

    [Environment]::SetEnvironmentVariable("PYTHONPATH", $env:PYTHONPATH +";C:\mayapybook\pylib\", "User")
    

    Option 2. To append the Maya bin folder to your PATH for only the current shell session:

    $env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
    

    Option 3. Create a powershell profile and set your env vars there.

    First you'll need to make sure powershell scripts can run locally: Hit the windows button, start typing powershell, right click and open as administrator. Enter: Get-ExecutionPolicy

    If it is says Restricted or AllSigned, set it to RemoteSigned like so:

    Set-ExecutionPolicy RemoteSigned
    

    Close that shell. Now in another powershell (not admin) type:

    cd ~\Documents
    md WindowsPowerShell
    cd WindowsPowerShell
    New-Item -path "profile.ps1" -type file
    notepad.exe profile.ps1
    

    Paste into the file any commands you want to run whenever a new powershell is opened:

    Write-Host "Hello From Your Profile"
    $env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
    $env:PYTHONPATH += ";C:\mayapybook\pylib\"
    

    Now whenever you open a powershell, you'll get a silly message and those paths will be set. You can test by typing:

    Write-Host $env:PATH
    

    or to list all env vars:

    Get-ChildItem Env:
    

    You should now be able to run commands from the maya bin directory. For example, type: maya to start maya.

    Some other useful powershell env var commands here.