30.4. zipimport — Import modules from Zip archives — Python v2.7.2 documentation

30.4.2. Examples¶

Here is an example that imports a module from a ZIP archive - note that the
zipimport module is not explicitly used.

$ unzip -l /tmp/example.zip
Archive:  /tmp/example.zipLength     Date   Time    Name--------    ----   ----    ----8467  11-26-02 22:30   jwzthreading.py--------                   -------8467                   1 file
$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
>>> import sys
>>> sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path
>>> import jwzthreading
>>> jwzthreading.__file__
'/tmp/example.zip/jwzthreading.py'

30.4. zipimport — Import modules from Zip archives — Python v2.7.2 documentation

zipimport — Import modules from Zip archives¶

New in version 2.3.

This module adds the ability to import Python modules (*.py,
*.py[co]) and packages from ZIP-format archives. It is usually not
needed to use the zipimport module explicitly; it is automatically used
by the built-in import mechanism for sys.path items that are paths
to ZIP archives.

Typically, sys.path is a list of directory names as strings. This module
also allows an item of sys.path to be a string naming a ZIP file archive.
The ZIP archive can contain a subdirectory structure to support package imports,
and a path within the archive can be specified to only import from a
subdirectory. For example, the path /tmp/example.zip/lib/ would only
import from the lib/ subdirectory within the archive.

Any files may be present in the ZIP archive, but only files .py and
.py[co] are available for import. ZIP import of dynamic modules
(.pyd, .so) is disallowed. Note that if an archive only contains
.py files, Python will not attempt to modify the archive by adding the
corresponding .pyc or .pyo file, meaning that if a ZIP archive
doesn’t contain .pyc files, importing may be rather slow.

Using the built-in reload() function will fail if called on a module
loaded from a ZIP archive; it is unlikely that reload() would be needed,
since this would imply that the ZIP has been altered during runtime.

ZIP archives with an archive comment are currently not supported.

28.3. pkgutil — Package extension utility — Jython v2.5.2 documentation

28.3. pkgutil — Package extension utility¶

New in version 2.3.

This module provides functions to manipulate packages:

pkgutil.extend_path(path, name)

Extend the search path for the modules which comprise a package.
Intended use is to place the following code in a package’s
__init__.py:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

This will add to the package’s __path__ all subdirectories of
directories on sys.path named after the package. This is
useful if one wants to distribute different parts of a single
logical package as multiple directories.

It also looks for *.pkg files beginning where * matches the
name argument. This feature is similar to *.pth files (see
the site module for more information), except that it doesn’t
special-case lines starting with import. A *.pkg file is
trusted at face value: apart from checking for duplicates, all
entries found in a *.pkg file are added to the path, regardless
of whether they exist on the filesystem. (This is a feature.)

If the input path is not a list (as is the case for frozen
packages) it is returned unchanged. The input path is not
modified; an extended copy is returned. Items are only appended to
the copy at the end.

It is assumed that sys.path is a sequence. Items of
sys.path that are not (Unicode or 8-bit) strings referring to
existing directories are ignored. Unicode items on sys.path
that cause errors when used as filenames may cause this function to
raise an exception (in line with os.path.isdir() behavior).

pkgutil.get_data(package, resource)

Get a resource from a package.

This is a wrapper for the PEP 302 loader get_data() API. The
package argument should be the name of a package, in standard
module format (foo.bar). The resource argument should be in the
form of a relative filename, using / as the path separator. The
parent directory name .. is not allowed, and nor is a rooted
name (starting with a /).

The function returns a binary string that is the contents of the
specified resource.

For packages located in the filesystem, which have already been
imported, this is the rough equivalent of:

d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), ‘rb’).read()

If the package cannot be located or loaded, or it uses a PEP 302
loader which does not support get_data(), then None is
returned.

28.3. pkgutil — Package extension utility — Jython v2.5.2 documentation

28.3. pkgutil — Package extension utility¶

New in version 2.3.

This module provides functions to manipulate packages:

pkgutil.extend_path(path, name)

Extend the search path for the modules which comprise a package.
Intended use is to place the following code in a package’s
__init__.py:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

This will add to the package’s __path__ all subdirectories of
directories on sys.path named after the package. This is
useful if one wants to distribute different parts of a single
logical package as multiple directories.

It also looks for *.pkg files beginning where * matches the
name argument. This feature is similar to *.pth files (see
the site module for more information), except that it doesn’t
special-case lines starting with import. A *.pkg file is
trusted at face value: apart from checking for duplicates, all
entries found in a *.pkg file are added to the path, regardless
of whether they exist on the filesystem. (This is a feature.)

If the input path is not a list (as is the case for frozen
packages) it is returned unchanged. The input path is not
modified; an extended copy is returned. Items are only appended to
the copy at the end.

