pkgutil
--- パッケージ拡張ユーティリティ¶
ソースコード: Lib/pkgutil.py
このモジュールはインポートシステムの、特にパッケージサポートに関するユーティリティです。
-
class
pkgutil.
ModuleInfo
(module_finder, name, ispkg)¶ モジュールの概要情報を格納する namedtuple
バージョン 3.6 で追加.
-
pkgutil.
extend_path
(path, name)¶ パッケージを構成するモジュールの検索パスを拡張します。パッケージの
__init__.py
で次のように書くことを意図したものです:from pkgutil import extend_path __path__ = extend_path(__path__, __name__)
This will add to the package's
__path__
all subdirectories of directories onsys.path
named after the package. This is useful if one wants to distribute different parts of a single logical package as multiple directories.同時に
*.pkg
の*
の部分が name 引数に指定された文字列に一致するファイルの検索もおこないます。この機能はimport
で始まる特別な行がないことを除き*.pth
ファイルに似ています (site
の項を参照)。*.pkg
は重複のチェックを除き、信頼できるものとして扱われます。*.pkg
ファイルの中に見つかったエントリはファイルシステム上に実在するか否かを問わず、そのまますべてパスに追加されます。(このような仕様です。)入力パスがリストでない場合(フリーズされたパッケージのとき)は何もせずにリターンします。入力パスが変更されていなければ、アイテムを末尾に追加しただけのコピーを返します。
sys.path
はシーケンスであることが前提になっています。sys.path
の要素の内、実在するディレクトリを指す文字列となっていないものは無視されます。ファイル名として使ったときにエラーが発生するsys.path
の Unicode要素がある場合、この関数(os.path.isdir()
を実行している行) で例外が発生する可能性があります。
-
class
pkgutil.
ImpImporter
(dirname=None)¶ Python の「旧式の」インポートアルゴリズムをラップする、 PEP 302 に基づく Finder です。
If dirname is a string, a PEP 302 finder is created that searches that directory. If dirname is
None
, a PEP 302 finder is created that searches the currentsys.path
, plus any modules that are frozen or built-in.ImpImporter
は現在のところsys.meta_path
に配置しての利用をサポートしていないことに注意してください。
-
class
pkgutil.
ImpLoader
(fullname, file, filename, etc)¶ Loader that wraps Python's "classic" import algorithm.
-
pkgutil.
find_loader
(fullname)¶ Retrieve a module loader for the given fullname.
これは後方互換性のために提供している
importlib.util.find_spec()
へのラッパーで、そこでのほどんどの失敗をImportError
に変換し、完全なModuleSpec
を返す代わりにローダのみを返しています。バージョン 3.3 で変更: Updated to be based directly on
importlib
rather than relying on the package internal PEP 302 import emulation.バージョン 3.4 で変更: PEP 451 ベースに更新されました。
-
pkgutil.
get_importer
(path_item)¶ Retrieve a finder for the given path_item.
The returned finder is cached in
sys.path_importer_cache
if it was newly created by a path hook.キャッシュ (やその一部) は、
sys.path_hooks
のリスキャンが必要になった場合は手動でクリアすることができます。
-
pkgutil.
get_loader
(module_or_name)¶ Get a loader object for module_or_name.
module か package が通常の import 機構によってアクセスできる場合、その機構の該当部分に対するラッパーを返します。モジュールが見つからなかったり import できない場合は
None
を返します。その名前のモジュールがまだ import されていない場合、そのモジュールを含むパッケージが(あれば)そのパッケージの__path__
を確立するために import されます。バージョン 3.3 で変更: Updated to be based directly on
importlib
rather than relying on the package internal PEP 302 import emulation.バージョン 3.4 で変更: PEP 451 ベースに更新されました。
-
pkgutil.
iter_importers
(fullname='')¶ Yield finder objects for the given module name.
If fullname contains a
'.'
, the finders will be for the package containing fullname, otherwise they will be all registered top level finders (i.e. those on bothsys.meta_path
andsys.path_hooks
).その名前のついたモジュールがパッケージ内に含まれている場合、この関数を実行した副作用としてそのパッケージが import されます。
If no module name is specified, all top level finders are produced.
-
pkgutil.
iter_modules
(path=None, prefix='')¶ Yields
ModuleInfo
for all submodules on path, or, if path isNone
, all top-level modules onsys.path
.path は
None
か、モジュールを検索する path のリストのどちらかでなければなりません。prefix は出力の全てのモジュール名の頭に出力する文字列です。
注釈
これは
iter_modules()
メソッドを定義している finder に対してのみ動作します。このインターフェイスは非標準なので、モジュールはimportlib.machinery.FileFinder
とzipimport.zipimporter
の実装も提供します。
-
pkgutil.
walk_packages
(path=None, prefix='', onerror=None)¶ Yields
ModuleInfo
for all modules recursively on path, or, if path isNone
, all accessible modules.path は
None
か、モジュールを検索する path のリストのどちらかでなければなりません。prefix は出力の全てのモジュール名の頭に出力する文字列です。
この関数は与えられた path 上の全ての パッケージ (全てのモジュール ではない) を、サブモジュールを検索するのに必要な
__path__
属性にアクセスするために import します。onerror は、パッケージを import しようとしたときに何かの例外が発生した場合に、 1つの引数 (import しようとしていたパッケージの名前) で呼び出される関数です。 onerror 関数が提供されない場合、
ImportError
は補足され無視されます。それ以外の全ての例外は伝播し、検索を停止させます。例:
# list all modules python can access walk_packages() # list all submodules of ctypes walk_packages(ctypes.__path__, ctypes.__name__ + '.')
注釈
これは
iter_modules()
メソッドを定義している finder に対してのみ動作します。このインターフェイスは非標準なので、モジュールはimportlib.machinery.FileFinder
とzipimport.zipimporter
の実装も提供します。
-
pkgutil.
get_data
(package, resource)¶ パッケージからリソースを取得します。
This is a wrapper for the 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/
).この関数が返すのは指定されたリソースの内容であるバイナリ文字列です。
ファイルシステム中に位置するパッケージで既にインポートされているものに対しては、次と大体同じです:
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 loader which does not support
get_data
, thenNone
is returned. In particular, the loader for namespace packages does not supportget_data
.
-
pkgutil.
resolve_name
(name)¶ Resolve a name to an object.
This functionality is used in numerous places in the standard library (see bpo-12915) - and equivalent functionality is also in widely used third-party packages such as setuptools, Django and Pyramid.
It is expected that name will be a string in one of the following formats, where W is shorthand for a valid Python identifier and dot stands for a literal period in these pseudo-regexes:
W(.W)*
W(.W)*:(W(.W)*)?
The first form is intended for backward compatibility only. It assumes that some part of the dotted name is a package, and the rest is an object somewhere within that package, possibly nested inside other objects. Because the place where the package stops and the object hierarchy starts can't be inferred by inspection, repeated attempts to import must be done with this form.
In the second form, the caller makes the division point clear through the provision of a single colon: the dotted name to the left of the colon is a package to be imported, and the dotted name to the right is the object hierarchy within that package. Only one import is needed in this form. If it ends with the colon, then a module object is returned.
The function will return an object (which might be a module), or raise one of the following exceptions:
ValueError
-- if name isn't in a recognised format.ImportError
-- if an import failed when it shouldn't have.AttributeError
-- If a failure occurred when traversing the object hierarchy within the imported package to get to the desired object.バージョン 3.9 で追加.