5. ã€ã³ããŒãã·ã¹ãã ¶
ãã 1 ã€ã® module ã«ãã Python ã³ãŒãããä»ã®ã¢ãžã¥ãŒã«ã ã€ã³ããŒã ããããšã§ãããã«ããã³ãŒããžã¢ã¯ã»ã¹ã§ããããã«ãªããŸãã import æã¯ã€ã³ããŒãæ©æ§ãåããæãäžè¬çãªæ¹æ³ã§ããããããå¯äžã®æ¹æ³ã§ã¯ãããŸããã importlib.import_module() ãçµã¿èŸŒã¿ã® __import__() ãšãã£ã颿°ã䜿ã£ãŠããã€ã³ããŒãæ©æ§ãåããããšãã§ããŸãã
import æã¯ 2 ã€ã®åŠçãé£ç¶ããŠè¡ã£ãŠããŸã; ããååã®ã¢ãžã¥ãŒã«ãæ¢ãããã®æ€çŽ¢çµæãããŒã«ã«ã¹ã³ãŒãã®ååã«æçžããŸãã import æã®æ€çŽ¢åŠçã¯ãé©åãªåŒæ°ã§ __import__() 颿°ãåŒã³åºãããšãšããŠå®çŸ©ãããŠããŸãã __import__() ã®æ»ãå€ã¯ import æã®ååæçžåŠçã®å®è¡ã§äœ¿ãããŸããååæçžåŠçã®å³å¯ãªè©³çŽ°ã¯ import æãåç
§ããŠãã ããã
__import__() ãçŽæ¥åŒã³åºããšã¢ãžã¥ãŒã«ã®æ€çŽ¢ã®ã¿ãè¡ãããèŠã€ãã£ãå Žåãã¢ãžã¥ãŒã«ã®äœæåŠçãè¡ãããŸãã芪ããã±ãŒãžã®ã€ã³ããŒãã (sys.modules ãå«ã) æ§ã
ãªãã£ãã·ã¥ã®æŽæ°ãªã©ã®å¯äœçšã¯èµ·ãããããããŸãããã import æã®ã¿ãååæçžåŠçãè¡ããŸãã
import æãå®è¡ããããšãã«ã¯ãæšæºã®çµã¿èŸŒã¿é¢æ° __import__() ãåŒã°ããŸããã€ã³ããŒãã·ã¹ãã ãåŒã³åºããã®ä»ã® (importlib.import_module() 颿°ã®ãããª) ã¡ã«ããºã ã¯ã__import__() ã®åŒã³åºãããã€ãã¹ããŠç¬èªã®ã€ã³ããŒãã»ã»ãã³ãã£ã¯ã¹ãå®è£
ããŠããå¯èœæ§ããããŸãã
ã¢ãžã¥ãŒã«ãåããŠã€ã³ããŒãããããšãã Python ã¯ãã®ã¢ãžã¥ãŒã«ãæ€çŽ¢ããèŠä»ãã£ãå Žåãã¢ãžã¥ãŒã«ãªããžã§ã¯ããäœæããåæåããŸã [1] ããã®ååã®ã¢ãžã¥ãŒã«ãèŠä»ãããªãã£ãå Žåã ModuleNotFoundError ãéåºãããŸãã Python ã«ã¯ãã€ã³ããŒãæ©æ§ãå®è¡ããããšãã«ååããã¢ãžã¥ãŒã«ãæ€çŽ¢ããæ§ã
ãªæŠç¥ãå®è£
ãããŠããŸãããããã®æŠç¥ã¯ããã以éã®ç¯ã§è§£èª¬ãããæ§ã
ãªããã¯ã䜿ã£ãŠãä¿®æ£ãããæ¡åŒµãããã§ããŸãã
ããŒãžã§ã³ 3.3 ã§å€æŽ: ã€ã³ããŒãã·ã¹ãã ã PEP 302 ã®ç¬¬ 2 ãã§ãŒãºã®å®å
šãªå®è£
ãžæŽæ°ãããŸããããã¯ãæé»çãªã€ã³ããŒãæ©æ§ã¯ãããŸãã - ã€ã³ããŒãæ©æ§å
šäœã¯ sys.meta_path ãéããŠå
¬éãããŠããŸããå ããŠããã€ãã£ãã®åå空éããã±ãŒãžã®ãµããŒãã¯å®è£
ãããŠããŸã (PEP 420 ãåç
§) ã
5.1. importlib¶
importlib ã¢ãžã¥ãŒã«ã¯ã€ã³ããŒãæ©æ§ãšããåãããããã®äŸ¿å©ãª API ãæäŸããŸããäŸãã° importlib.import_module() ã¯ãã€ã³ããŒãæ©æ§ãå®è¡ããããã®çµã¿èŸŒã¿ã® __import__() ãããã·ã³ãã«ã§æšå¥šããã API ãæäŸããŸãããã詳现ãªããšã¯ importlib ã©ã€ãã©ãªã®ããã¥ã¡ã³ããåç
§ããŠãã ããã
5.2. ããã±ãŒãžÂ¶
Python ã«ã¯ã¢ãžã¥ãŒã«ãªããžã§ã¯ãã®çš®é¡ã¯ 1 çš®é¡ãããªãã Python ã C ããã以å€ã®ãã®ã®ã©ãã§å®è£ ãããŠãããã«é¢ä¿ãªãããã¹ãŠã®ã¢ãžã¥ãŒã«ã¯ãã®çš®é¡ã«ãªããŸããã¢ãžã¥ãŒã«ã®çµç¹åãå©ããååéå±€ãæäŸããããã«ã Python ã«ã¯ ããã±ãŒãž ãšããæŠå¿µããããŸãã
ããã±ãŒãžã¯ãã¡ã€ã«ã·ã¹ãã ã®ãã£ã¬ã¯ããªãã¢ãžã¥ãŒã«ã¯ãã£ã¬ã¯ããªã«ãããã¡ã€ã«ãšèããããšãã§ããŸãããããã±ãŒãžãã¢ãžã¥ãŒã«ã¯ãã¡ã€ã«ã·ã¹ãã ããçãŸããå¿ èŠã¯ãªãã®ã§ããã®æ¯å©ãé¡é¢éãã«åãåã£ãŠã¯ãããŸããããã®ææžã®ç®çã®ããã«ããã£ã¬ã¯ããªãšãã¡ã€ã«ãšãã䟿å©ãªæ¯å©ã䜿ãããšã«ããŸãããã¡ã€ã«ã·ã¹ãã ã®ãã£ã¬ã¯ããªã®ããã«ãããã±ãŒãžã¯éå±€æ§é ãæããéåžžã®ã¢ãžã¥ãŒã«ã ãã§ãªãããµãããã±ãŒãžãå«ãããšããããŸãã
ãã¹ãŠã®ããã±ãŒãžã¯ã¢ãžã¥ãŒã«ã§ããããã¹ãŠã®ã¢ãžã¥ãŒã«ãããã±ãŒãžãšã¯éããªãããšãå¿ã«çããŠããã®ãéèŠã§ãããããã¯ä»ã®èšãæ¹ããããšãããã±ãŒãžã¯åãªãç¹å¥ãªçš®é¡ã®ã¢ãžã¥ãŒã«ã§ãããšèšããŸããç¹ã«ã__path__ 屿§ãæã€ä»»æã®ã¢ãžã¥ãŒã«ã¯ããã±ãŒãžãšèŠãªãããŸãã
ãã¹ãŠã®ã¢ãžã¥ãŒã«ã¯ååãæã¡ãŸããPython ã®å±æ§ã¢ã¯ã»ã¹ã®ææ³ãšåæ§ã«ããµãããã±ãŒãžã®ååã¯èŠªããã±ãŒãžåãšãããèšå·ã§åºåãããŸãããããã£ãŠãemail ãšããååã®ããã±ãŒãžãããããå«ã email.mime ãšããååã®ãµãããã±ãŒãžãããã«ããã«å«ãŸãã email.mime.text ãšèšãååã®ã¢ãžã¥ãŒã«ãèããããšãã§ããŸãã
5.2.1. éåžžã®ããã±ãŒãžÂ¶
Python ã§ã¯ã éåžžã®ããã±ãŒãž ãš åå空éããã±ãŒãž ã® 2 çš®é¡ã®ããã±ãŒãžãå®çŸ©ãããŠããŸããéåžžã®ããã±ãŒãžã¯ Python 3.2 以åããååšããäŒçµ±çãªããã±ãŒãžã§ããå
žåçãªéåžžã®ããã±ãŒãžã¯ __init__.py ãã¡ã€ã«ãå«ããã£ã¬ã¯ããªãšããŠå®è£
ãããŸããéåžžã®ããã±ãŒãžãã€ã³ããŒãããããšãããã® __init__.py ãã¡ã€ã«ãæé»çã«å®è¡ãããããã§å®çŸ©ããŠãããªããžã§ã¯ããããã±ãŒãžåå空éã«ããååã«æçžãããŸãã __init__.py ãã¡ã€ã«ã¯ãä»ã®ã¢ãžã¥ãŒã«ã«æžãã Python ã³ãŒããšåããã®ãå«ãããšãã§ããã¢ãžã¥ãŒã«ãã€ã³ããŒãããããšãã« Python ã¯ã¢ãžã¥ãŒã«ã«å±æ§ã远å ãããããŸãã
äŸãã°ã以äžã®ãããªãã¡ã€ã«ã·ã¹ãã é
眮ã¯ã3 ã€ã®ãµãããã±ãŒãžãæã€æäžäœã® parent ããã±ãŒãžãå®çŸ©ããŸã:
parent/
__init__.py
one/
__init__.py
two/
__init__.py
three/
__init__.py
parent.one ãã€ã³ããŒããããšæé»çã« parent/__init__.py ãš parent/one/__init__.py ãå®è¡ãããŸãããã®åŸã« parent.two ããã㯠parent.three ãã€ã³ããŒããããšããããã parent/two/__init__.py ã parent/three/__init__.py ãå®è¡ãããŸãã
A subdirectory inside a regular package that does not contain an
__init__.py file is treated as an implicit
namespace package (a "namespace
subpackage") rooted in that parent. See PEP 420 for the underlying
specification.
5.2.2. åå空éããã±ãŒãžÂ¶
åå空éããã±ãŒãžã¯æ§ã 㪠ããŒã·ã§ã³ ãå¯ãéãããã®ã§ãããããã®ããŒã·ã§ã³ã¯ãµãããã±ãŒãžã芪ããã±ãŒãžã«æäŸããŸããããŒã·ã§ã³ã¯ãã¡ã€ã«ã·ã¹ãã ã®å¥ã ã®å Žæã«ããããšããããŸããããŒã·ã§ã³ã¯ã zip ãã¡ã€ã«ã®äžããããã¯ãŒã¯äžãããã以å€ã®ã€ã³ããŒãæã« Python ãæ¢ãã©ããã®å Žæã§èŠã€ããããšããããŸããåå空éããã±ãŒãžã¯ãã¡ã€ã«ã·ã¹ãã äžã®ãªããžã§ã¯ãã«å¯Ÿå¿ããããšãããããããã§ãªãããšããããŸã; ãããã¯å®éã®å®äœã®ãªãä»®æ³ã¢ãžã¥ãŒã«ã§ãã
åå空éããã±ãŒãžã¯ã __path__ 屿§ã«æ®éã®ãªã¹ãã¯äœ¿ããŸããããã®ä»£ããã«ç¬èªã® iterable åã䜿ã£ãŠããŠãããŒã·ã§ã³ã®èŠªããã±ãŒãžã®ãã¹ (ãããã¯æäžäœããã±ãŒãžã®ããã® sys.path) ãå€ãã£ãå Žåããã®ããã±ãŒãžã§ã®æ¬¡ã®ã€ã³ããŒãã®éã«ãæ°ãã«èªåã§ããã±ãŒãžããŒã·ã§ã³ãæ€çŽ¢ããŸãã
åå空éããã±ãŒãžã«ã¯ parent/__init__.py ãã¡ã€ã«ã¯ãããŸãããããã©ããããç°ãªãããŒã·ã§ã³ãããããæäŸããè€æ°ã® parent ãã£ã¬ã¯ããªãã€ã³ããŒãæ€çŽ¢ã®éã«èŠã€ããããšããããŸãããããã£ãŠ parent/one ã¯ç©ççã« parent/two ã®é£ãã«ãããšã¯éããŸããããã®å Žåããã®ããã±ãŒãžããµãããã±ãŒãžã®ãã¡ 1 ã€ãã€ã³ããŒãããããšããPython ã¯æäžäœã® parent ããã±ãŒãžã®ããã®åå空éããã±ãŒãžãäœæããŸãã
Namespace packages may also be nested inside a regular package. When the
import system searches a regular package's __path__ and encounters a
subdirectory that does not contain an __init__.py file, that
subdirectory becomes a portion contributing to a namespace
subpackage of the enclosing regular package.
åå空éããã±ãŒãžã®ä»æ§ã«ã€ããŠã¯ PEP 420 ãåç §ããŠãã ããã
5.3. æ€çŽ¢Â¶
æ€çŽ¢ãå§ããããã«ã¯ã Python ã¯ã€ã³ããŒããããã¢ãžã¥ãŒã« (ãããã¯ããã±ãŒãžã§ãããããã§ã®è°è«ã®ç®çã«ãããŠã¯ããããªéãã§ã) ã® å®å
šä¿®é£Ÿ åãå¿
èŠãšããŸãããã®ååã¯ã import æã®æ§ã
ãªåŒæ°ã importlib.import_module() ããã³ __import__() 颿°ã®ãã©ã¡ãŒã¿ããåŸãããŸãã
ãã®ååã¯ã€ã³ããŒãæ€çŽ¢ã®æ§ã
ãªãã§ãŒãºã§äœ¿ãããããã¯äŸãã° foo.bar.baz ã®ãããªãããã§åºåããããµãã¢ãžã¥ãŒã«ãžã®ãã¹ã ã£ããããŸãããã®å Žåã Python ã¯æåã« foo ããæ¬¡ã« foo.bar ããããŠæåŸã« foo.bar.baz ãã€ã³ããŒãããããšããŸããäžéã®ããããã®ã€ã³ããŒãã«å€±æããå Žåã¯ã ModuleNotFoundError ãéåºãããŸãã
5.3.1. ã¢ãžã¥ãŒã«ãã£ãã·ã¥Â¶
ã€ã³ããŒãæ€çŽ¢ã§æåã«èª¿ã¹ãå Žæã¯ sys.modules ã§ãããã®ãããã³ã°ã¯ãäžéã®ãã¹ãå«ãããããŸã§ã«ã€ã³ããŒãããããã¹ãŠã®ã¢ãžã¥ãŒã«ã®ãã£ãã·ã¥ãæäŸããŸãããªã®ã§ foo.bar.baz ãã€ã³ããŒãæžã¿ã®å Žåã sys.modules 㯠foo ã foo.bar ã foo.bar.baz ã®ãšã³ããªãŒãå«ã¿ãŸããããããã®ããŒã¯ãã®å€ãšããŠå¯Ÿå¿ããã¢ãžã¥ãŒã«ãªããžã§ã¯ããæã¡ãŸãã
ã€ã³ããŒãã§ã¯ã¢ãžã¥ãŒã«å㯠sys.modules ããæ¢ãããååšããå Žåã¯ã察å¿ããå€ãã€ã³ããŒããããã¹ãã¢ãžã¥ãŒã«ã§ããããã®åŠçã¯å®äºããŸãããããå€ã None ã ã£ãå Žåã ModuleNotFoundError ãéåºãããŸããã¢ãžã¥ãŒã«åãèŠä»ãããªãã£ãå Žåã¯ã Python ã¯ã¢ãžã¥ãŒã«ã®æ€çŽ¢ãç¶ããŸãã
sys.modules ã¯æžã蟌ã¿å¯èœã§ããããŒã®åé€ã¯å¯Ÿå¿ããã¢ãžã¥ãŒã«ãç Žå£ããªã (ä»ã®ã¢ãžã¥ãŒã«ããã®ã¢ãžã¥ãŒã«ãžã®åç
§ãæã£ãŠãã) ãããããŸããããæå®ãããã¢ãžã¥ãŒã«ã®ãã£ãã·ã¥ããããšã³ããªãŒãç¡å¹ã«ãããããæ¬¡ã«ã€ã³ããŒãããããšã Python ã«ãã®ã¢ãžã¥ãŒã«ãæ¹ããŠæ€çŽ¢ãããããšã«ãªããŸããããŒã None ã«å¯Ÿå¿ä»ããããšãã§ããŸãããæ¬¡ã«ãã®ã¢ãžã¥ãŒã«ãã€ã³ããŒãããããšãã« ModuleNotFoundError ãšãªã£ãŠããŸããŸãã
ããšãã¢ãžã¥ãŒã«ãªããžã§ã¯ããžã®åç
§ãä¿æããŠãããŠã sys.modules ã«ãã£ãã·ã¥ããããšã³ããªãŒãç¡å¹ã«ãããã®æå®ããã¢ãžã¥ãŒã«ãåã€ã³ããŒããããšããŠãã 2 ã€ã®ã¢ãžã¥ãŒã«ãªããžã§ã¯ãã¯åãã§ã¯ ãªã ããšã«æ³šæããŠãã ããããããšã¯å¯Ÿç
§çã«ã importlib.reload() 㯠åã ã¢ãžã¥ãŒã«ãªããžã§ã¯ããåå©çšããã¢ãžã¥ãŒã«ã®ã³ãŒããåå®è¡ããããšã§åã«ã¢ãžã¥ãŒã«ã®å
容ãååæåããã ãã§ãã
5.3.2. ãã¡ã€ã³ããŒãšããŒããŒÂ¶
sys.modules ã«æå®ãããã¢ãžã¥ãŒã«ãèŠã€ãããªãã£ãå Žåã¯ã Python ã®ã€ã³ããŒããããã³ã«ãèµ·åãããã¢ãžã¥ãŒã«ãèŠã€ãããŒãããŸãããã®ãããã³ã«ã¯ 2 ã€ã®æŠå¿µçãªãªããžã§ã¯ãã ãã¡ã€ã³ã㌠㚠ããŒã㌠ããæããŸãããã¡ã€ã³ããŒã®ä»äºã¯ãç¥ã£ãŠããæŠç¥ã䜿ã£ãŠæå®ãããã¢ãžã¥ãŒã«ãèŠã€ãããããã©ãã倿ããããšã§ããäž¡æ¹ã®ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£
ããŠãããªããžã§ã¯ã㯠ã€ã³ããŒã¿ãŒ ãšåŒã°ããŸã - ã€ã³ããŒã¿ãŒã¯èŠæ±ãããã¢ãžã¥ãŒã«ãããŒãã§ãããšåãã£ããšããèªåèªèº«ãè¿ããŸãã
Python ã«ã¯ããã©ã«ãã®ãã¡ã€ã³ããŒãšã€ã³ããŒã¿ãŒãããã€ããããŸãã 1 ã€ç®ã®ãã®ã¯çµã¿èŸŒã¿ã¢ãžã¥ãŒã«ã®èŠã€ãæ¹ãç¥ã£ãŠããŠã 2 ã€ç®ã®ãã®ã¯åçµãããã¢ãžã¥ãŒã« (蚳泚: freeze ããŒã«ã§åŠçãããã¢ãžã¥ãŒã«ã®ããšã ããã°ã©ãã³ã° FAQ ã®ãã©ãããã Python ã¹ã¯ãªããããã¹ã¿ã³ãã¢ãã³ãã€ããªãäœããŸããïŒãã®é ç®ãåç §) ã®èŠã€ãæ¹ãç¥ã£ãŠããŸãã 3 ã€ç®ã®ãã®ã¯ ã€ã³ããŒããã¹ ããã¢ãžã¥ãŒã«ãæ¢ããŸãã ã€ã³ããŒããã¹ ã¯ãã¡ã€ã«ã·ã¹ãã ã®ãã¹ã zip ãã¡ã€ã«ã®äœçœ®ã瀺ããªã¹ãã§ãããã®ãªã¹ãã¯ã URL ã§ç¹å®ã§ãããã®ã®ãããªãäœçœ®ã瀺ãããšã®ã§ããä»»æã®ãªãœãŒã¹ã®æ€çŽ¢ã«ãŸã§æ¡åŒµããããšãã§ããŸãã
ã€ã³ããŒãæ©æ§ã¯æ¡åŒµå¯èœãªã®ã§ãã¢ãžã¥ãŒã«æ€çŽ¢ã®ç¯å²ãšã¹ã³ãŒããæ¡åŒµããããã«æ°ãããã¡ã€ã³ããŒãä»ãå ããããšãã§ããŸãã
ãã¡ã€ã³ããŒã¯å®éã«ã¯ã¢ãžã¥ãŒã«ãããŒãããŸãããæå®ãããã¢ãžã¥ãŒã«ãèŠã€ãã£ãå Žåããã¡ã€ã³ããŒã¯ module spec (ã¢ãžã¥ãŒã«ä»æ§)ãããªãã¡ã¢ãžã¥ãŒã«ã®ã€ã³ããŒãé¢é£ã®æ å ±ãã«ãã»ã«åãããã®ãè¿ããŸããã¢ãžã¥ãŒã«ã®ããŒãæã«ã€ã³ããŒãæ©æ§ã¯ãããå©çšããŸãã
次ã®ç¯ã§ã¯ãã€ã³ããŒãæ©æ§ãæ¡åŒµããããã®æ°ãããã¡ã€ã³ããŒãããŒããŒã®äœæãšç»é²ãå«ãããã¡ã€ã³ããŒãšããŒããŒã®ãããã³ã«ã«ã€ããŠãã詳ãã解説ããŸãã
ããŒãžã§ã³ 3.4 ã§å€æŽ: Python ã®ä»¥åã®ããŒãžã§ã³ã§ã¯ããã¡ã€ã³ããŒã¯çŽæ¥ ããŒã㌠ãè¿ããŠããŸããããçŸåšã¯ããŒããŒã å«ã ã¢ãžã¥ãŒã«ä»æ§ãè¿ããŸããããŒããŒã¯ã€ã³ããŒãäžã¯ãŸã 䜿ãããŠããŸãããè²¬ä»»ã¯æžããŸããã
5.3.3. ã€ã³ããŒãããã¯Â¶
ã€ã³ããŒãæ©æ§ã¯æ¡åŒµå¯èœãªããã«èšèšãããŠããŸã; ãã®äž»ãšãªãä»çµã¿ã¯ ã€ã³ããŒããã㯠ã§ããã€ã³ããŒãããã¯ã«ã¯ 2 çš®é¡ãããŸã: ã¡ã¿ãã㯠㚠ã€ã³ããŒããã¹ãã㯠ã§ãã
ã¡ã¿ããã¯ã¯ã€ã³ããŒãåŠçã®æåã sys.modules ãã£ãã·ã¥ã®æ€çޢ以å€ã®ã€ã³ããŒãåŠçããåã«åŒã³åºãããŸããããã«ããã sys.path ã®åŠçãåçµãããã¢ãžã¥ãŒã«ãçµã¿èŸŒã¿ã®ã¢ãžã¥ãŒã«ã§ããããã¡ã¿ããã¯ã§äžæžãããããšãã§ããŸããã¡ã¿ããã¯ã¯ä»¥äžã§è§£èª¬ããããã«ã sys.meta_path ã«æ°ãããã¡ã€ã³ããŒãªããžã§ã¯ãã远å ããããšã§ç»é²ãããŸãã
ã€ã³ããŒããã¹ããã¯ã¯ã sys.path (ããã㯠package.__path__) ã®åŠçã®äžéšãšããŠã察å¿ãããã¹èŠçŽ ãåãæ±ããšããã§åŒã³åºãããŸããã€ã³ããŒããã¹ããã¯ã¯ä»¥äžã§è§£èª¬ããããã«ãæ°ããåŒã³åºãå¯èœãªããžã§ã¯ãã sys.path_hooks ã«è¿œå ããããšã§ç»é²ãããŸãã
5.3.4. ã¡ã¿ãã¹Â¶
When the named module is not found in sys.modules, Python next
searches sys.meta_path, which contains a list of meta path finder
objects. These finders are queried in order to see if they know how to handle
the named module. Meta path finders must implement a method called
find_spec() which takes three arguments:
a name, an import path, and (optionally) a target module. The meta path
finder can use any strategy it wants to determine whether it can handle
the named module or not.
meta path finder ãæå®ãããã¢ãžã¥ãŒã«ã®æ±ãæ¹ãç¥ã£ãŠããå Žåã¯ããã¡ã€ã³ã㯠spec ãªããžã§ã¯ããè¿ããŸããæå®ãããã¢ãžã¥ãŒã«ãæ±ããªãå Žå㯠None ãè¿ããŸãã sys.meta_path ã«å¯ŸããåŠçã spec ãè¿ããã«ãªã¹ãã®æ«å°Ÿã«å°éããŠããŸã£ãå Žåã¯ã ModuleNotFoundError ãéåºããŸãããã®ä»ã®éåºãããäŸå€ã¯ãã®ãŸãŸåŒã³åºãå
ã«äŒæãããã€ã³ããŒãåŠçãç°åžžçµäºãããŸãã
The find_spec() method of meta path
finders is called with two or three arguments. The first is the fully
qualified name of the module being imported, for example foo.bar.baz.
The second argument is the path entries to use for the module search. For
top-level modules, the second argument is None, but for submodules or
subpackages, the second argument is the value of the parent package's
__path__ attribute. If the appropriate __path__ attribute cannot
be accessed, a ModuleNotFoundError is raised. The third argument
is an existing module object that will be the target of loading later.
The import system passes in a target module only during reload.
ã¡ã¿ãã¹ã¯ã1 åã®ã€ã³ããŒãèŠæ±ã§è€æ°åèµ°æ»ãããå¯èœæ§ããããŸããäŸãã°ãé¢ä¿ããã¢ãžã¥ãŒã«ãã©ãããŸã ãã£ãã·ã¥ãããŠããªããšãããšãã« foo.bar.baz ãã€ã³ããŒããããšãæåã¯åã¡ã¿ãã¹ã»ãã¡ã€ã³ã㌠(mpf) ã«å¯Ÿã㊠mpf.find_spec("foo", None, None) ãåŒã³åºããŠãæäžäœã®ã€ã³ããŒãåŠçãè¡ããŸããfoo ãã€ã³ããŒããããåŸã«ãmpf.find_spec("foo.bar", foo.__path__, None) ãåŒã³åºããŠãã 2 åç®ã®ã¡ã¿ãã¹ã®èµ°æ»ãè¡ãããfoo.bar ãã€ã³ããŒããããŸããfoo.bar ã®ã€ã³ããŒããŸã§è¡ãããããæåŸã®èµ°æ»ã§ mpf.find_spec("foo.bar.baz", foo.bar.__path__, None) ãåŒã³åºããŠãããŸãã
ããã¡ã¿ãã¹ã»ãã¡ã€ã³ããŒã¯æäžäœã®ã€ã³ããŒãã®ã¿ãµããŒãããŠããŸãããããã®ã€ã³ããŒã¿ãŒã¯ã2 ã€ç®ã®åŒæ°ã« None 以å€ã®ãã®ãæž¡ããããšããåžžã« None ãè¿ããŸãã
Python ã®ããã©ã«ãã® sys.meta_path 㯠3 ã€ã®ãã¹ãã¡ã€ã³ããŒãæã£ãŠããŸããçµã¿èŸŒã¿ã¢ãžã¥ãŒã«ã®ã€ã³ããŒãã®æ¹æ³ãç¥ã£ãŠãããã®ãåçµãããã¢ãžã¥ãŒã«ã®ã€ã³ããŒãã®æ¹æ³ãç¥ã£ãŠãããã®ã ã€ã³ããŒããã¹ ããã®ã¢ãžã¥ãŒã«ã®ã€ã³ããŒãã®æ¹æ³ãç¥ã£ãŠãããã® (ã€ãŸã ãã¹ããŒã¹ã»ãã¡ã€ã³ããŒ) ããããŸãã
ããŒãžã§ã³ 3.4 ã§å€æŽ: The find_spec() method of meta path
finders replaced find_module(), which
is now deprecated. While it will continue to work without change, the
import machinery will try it only if the finder does not implement
find_spec().
ããŒãžã§ã³ 3.10 ã§å€æŽ: Use of find_module() by the import system
now raises ImportWarning.
ããŒãžã§ã³ 3.12 ã§å€æŽ: find_module() has been removed.
Use find_spec() instead.
5.4. ããŒã¶
ã¢ãžã¥ãŒã«ä»æ§ãèŠã€ãã£ãå Žåãã€ã³ããŒãæ©æ§ã¯ã¢ãžã¥ãŒã«ãããŒãããæã«ãã (ããã³ããã«å«ãŸããããŒããŒ) ã䜿ããŸããããã¯ãã€ã³ããŒãã®ããŒãéšåã§èµ·ããããšã®è¿äŒŒã§ã:
module = None
if spec.loader is not None and hasattr(spec.loader, 'create_module'):
# It is assumed 'exec_module' will also be defined on the loader.
module = spec.loader.create_module(spec)
if module is None:
module = ModuleType(spec.name)
# The import-related module attributes get set here:
_init_module_attrs(spec, module)
if spec.loader is None:
# unsupported
raise ImportError
if spec.origin is None and spec.submodule_search_locations is not None:
# namespace package
sys.modules[spec.name] = module
elif not hasattr(spec.loader, 'exec_module'):
module = spec.loader.load_module(spec.name)
else:
sys.modules[spec.name] = module
try:
spec.loader.exec_module(module)
except BaseException:
try:
del sys.modules[spec.name]
except KeyError:
pass
raise
return sys.modules[spec.name]
以äžã®è©³çŽ°ã«æ³šæããŠãã ãã:
sys.modulesã®äžã«äžããããååãæã€æ¢åã®ã¢ãžã¥ãŒã«ãªããžã§ã¯ãããããªãã import ã¯æ¢ã«ãããè¿ããŠããã§ããããã¢ãžã¥ãŒã«ã¯ãããŒããŒãã¢ãžã¥ãŒã«ã³ãŒããå®è¡ããåã«
sys.modulesã«ååšããŠããŸãã ã¢ãžã¥ãŒã«ã³ãŒãã (çŽæ¥çãŸãã¯éæ¥çã«) èªåèªèº«ãã€ã³ããŒãããå¯èœæ§ãããã®ã§ãããã¯éèŠã§ã; ã¢ãžã¥ãŒã«ãsys.modulesã«è¿œå ããããšã§ãææªã®ã±ãŒã¹ã§ã¯ç¡éã®ååž°ãããããŠæè¯ã®ã±ãŒã¹ã§ã¯è€æ°åã®ããŒãããåãã£ãŠé²æ¢ãããŸããããŒãåŠçã«å€±æããå Žåããã®å€±æããã¢ãžã¥ãŒã«ã¯ -- ãããŠããã®ã¢ãžã¥ãŒã«ã ãã --
sys.modulesããåãé€ãããŸããsys.modulesãã£ãã·ã¥ã«æ¢ã«å«ãŸããŠãããã¹ãŠã®ã¢ãžã¥ãŒã«ãšãå¯äœçšãšããŠããŒãã«æåãããã¹ãŠã®ã¢ãžã¥ãŒã«ã¯ãåžžã«ãã£ãã·ã¥ã«æ®ãããŸããããã¯ãªããŒããšã¯å¯Ÿç §çã§ããªããŒãã®å Žåã¯å€±æããã¢ãžã¥ãŒã«ãsys.modulesã«æ®ãããŸããåŸã®ã»ã¯ã·ã§ã³ ã§èŠçŽãããããã«ãã¢ãžã¥ãŒã«ãäœãããŠããå®è¡ããããŸã§ã®éã«ã€ã³ããŒãæ©æ§ã¯ã€ã³ããŒãé¢é£ã®ã¢ãžã¥ãŒã«å±æ§ãèšå®ããŸã (äžèšæ¬äŒŒã³ãŒãäŸã® "_init_module_attrs")ã
ã¢ãžã¥ãŒã«å®è¡ã¯ã¢ãžã¥ãŒã«ã®åå空éãæ§ç¯ãããããŒãã®éèŠãªç¬éã§ããå®è¡ã¯ããŒããŒã«å®å šã«å§ä»»ãããããŒããŒã¯äœãã©ã®ããã«æ§ç¯ããããæ±ºå®ããããšã«ãªããŸãã
ããŒãã®éã«äœæãã㊠exec_module() ã«æž¡ãããã¢ãžã¥ãŒã«ã¯ãã€ã³ããŒãã®çµããã«è¿ããããã®ãšã¯ç°ãªããããããŸãã [2]ã
ããŒãžã§ã³ 3.4 ã§å€æŽ: The import system has taken over the boilerplate responsibilities of
loaders. These were previously performed by the
importlib.abc.Loader.load_module() method.
5.4.1. ããŒããŒÂ¶
ã¢ãžã¥ãŒã«ããŒããŒã¯ãããŒãã®éèŠãªæ©èœã§ããã¢ãžã¥ãŒã«å®è¡æ©èœãæäŸããŸããã€ã³ããŒãæ©æ§ã¯ãå®è¡ããããšããã¢ãžã¥ãŒã«ãªããžã§ã¯ããåäžã®åŒæ°ãšã㊠importlib.abc.Loader.exec_module() ã¡ãœãããåŒã³åºããŸãã importlib.abc.Loader.exec_module() ããè¿ãããä»»æã®å€ã¯ç¡èŠãããŸãã
ããŒããŒã¯ä»¥äžã®ä»æ§ãæºãããŠããªããã°ãããŸãã:
ã¢ãžã¥ãŒã«ã (çµã¿èŸŒã¿ã¢ãžã¥ãŒã«ãåçã«èªã¿èŸŒãŸããæ¡åŒµã¢ãžã¥ãŒã«ã§ã¯ãªããŠ) Python ã¢ãžã¥ãŒã«ã ã£ãå ŽåãããŒããŒã¯ã¢ãžã¥ãŒã«ã®ã°ããŒãã«åå空é (
module.__dict__) ã§ãã¢ãžã¥ãŒã«ã®ã³ãŒããå®è¡ãã¹ãã§ããexec_module()ã®åŒã³åºãäžã«ImportError以å€ã®äŸå€ãéåºãããäŒæãããŠãããšããŠããã¢ãžã¥ãŒã«ãããŒãã§ããªãå Žåã¯ImportErrorãéåºãã¹ãã§ãã
å€ãã®å Žåããã¡ã€ã³ããŒãšããŒããŒã¯åããªããžã§ã¯ãã§æ§ããŸãã; ãã®ãããªå Žåã§ã¯ find_spec() ã¡ãœããã¯åã« self (蚳泚: ãªããžã§ã¯ãèªèº«) ãè¿ãã ãã§ãã
ã¢ãžã¥ãŒã«ããŒããŒã¯ã create_module() ã¡ãœãããå®è£
ããããšã§ããŒãäžã«ã¢ãžã¥ãŒã«ãªããžã§ã¯ããäœæããããšãéžæã§ããŸãããã®ã¡ãœããã¯ãã¢ãžã¥ãŒã«ä»æ§ãåŒæ°ã«åã£ãŠãããŒãäžã«äœ¿ãæ°ããã¢ãžã¥ãŒã«ãªããžã§ã¯ããè¿ããŸãã create_module() ã¯ã¢ãžã¥ãŒã«ãªããžã§ã¯ãã«å±æ§ãèšå®ããå¿
èŠã¯ãããŸããããããã®ã¡ãœããã None ãè¿ããªããã€ã³ããŒãæ©æ§ã¯æ°ããã¢ãžã¥ãŒã«ãèªèº«ã§äœæããŸãã
Added in version 3.4: ããŒããŒã® create_module() ã¡ãœããã
ããŒãžã§ã³ 3.4 ã§å€æŽ: The load_module() method was replaced by
exec_module() and the import
machinery assumed all the boilerplate responsibilities of loading.
æ¢åã®ããŒããŒãšã®äºææ§ã®ãããããããŒããŒã« load_module() ã¡ãœãããååšãããã€ããŒããŒã exec_module() ãå®è£
ããŠããªããã°ãã€ã³ããŒãæ©æ§ã¯ããŒããŒã® load_module() ã¡ãœããã䜿ããŸãããããã load_module() 㯠deprecated ã§ãããããŒããŒã¯ä»£ããã« exec_module() ãå®è£
ãã¹ãã§ãã
load_module() ã¡ãœããã¯ãã¢ãžã¥ãŒã«ãå®è¡ããããšã«å ããŠäžèšã§èª¬æããããã¹ãŠã®å®åçãªããŒãæ©èœã宿œããªããã°ãªããŸãããåãå¶çŽãé©çšãããŸãã以äžã¯è¿œå ã®æç¢ºåã§ã:
sys.modulesã«äžããããååã®ã¢ãžã¥ãŒã«ãååšããŠããå ŽåãããŒããŒã¯ãã®æ¢åã®ã¢ãžã¥ãŒã«ã䜿ããªããã°ãããŸããã (ããããªããšimportlib.reload()ã¯æ£ããåããªãã§ãããã) æå®ãããã¢ãžã¥ãŒã«ãsys.modulesã«ååšããªãå ŽåãããŒããŒã¯æ°ããã¢ãžã¥ãŒã«ãªããžã§ã¯ããäœæããsys.modulesã«è¿œå ããªããã°ãããŸãããç¡éã®ååž°ãŸãã¯è€æ°åã®ããŒãã鲿¢ããããã«ãããŒããŒãã¢ãžã¥ãŒã«ã³ãŒããå®è¡ããåã«ã¢ãžã¥ãŒã«ã¯
sys.modulesã«ååšããªããã°ãªããŸãã (must)ãããŒãåŠçã«å€±æããå ŽåãããŒããŒã¯
sys.modulesã«è¿œå ããã¢ãžã¥ãŒã«ãåãé€ããªããã°ãããŸããããããã¯ããŒãã«å€±æããã¢ãžã¥ãŒã« ã®ã¿ ãããã®ã¢ãžã¥ãŒã«ãããŒããŒèªèº«ã«æç€ºçã«ããŒããããå Žåã«éããé€å»ããªããã°ãªããŸããã
ããŒãžã§ã³ 3.5 ã§å€æŽ: exec_module() ãå®çŸ©ãããŠã㊠create_module() ãå®çŸ©ãããŠããªãå Žåã DeprecationWarning ãéåºãããããã«ãªããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ: exec_module() ãå®çŸ©ãããŠã㊠create_module() ãå®çŸ©ãããŠããªãå Žåã ImportError ãéåºãããããã«ãªããŸããã
ããŒãžã§ã³ 3.10 ã§å€æŽ: load_module() ã䜿çšãããš ImportWarning ãçºçããŸãã
5.4.2. ãµãã¢ãžã¥ãŒã«Â¶
ãµãã¢ãžã¥ãŒã«ãããŒãããã®ã«ã©ã®ãããªã¡ã«ããºã (äŸãã°ã importlib API ã import ãŸã㯠import-from ã¹ããŒãã¡ã³ãããŸãã¯ãã«ãã€ã³é¢æ°ã® __import__) ã䜿ãããå Žåã§ãããã€ã³ãã£ã³ã°ã¯ãµãã¢ãžã¥ãŒã«ãªããžã§ã¯ãã芪ã¢ãžã¥ãŒã«ã®åå空éã«é
眮ããŸããäŸãã°ãããããã±ãŒãž spam ããµãã¢ãžã¥ãŒã« foo ãæã£ãŠããå Žåã spam.foo ãã€ã³ããŒãããåŸã¯ spam ã¯å€ããµãã¢ãžã¥ãŒã«ã«æçžããã屿§ foo ãæã¡ãŸãã以äžã®ãã£ã¬ã¯ããªæ§é ãæã£ãŠãããšããŸããã:
spam/
__init__.py
foo.py
ãã㊠spam/__init__.py ã¯ä»¥äžã®ããã«ãªã£ãŠãããšããŸã:
from .foo import Foo
ãã®ãšãã以äžãå®è¡ããããšã«ãã spam ã¢ãžã¥ãŒã«ã®äžã« foo ãš Foo ã«æçžãããååã眮ãããŸã:
>>> import spam
>>> spam.foo
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>
>>> spam.Foo
<class 'spam.foo.Foo'>
Python ã®æ
£ã芪ããã ååæçžã«ãŒã«ãããããšããã¯é©ããããããŸããããããã¯å®éã€ã³ããŒãã·ã¹ãã ã®åºæ¬çãªæ©èœã§ããäžå€ã«ä¿ããªããã°ãªããªãã®ã¯ (äžèšã®ã€ã³ããŒãã®åŸãªã©ã§) sys.modules['spam'] ãš sys.modules['spam.foo'] ãååšããå ŽåãåŸè
ãåè
ã® foo 屿§ãšããŠååšããªããã°ãªããªããšããããšã§ãã
5.4.3. Module specs¶
ã€ã³ããŒãæ©æ§ã¯ãã€ã³ããŒãã®é (ç¹ã«ããŒãã®å) ã«ãåã ã®ã¢ãžã¥ãŒã«ã«ã€ããŠã®ããŸããŸãªæ å ±ãæ±ããŸããæ å ±ã®ã»ãšãã©ã¯ãã¹ãŠã®ã¢ãžã¥ãŒã«ã§å ±éã§ããã¢ãžã¥ãŒã«ä»æ§ã®ç®çã¯ããã®ã€ã³ããŒãé¢é£ã®æ å ±ãã¢ãžã¥ãŒã«ã®åäœã§ã«ãã»ã«åããããšã§ãã
ã€ã³ããŒãã®éã«ã¢ãžã¥ãŒã«ä»æ§ã䜿ãããšã¯ãã€ã³ããŒãã·ã¹ãã ã³ã³ããŒãã³ãéãäŸãã°ã¢ãžã¥ãŒã«ä»æ§ãäœæãããã¡ã€ã³ããŒãšãããå®è¡ããããŒããŒã®éã§ç¶æ ã転éããããšãå¯èœã«ããŸããæãéèŠãªã®ã¯ãããã«ãã£ãŠã€ã³ããŒãæ©æ§ãããŒãã®å®åçãªäœæ¥ãå®è¡ã§ããããã«ãªããšããããšã§ããããã«å¯ŸããŠãã¢ãžã¥ãŒã«ä»æ§ãªãã§ã¯ããŒãããã®è²¬ä»»ãæ ã£ãŠããŸããã
The module's spec is exposed as module.__spec__. Setting
__spec__ appropriately applies equally to
modules initialized during interpreter startup.
The one exception is __main__, where __spec__ is
set to None in some cases.
See ModuleSpec for details on the contents of
the module spec.
Added in version 3.4.
5.4.4. __path__ attributes on modules¶
The __path__ attribute should be a (possibly empty)
sequence of strings enumerating the locations where the package's
submodules will be found. By definition, if a module has a __path__
attribute, it is a package.
A package's __path__ attribute is used during imports of its
subpackages.
Within the import machinery, it functions much the same as sys.path,
i.e. providing a list of locations to search for modules during import.
However, __path__ is typically much more constrained than
sys.path.
The same rules used for sys.path also apply to a package's
__path__. sys.path_hooks (described below) are
consulted when traversing a package's __path__.
A package's __init__.py file may set or alter the package's
__path__
attribute, and this was typically the way namespace packages were implemented
prior to PEP 420. With the adoption of PEP 420, namespace packages no
longer need to supply __init__.py files containing only __path__
manipulation code; the import machinery automatically sets __path__
correctly for the namespace package.
5.4.5. ã¢ãžã¥ãŒã«ã® repr¶
ããã©ã«ãã§ã¯ããã¹ãŠã®ã¢ãžã¥ãŒã«ã¯å©çšå¯èœãª repr ãæã£ãŠããŸãããã ãããã¯ããããŸã§ã«èª¬æãã屿§ã®èšå®å 容ã«äŸåããŠãããã¢ãžã¥ãŒã«ä»æ§ã«ãã£ãŠã¢ãžã¥ãŒã«ãªããžã§ã¯ãã® repr ãããæç€ºçã«å¶åŸ¡ããããšãã§ããŸãã
ããã¢ãžã¥ãŒã«ã仿§ (__spec__) ãæã£ãŠããã°ãã€ã³ããŒãæ©æ§ã¯ãããã repr ãçæããããšããŸããããããã倱æãããããŸãã¯ä»æ§ãååšããªããã°ãã€ã³ããŒãã·ã¹ãã ã¯ã¢ãžã¥ãŒã«ã§å
¥æå¯èœãªããããæ
å ±ã䜿ã£ãŠããã©ã«ãã® repr ãæ§ç¯ããŸãããã㯠module.__name__, module.__file__, module.__loader__ ã (è¶³ããªãæ
å ±ã«ã€ããŠã¯ããã©ã«ãå€ã䜿ã£ãŠè£ããªãã) repr ãžã®å
¥åãšããŠäœ¿ãããšè©Šã¿ãŸãã
ããã䜿ãããŠããæ£ç¢ºãªèŠåã§ã:
ã¢ãžã¥ãŒã«ã
__spec__屿§ãæã£ãŠããã°ã仿§ã«å«ãŸããæ å ±ã repr ãçæããããã«äœ¿ãããŸãã "name", "loader", "origin", "has_location" 屿§ãåç §ãããŸããã¢ãžã¥ãŒã«ã«
__file__屿§ãããå Žåã¯ãã¢ãžã¥ãŒã«ã® repr ã®äžéšãšããŠäœ¿ãããŸããã¢ãžã¥ãŒã«ã«
__file__ã¯ãªãã__loader__ãããããã®å€ãNoneã§ã¯ãªãå Žåã¯ãããŒããŒã® repr ãã¢ãžã¥ãŒã«ã® repr ã®äžéšãšããŠäœ¿ãããŸããããã§ãªããã°ãåã«ã¢ãžã¥ãŒã«ã®
__name__ã repr ã®äžã§äœ¿ããŸãã
ããŒãžã§ã³ 3.12 ã§å€æŽ: Use of module_repr(), having been deprecated since Python 3.4, was
removed in Python 3.12 and is no longer called during the resolution of a
module's repr.
5.4.6. ãã£ãã·ã¥ããããã€ãã³ãŒãã®ç¡å¹å¶
Before Python loads cached bytecode from a .pyc file, it checks whether the
cache is up-to-date with the source .py file. By default, Python does this
by storing the source's last-modified timestamp and size in the cache file when
writing it. At runtime, the import system then validates the cache file by
checking the stored metadata in the cache file against the source's
metadata.
Python also supports "hash-based" cache files, which store a hash of the source
file's contents rather than its metadata. There are two variants of hash-based
.pyc files: checked and unchecked. For checked hash-based .pyc files,
Python validates the cache file by hashing the source file and comparing the
resulting hash with the hash in the cache file. If a checked hash-based cache
file is found to be invalid, Python regenerates it and writes a new checked
hash-based cache file. For unchecked hash-based .pyc files, Python simply
assumes the cache file is valid if it exists. Hash-based .pyc files
validation behavior may be overridden with the --check-hash-based-pycs
flag.
ããŒãžã§ã³ 3.7 ã§å€æŽ: Added hash-based .pyc files. Previously, Python only supported
timestamp-based invalidation of bytecode caches.
5.5. ãã¹ããŒã¹ã»ãã¡ã€ã³ããŒÂ¶
äžã§è§Šããéãã Python ã«ã¯ããã€ãã®ããã©ã«ãã®ã¡ã¿ãã¹ã»ãã¡ã€ã³ããŒãåãã£ãŠããŸãããã®ãã¡ã® 1 ã€ã¯ ãã¹ããŒã¹ã»ãã¡ã€ã³ã㌠(PathFinder) ãšåŒã°ãã ãã¹ãšã³ã㪠ã®ãªã¹ãã§ãã ã€ã³ããŒããã¹ ãæ€çŽ¢ããŸããããããã®ãã¹ãšã³ããªã¯ãã¢ãžã¥ãŒã«ãæ¢ãå ŽæãæããŠããŸãã
ãã¹ããŒã¹ã»ãã¡ã€ã³ããŒèªäœã¯äœãã®ã€ã³ããŒãæ¹æ³ãç¥ã£ãŠããããã§ã¯ãããŸããããã®ä»£ããã«ãåã ã®ãã¹ãšã³ããªãèµ°æ»ããããããã«ç¹å®ã®çš®é¡ã®ãã¹ã®æ±ããç¥ã£ãŠãããã¹ãšã³ããªã»ãã¡ã€ã³ããŒãé¢é£ä»ããŸãã
ããã©ã«ãã®ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒã¯ããã¡ã€ã«ã·ã¹ãã äžã®ã¢ãžã¥ãŒã«ãèŠã€ããããã®ãã¹ãŠã®ã»ãã³ãã£ã¯ã¹ãå®è£
ããŠããŸãããã㯠Python ãœãŒã¹ã³ãŒã (.py ãã¡ã€ã«) ãPython ãã€ãã³ãŒã (.pyc ãã¡ã€ã«) ãå
±æã©ã€ãã©ãª (äŸãã° .so ãã¡ã€ã«) ãªã©ã®ç¹å¥ãªãã¡ã€ã«ã¿ã€ããåŠçããŸããæšæºã©ã€ãã©ãªã® zipimport ã¢ãžã¥ãŒã«ã«ãã£ãŠãµããŒããããå Žåã¯ãããã©ã«ãã®ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒã¯ (å
±æã©ã€ãã©ãªä»¥å€ã®) ãã¹ãŠã®ãã¡ã€ã«ã¿ã€ãã® zip ãã¡ã€ã«ããã®ããŒããæ±ããŸãã
ãã¹ãšã³ããªã¯ãã¡ã€ã«ã·ã¹ãã äžã®å Žæã«éå®ãããå¿ èŠã¯ãããŸãããURL ãããŒã¿ããŒã¹ã¯ãšãªããã®ä»æååã§æå®ã§ããå Žæãåç §ããããšãå¯èœã§ãã
ãã¹ããŒã¹ã»ãã¡ã€ã³ããŒã«ã¯ããã¯ããããã³ã«ã远å ããããšãã§ããããã«ãã£ãŠæ€çŽ¢å¯èœãªãã¹ãšã³ããªã®çš®é¡ãæ¡åŒµããã«ã¹ã¿ãã€ãºããããšãã§ããŸããäŸãã°ããããã¯ãŒã¯äžã® URL ããã¹ãšã³ããªãšããŠãµããŒããããå Žåã web äžã®ã¢ãžã¥ãŒã«ãèŠã€ããããã« HTTP ã®åãæ±ãæ¹ãå®è£ ããããã¯ãæžãããšãã§ããŸãããã® (åŒã³åºãå¯èœãªããžã§ã¯ãã§ãã) ããã¯ã¯ãäžã§è§£èª¬ãããããã³ã«ããµããŒããã ãã¹ãšã³ããªã»ãã¡ã€ã³ã㌠ãè¿ããŸãããã®ãããã³ã«ã¯ web ããã¢ãžã¥ãŒã«ã®ããŒããŒãååŸããã®ã«äœ¿ãããŸãã
èŠåã®èšè: ãã®ç¯ãšåã®ç¯ã®äž¡æ¹ã§ ãã¡ã€ã³ã㌠ãšããèšèãã ã¡ã¿ãã¹ã»ãã¡ã€ã³ã㌠㚠ãã¹ãšã³ããªã»ãã¡ã€ã³ã㌠ãšããçšèªã§åºå¥ãããŠäœ¿ãããŠããŸããããã 2 çš®é¡ã®ãã¡ã€ã³ããŒã¯éåžžã«äŒŒãŠããã䌌ããããã³ã«ããµããŒãããã€ã³ããŒãåŠçã§åãããã«æ©èœããŸããã埮åŠã«ç°ãªã£ãŠããã®ãå¿ã«çããŠããã®ã¯éèŠã§ããç¹ã«ãã¡ã¿ãã¹ã»ãã¡ã€ã³ããŒã¯ã€ã³ããŒãåŠçã®éå§æã sys.meta_path ã®èµ°æ»ãåããšãã«åäœããŸãã
ãããšã¯å¯Ÿç
§çã«ããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã¯ããæå³ã§ãã¹ããŒã¹ã»ãã¡ã€ã³ããŒã®å®è£
詳现ã§ãããå®é sys.meta_path ãããã¹ããŒã¹ã»ãã¡ã€ã³ããŒãåãé€ãããå Žåããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã®å®è£
ã¯äœãå®è¡ãããªãã§ãããã
5.5.1. ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒÂ¶
ãã¹ããŒã¹ã»ãã¡ã€ã³ã㌠ã«ã¯ãæåå ãã¹ãšã³ããª ã§æå®ãããå Žæã® Python ã¢ãžã¥ãŒã«ã Python ããã±ãŒãžãèŠã€ããããŒããã責任ããããŸããã»ãšãã©ã®ãã¹ãšã³ããªã¯ãã¡ã€ã«ã·ã¹ãã äžã®å Žæãæå®ããŠããŸãããããã«å¶éãããå¿ èŠã¯ãããŸããã
ã¡ã¿ãã¹ã»ãã¡ã€ã³ããŒãšããŠã ãã¹ããŒã¹ã»ãã¡ã€ã³ã㌠ã«ã¯åã«è§£èª¬ãã find_spec() ãããã³ã«ãå®è£
ãããŠããŸãããããã«å ã㊠ã€ã³ããŒããã¹ ããã¢ãžã¥ãŒã«ãèŠã€ããããŒãããæ¹æ³ãã«ã¹ã¿ãã€ãºããããã«äœ¿ããããã¯ãæäŸããŠããŸãã
ãã¹ããŒã¹ã»ãã¡ã€ã³ã㌠㯠sys.path ã sys.path_hooks ã sys.path_importer_cache ãšãã 3 ã€ã®å€æ°ã䜿ããŸããããã«ããã±ãŒãžãªããžã§ã¯ãã® __path__ 屿§ã䜿ããŸãããããã«ãã£ãŠãã€ã³ããŒãåŠçãã«ã¹ã¿ãã€ãºããæ¹æ³ãæäŸãããŸãã
sys.path contains a list of strings providing search locations for
modules and packages. It is initialized from the PYTHONPATH
environment variable and various other installation- and
implementation-specific defaults. Entries in sys.path can name
directories on the file system, zip files, and potentially other "locations"
(see the site module) that should be searched for modules, such as
URLs, or database queries. Only strings should be present on
sys.path; all other data types are ignored.
ãã¹ããŒã¹ã»ãã¡ã€ã³ã㌠㯠ã¡ã¿ãã¹ã»ãã¡ã€ã³ã㌠ãªã®ã§ãã€ã³ããŒãæ©æ§ã¯ãåã§è§£èª¬ãããã¹ããŒã¹ã»ãã¡ã€ã³ããŒã® find_spec() ã¡ãœãããåŒã³åºãããšã§ ã€ã³ããŒããã¹ ã®æ€çŽ¢ãå§ããŸãã path åŒæ°ã find_spec() ã«æž¡ããããšãã¯ãããã¯èµ°æ»ãããã¹æååã®ãªã¹ã - å
žåçã«ã¯ãã®ããã±ãŒãžã®äžã§ã€ã³ããŒãããŠããããã±ãŒãžã® __path__ 屿§ã«ãªããŸãã path åŒæ°ã None ã ã£ãå Žåãããã¯æäžäœã®ã€ã³ããŒãã§ããããšã瀺ããŠããŠã sys.path ã䜿ãããŸãã
The path based finder iterates over every entry in the search path, and
for each of these, looks for an appropriate path entry finder
(PathEntryFinder) for the
path entry. Because this can be an expensive operation (e.g. there may be
stat() call overheads for this search), the path based finder maintains
a cache mapping path entries to path entry finders. This cache is maintained
in sys.path_importer_cache (despite the name, this cache actually
stores finder objects rather than being limited to importer objects).
In this way, the expensive search for a particular path entry
location's path entry finder need only be done once. User code is
free to remove cache entries from sys.path_importer_cache forcing
the path based finder to perform the path entry search again.
path entry ããã£ãã·ã¥ã®äžã«ç¡ãã£ãå Žåã path based finder 㯠sys.path_hooks ã®äžã®åŒã³åºãå¯èœãªããžã§ã¯ããå
šãŠèŸ¿ããŸãã
ãã®ãªã¹ãã®ããããã® path entry ãã㯠ã¯ãæ€çŽ¢ãã path entry ãšããåŒæ° 1 ã€ãæž¡ããŠåŒã³åºãããŸãã
ãã®åŒã³åºãå¯èœãªããžã§ã¯ã㯠path entry ãæ±ãã path entry finder ãè¿ããã ImportError ãéåºããŸãã
ImportError ã¯ãããã¯ã path entry ã®ããã® path entry finder ãæ¢ããªãããšãå ±ããããã« path based finder ã䜿ããŸãã
ãã®äŸå€ã¯åŠçãããã import path ã蟿ã£ãŠããåŠçãç¶ããããŸãã
ããã¯ã¯åŒæ°ãšããŠæååãŸãã¯ãã€ãåãªããžã§ã¯ããæåŸ
ããŸã;
ãã€ãåãªããžã§ã¯ãã®ãšã³ã³ãŒãã£ã³ã°ã¯ããã¯ã«ä»»ãããŠã㊠(äŸãã°ããã¡ã€ã«ã·ã¹ãã ã®ãšã³ã³ãŒãã£ã³ã°ã® UTF-8 ããã以å€ãªã©ã§ã) ãããã¯ãåŒæ°ããã³ãŒãã§ããªãã£ãå Žå㯠ImportError ãéåºãã¹ãã§ãã
sys.path_hooks ã蟿ãåŠçã ãã¹ãšã³ããªã»ãã¡ã€ã³ã㌠ãäœãè¿ããã«çµãã£ãå Žåããã¹ããŒã¹ã»ãã¡ã€ã³ããŒã® find_spec() ã¡ãœããã¯ã sys.path_importer_cache ã« (ãã®ãã¹ãšã³ããªã«å¯Ÿãããã¡ã€ã³ããŒãååšããªãããšã瀺ãããã«) None ãä¿åãã ã¡ã¿ãã¹ã»ãã¡ã€ã³ã㌠ã¯ã¢ãžã¥ãŒã«ãèŠã€ãããªãã£ãããšãäŒããããã« None ãè¿ããŸãã
sys.path_hooks äžã® ãã¹ãšã³ããªãã㯠åŒã³åºãå¯èœãªããžã§ã¯ãã®æ»ãå€ã®ããããã ãã¹ãšã³ããªã»ãã¡ã€ã³ã㌠ã§ãã£ã å ŽåãåŸã§åºãŠããã¢ãžã¥ãŒã«ä»æ§ãæ¢ãããã®ãããã³ã«ã䜿ããããããã¢ãžã¥ãŒã«ãããŒãããããã«äœ¿ãããŸãã
The current working directory -- denoted by an empty string -- is handled
slightly differently from other entries on sys.path. First, if the
current working directory cannot be determined or is found not to exist, no
value is stored in sys.path_importer_cache. Second, the value for the
current working directory is looked up fresh for each module lookup. Third,
the path used for sys.path_importer_cache and returned by
importlib.machinery.PathFinder.find_spec() will be the actual current
working directory and not the empty string.
5.5.2. ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒã»ãããã³ã«Â¶
ã¢ãžã¥ãŒã«ãšåæåãããããã±ãŒãžã®ã€ã³ããŒãããµããŒããããããããã³åå空éããã±ãŒãžã®ããŒã·ã§ã³ãšããŠæäŸããããã«ããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã¯ find_spec() ã¡ãœãããå®è£
ããªããã°ãããŸããã
find_spec() 㯠2 ã€ã®åŒæ°ãåããŸããã€ã³ããŒãããããšããŠããã¢ãžã¥ãŒã«ã®å®å
šä¿®é£Ÿåãšã (ãªãã·ã§ã³ã®) 察象ã¢ãžã¥ãŒã«ã§ãã find_spec() ã¯ã¢ãžã¥ãŒã«ã«å¯Ÿå¿ããå®å
šã«åæå (populated) ããã仿§ãè¿ããŸãããã®ä»æ§ã¯ (1ã€ã®äŸå€ãé€ããŠ) åžžã« "loader" ã»ãããæã£ãŠããŸãã
To indicate to the import machinery that the spec represents a namespace
portion, the path entry finder sets submodule_search_locations to
a list containing the portion.
ããŒãžã§ã³ 3.4 ã§å€æŽ: find_spec() replaced
find_loader() and
find_module(), both of which
are now deprecated, but will be used if find_spec() is not defined.
å€ããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã®äžã«ã¯ã find_spec() ã®ä»£ããã«ããã 2 ã€ã® deperecated ãªã¡ãœããã®ãã¡ã®ãããããå®è£
ããŠãããã®ããããããããŸããããããã®ã¡ãœããã¯åŸæ¹äºææ§ã®ããã«ãŸã èæ
®ãããŠããŸãããããããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã« find_spec() ãå®è£
ãããŠããã°ãå€ãã¡ãœããã¯ç¡èŠãããŸãã
find_loader() takes one argument, the
fully qualified name of the module being imported. find_loader()
returns a 2-tuple where the first item is the loader and the second item
is a namespace portion.
ä»ã®ã€ã³ããŒãæ©æ§ã®å®è£
ã«å¯ŸããåŸæ¹äºææ§ã®ããã«ãå€ãã®ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒã¯ãã¡ã¿ãã¹ã»ãã¡ã€ã³ããŒããµããŒãããã®ãšåãäŒçµ±ç㪠find_module() ã¡ãœããããµããŒãããŠããŸãããããããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã® find_module() ã¡ãœããã¯ã決ã㊠path åŒæ°ã§ã¯åŒã³åºãããŸãã (ãã®ã¡ãœããã¯ããã¹ããã¯ã®æåã®åŒã³åºãããé©åãªãã¹æ
å ±ãèšé²ããåäœãæåŸ
ãããŠããŸã)ã
ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒã® find_module() ã¡ãœãã㯠deprecated ã§ãããªããªãããã®æ¹æ³ã§ã¯ãã¹ãšã³ããªã»ãã¡ã€ã³ããŒãåå空éããã±ãŒãžã«å¯ŸããŠããŒã·ã§ã³ãæäŸããããšãã§ããªãããã§ãããã find_loader() ãš find_module() ã®äž¡æ¹ããã¹ãšã³ããªã»ãã¡ã€ã³ããŒã«ååšããããã€ã³ããŒãã·ã¹ãã ã¯åžžã« find_module() ããã find_loader() ãåªå
ããŠåŒã³åºããŸãã
ããŒãžã§ã³ 3.10 ã§å€æŽ: Calls to find_module() and
find_loader() by the import
system will raise ImportWarning.
ããŒãžã§ã³ 3.12 ã§å€æŽ: find_module() and find_loader() have been removed.
5.6. æšæºã®ã€ã³ããŒãã·ã¹ãã ã眮ãæãã¶
ã€ã³ããŒãã·ã¹ãã å
šäœã眮ãæããããã®æãä¿¡é Œæ§ã®ããä»çµã¿ã¯ã sys.meta_path ã®ããã©ã«ãã®å
容ãåé€ããå
šéšãã«ã¹ã¿ã ã®ã¡ã¿ãã¹ããã¯ã§çœ®ãæãããã®ã§ãã
If it is acceptable to only alter the behaviour of import statements
without affecting other APIs that access the import system, then replacing
the builtin __import__() function may be sufficient.
(æšæºã®ã€ã³ããŒãã·ã¹ãã å
šäœã忢ããã®ã§ã¯ãªã) ãã§ã«ã¡ã¿ãã¹ã«ããããã¯ããããã¢ãžã¥ãŒã«ã®ã€ã³ããŒããéžæçã«é²ãããã«ã¯ã find_spec() ãã None ãè¿ã代ããã«ãçŽæ¥ ModuleNotFoundError ãéåºããã ãã§ååã§ãã None ãè¿ãã®ã¯ã¡ã¿ãã¹ã®èµ°æ»ãç¶ããã¹ãã§ããããšãæå³ããŸãããäŸå€ãéåºãããšããã«èµ°æ»ãæã¡åããŸãã
5.7. Package Relative Imports¶
Relative imports use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots indicate a relative import to the parent(s) of the current package, one level per dot after the first. For example, given the following package layout:
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
In either subpackage1/moduleX.py or subpackage1/__init__.py,
the following are valid relative imports:
from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
Absolute imports may use either the import <> or from <> import <>
syntax, but relative imports may only use the second form; the reason
for this is that:
import XXX.YYY.ZZZ
should expose XXX.YYY.ZZZ as a usable expression, but .moduleY is
not a valid expression.
5.8. __main__ ã«å¯Ÿããç¹å¥ãªèæ ®Â¶
__main__ ã¢ãžã¥ãŒã«ã¯ã Python ã®ã€ã³ããŒãã·ã¹ãã ã«é¢é£ããç¹å¥ãªã±ãŒã¹ã§ãã ä»ã®å Žæ ã§èšåãããŠããããã«ã __main__ ã¢ãžã¥ãŒã«ã¯ sys ã builtins ãªã©ãšåæ§ã«ã€ã³ã¿ããªã¿ãŒã¹ã¿ãŒãã¢ããã§çŽæ¥åæåãããŸããããããåè
2 ã€ã®ã¢ãžã¥ãŒã«ãšéã£ãŠã __main__ ã¯å³å¯ã«ã¯ãã«ãã€ã³ã®ã¢ãžã¥ãŒã«ãšããŠã®è³æ Œãæã£ãŠããŸãããããã¯ã __main__ ãåæåãããæ¹æ³ãã€ã³ã¿ããªã¿ãèµ·åããããšãã®ãã©ã°ããã®ä»ã®ãªãã·ã§ã³ã«äŸåããããã§ãã
5.8.1. __main__.__spec__¶
__main__ ãã©ã®ããã«åæåããããã«äŸåããŠã __main__.__spec__ ã¯é©åã«èšå®ãããããšãããã° None ã«ãªãããšããããŸãã
Python ã -m ãªãã·ã§ã³ãä»ããŠå®è¡ãããå Žåã«ã¯ã __spec__ ã¯å¯Ÿå¿ããã¢ãžã¥ãŒã«ãŸãã¯ããã±ãŒãžã®ã¢ãžã¥ãŒã«ä»æ§ã«èšå®ãããŸãããŸãããã£ã¬ã¯ããªã zip ãã¡ã€ã«ããŸãã¯ä»ã® sys.path ãšã³ããªãå®è¡ããåŠçã®äžéšãšã㊠__main__ ã¢ãžã¥ãŒã«ãããŒããããå Žåã«ã __spec__ ãçæ (populate) ãããŸãã
ãã以å€ã®ã±ãŒã¹ ã§ã¯ã __main__.__spec__ 㯠None ã«èšå®ãããŸããããã¯ã __main__ ãçæ (populate) ããããã«äœ¿ãããã³ãŒããã€ã³ããŒãå¯èœãªã¢ãžã¥ãŒã«ãšçŽæ¥äžèŽããŠããªãããã§ã:
察話ããã³ãã
-cãªãã·ã§ã³stdin ããèµ·åãããå Žå
ãœãŒã¹ãã¡ã€ã«ããã€ãã³ãŒããã¡ã€ã«ããçŽæ¥èµ·åãããå Žå
æåŸã®ã±ãŒã¹ã§ã¯ãããšãæè¡çã«ã¯ãã¡ã€ã«ãã¢ãžã¥ãŒã«ãšããŠçŽæ¥ã€ã³ããŒãã§ãã ãšããŠã __main__.__spec__ ã¯åžžã« None ã«ãªãããšã«æ³šæããŠãã ããããã __main__ ã«ãããŠæå¹ãªã¢ãžã¥ãŒã«ã¡ã¿ããŒã¿ãå¿
èŠãªã -m ã¹ã€ããã䜿ã£ãŠãã ããã
__main__ ãã€ã³ããŒãå¯èœãªã¢ãžã¥ãŒã«ãšäžèŽãã __main__.__spec__ ãããã«å¿ããŠèšå®ãããŠãããšããŠããããã§ããªãããã® 2 ã€ã®ã¢ãžã¥ãŒã«ã¯å¥ç©ãšã¿ãªãããããšã«æ³šæããŠãã ãããããã¯ã if __name__ == "__main__": ãã§ãã¯ã«ãã£ãŠä¿èšŒããããããã¯ã¯ã __main__ åå空éãçæ (populate) ããããã«ã¢ãžã¥ãŒã«ã䜿çšãããæã«ã ãå®è¡ãããéåžžã®ã€ã³ããŒãæã«ã¯å®è¡ãããªãããšããäºå®ã«èµ·å ããŠããŸãã
5.9. åèè³æÂ¶
Python ã®åæã®é ãããããšãã€ã³ããŒãæ©æ§ã¯ç®èŠãŸããçºå±ãéããŸããã äžéšçްãããšãããããã¥ã¡ã³ããæžããããšãããå€ãã£ãŠã¯ããŸãããæåæã® ããã±ãŒãžã®ä»æ§ ã¯ãŸã èªãããšãã§ããŸãã
ãªãªãžãã«ã® sys.meta_path ã®ä»æ§ã¯ PEP 302 ã§ããã®åŸç¶ãšãªãæ¡åŒµã PEP 420 ã§ãã
PEP 420 introduced namespace packages for
Python 3.3. PEP 420 also introduced the find_loader() protocol as an
alternative to find_module().
PEP 366 ã¯ãã¡ã€ã³ã¢ãžã¥ãŒã«ã§ã®æç€ºçãªçžå¯Ÿã€ã³ããŒãã®ããã«è¿œå ãã __package__ 屿§ã®è§£èª¬ãããŠããŸãã
PEP 328 ã¯çµ¶å¯Ÿã€ã³ããŒããæç€ºçãªçžå¯Ÿã€ã³ããŒããããã³ãåœå __name__ ã§ææ¡ããåŸã« PEP 366 ã __package__ ã§å®ãã仿§ãå°å
¥ããŸããã
PEP 338 ã¯ã¢ãžã¥ãŒã«ãã¹ã¯ãªãããšããŠå®è¡ãããšãã®ä»æ§ãå®ããŠããŸãã
PEP 451 ã¯ãã¢ãžã¥ãŒã«ä»æ§ãªããžã§ã¯ãã«ãããã¢ãžã¥ãŒã«æ¯ã®ã€ã³ããŒãç¶æ ã®ã«ãã»ã«åã远å ããŠããŸãããŸããããŒããŒã®å®åçãªè²¬ä»»ã®ã»ãšãã©ãã€ã³ããŒãæ©æ§ã«è©ä»£ãããããŠããŸãããããã®å€æŽã«ãããã€ã³ããŒãã·ã¹ãã ã®ããã€ãã® API ã deprecate ããããŸããã¡ã€ã³ããŒãšããŒããŒã«ã¯æ°ããã¡ãœããã远å ãããŸããã
èæ³š