It is assumed that sys.path is a sequence. Items of
sys.path that are not (Unicode or 8-bit) strings referring to
existing directories are ignored. Unicode items on sys.path
that cause errors when used as filenames may cause this function to
raise an exception (in line with os.path.isdir() behavior).

pkgutil.get_data(package, resource)

Get a resource from a package.

This is a wrapper for the PEP 302 loader get_data() API. The
package argument should be the name of a package, in standard
module format (foo.bar). The resource argument should be in the
form of a relative filename, using / as the path separator. The
parent directory name .. is not allowed, and nor is a rooted
name (starting with a /).

The function returns a binary string that is the contents of the
specified resource.

For packages located in the filesystem, which have already been
imported, this is the rough equivalent of:

d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), ‘rb’).read()

If the package cannot be located or loaded, or it uses a PEP 302
loader which does not support get_data(), then None is
returned.

zipimport — Import modules from Zip archives¶相关推荐

  1. R语言使用unzip函数解压压缩文件(Extract or List Zip Archives)

    R语言使用unzip函数解压压缩文件(Extract or List Zip Archives) 目录 R语言使用unzip函数解压压缩文件(Extract or List Zip Archives) ...

  2. Python著名的lib和开发框架(均为转载)

    第一,https://github.com/vinta/awesome-python Awesome Python A curated list of awesome Python framework ...

  3. 导出PDF和Zip文件的工具类

    一.导出pdf文件,使用itext框架: 1.引入pom文件: <dependency><groupId>com.itextpdf</groupId><art ...

  4. Python打包文件夹(zip/tar/tar.gz)

    Code tells all: 一.zip import os, zipfile#打包目录为zip文件(未压缩) def make_zip(source_dir, output_filename):z ...

  5. AS3的一些压缩解压缩类库(AS3 ZIP、AS3 GZIP等等)

    原文链接:AS3的一些压缩解压缩类库(AS3 ZIP.AS3 GZIP等等) 在现在开发的游戏中,由于战斗数据比较大,所以尝试对战斗数据进行压缩,然后输出到客户端flash端再解压. Google到一 ...

  6. Python zipfile – Python ZIP

    Python zipfile module helps us in working with zip files. Today we will learn how to read zip archiv ...

  7. HDFS文件系统内的文件格式转换(zip格式转化成gzip格式)

    这篇主要介绍利用hdfs接口,使用java编程向hdfs写入数据. 一.模仿hadoop fs -put 和 -copyFromLoca命令,实现本地复制文件到hdfs: View Code 二.写一 ...

  8. Java解压zip文件(文本)压缩包

    2019独角兽企业重金招聘Python工程师标准>>> 说明:由于我们的日志收集到指定服务器上,会按天压缩成一个zip格式的压缩包,但是有时候需要对这些日志进行处理,人工解压在处理, ...

  9. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

最新文章

  1. 用Pytorch给你的母校做一个样式迁移吧!
  2. 功率谱密度相关方法MATLAB实现
  3. 程序猿段子_【新年新气象】今年,能为程序员男友做点什么?
  4. Go 网络 TCP Client (一)
  5. 电脑任务栏跑到右边去了_手把手教你用U盘安装Win10系统?学会了再也不用去跑电脑店了...
  6. ffdshow神奇的功能:视频播放时显示运动矢量和QP
  7. 经验 | 我的研究生这三年
  8. select2 ajax 搜索框,修改Select2搜索框(Modify Select2 search box)
  9. 用C语言编写贪吃蛇小游戏
  10. 【转】Latex入门教程
  11. 电子制作:红外遥控器检测仪的制作
  12. 喜马拉雅xm格式转化mp3_MTS视频格式转化
  13. 直播回顾 |「星轨巡讲」技术分享会亮点速递
  14. 1135: [POI2009]Lyz
  15. 一般人不会告诉你的跨境电商货源渠道大全
  16. 用python定时自动发微博_Python脚本实现自动发带图的微博
  17. SONY Xperia SP M35 解锁后重新上锁
  18. 正则表达式、常用的匹配总结
  19. 基于QT的五子棋人机博弈问题设计与实现 文档+任务书+答辩PPT+演示视频+项目源码
  20. Excel 2010/2013/2016在鼠标右键新建xls或xlsx文件后,打开报错“无法打开文件”“文件格式或文件扩展名无效”

热门文章

  1. win7下使用命令行关闭被某一端口占用的进程
  2. Oracle发布Oracle数据库的官方Node.js驱动node-oracledb
  3. 一位网络工程师的终告
  4. IT餐馆—第四回 离职
  5. openmp官方源码_Faiss 源码解析
  6. centos docker 入门笔记(一)
  7. Asp.net五大对象
  8. $_SERVER['REQUEST_URI']和$_SERVER[HTTP_X_REWRITE_URL]的区别
  9. K8S架构设计及工作流程分析
  10. undefined reference to `__isnanf'