Maya Python Create and Use Zipped Package?

一曲冷凌霜 提交于 2019-12-08 15:23:27

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!