Maya Python创建和使用压缩包? [英] Maya Python Create and Use Zipped Package?

查看:175
本文介绍了Maya Python创建和使用压缩包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能确切描述某人如何在Maya中创建和执行python zip软件包?许多与此有关的教程和问题跳入了中间,并假设了一定的知识.我需要一个简单的例子作为起点.

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()

在文件夹中,我们有三个脚本. ScriptA引用了其他两个.我使用Winrar之类的程序压缩文件夹.在Maya中,我想从此zip加载,就像其中的文件是坐在我的脚本文件夹中的其他模块一样(最好不进行解压缩).我该怎么做呢?

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?

推荐答案

您python路径上的任何zip都被视为一个文件夹,因此:

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()

唯一的问题是深度:zipImporter不希望嵌套目录结构.这样就可以了:

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

但这不是:

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

site模块也无法在 内部添加路径.有一种解决方法,此处.您可能还需要注意sys.path的顺序:您想确保知道是使用压缩的文件还是磁盘上的松散文件来工作.

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.

您可以通过仅压缩.pyc文件而不是整个文件来节省空间,顺便说一句.

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

PS谨防在sys.path.append中使用左斜杠:必须将其转义\\ –右斜杠在Windows和* ix上都可以工作

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

这篇关于Maya Python创建和使用压缩包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