Search code examples
pythonscriptingzipmaya

Maya Python Create and Use Zipped Package?


Can anyone describe exactly how someone can create and execute a python zip package in Maya? A lot of tutorials and questions regarding this jump into the middle and assume certain knowledge. I need a simple example as a starting point.

Folder/
    scriptA.py
    scriptB.py
    scriptC.py

ScriptA:
    Import ScriptB
    Import ScriptC

Create zip of Folder

In Maya
    Code to run Folder as if not zipped
    ScriptA.foo()

In folder we have three scripts. ScriptA references the other two. I zip the folder with a program like winrar. In maya, I want to load from this zip as if the files inside were any other module sitting in my script folder (without unzipping preferably). How do I do this?


Solution

  • Any zip on your python path is treated like a folder, so:

    import sys
    sys.path.append('path/to/archive.zip')
    
    import thingInZip
    thingInZip.do_something()
    

    The only issue is depth: the zipImporter does not expect nested directory structures. So this is OK:

     ziparchive.zip
       +--- module1.py
       +----module2.py
       +----package_folder
         |
         +-- __init.__py
         +-- submodule1.py
         +-- submodule2.py
         +--- subpackage
           |
           +- __init__.py
    

    But this is not:

    ziparchive.zip
     + --- folder
        +- just_a_python_file_not_part_of_a_package.py
    

    also the site module can't add paths inside a zip. There's a workaround here. You will also probably need to be careful about the order of your sys.path: you want to make sure you know if you are working from the zipped one or from loose files on your disk.

    You can save space by zipping only the .pyc files instead of the whole thing, btw.

    PS beware of using left slashes in sys.path.append : they have to be escaped \\ -- right slashes work on both windows and *ix