ããã¯ããªã«ãããããŠæžãããã®ïŒ
Pythonã®ããã°ã©ã ãèªã¿æžãããŠããŠãimportãããããããªãã£ãã®ã§ãã¡ãããšèŠãŠã¿ãããšã«ããŸããã
importãšã¢ãžã¥ãŒã«ãšããã±ãŒãž
importèªäœã«ã€ããŠã¯ããã¡ãã®ããŒãžãåç §ã
5. インポートシステム — Python 3.8.10 ドキュメント
importã¯ãããPythonã¢ãžã¥ãŒã«ããå¥ã®Pythonã¢ãžã¥ãŒã«ã䜿ãããã®ä»çµã¿ã§ãã
ããã«ããã±ãŒãžãšãããã®ãããã以äžã®èª¬æãåç
§ãããšããã£ããã¢ãžã¥ãŒã«ïŒãã¡ã€ã«ãããã±ãŒãžïŒãã£ã¬ã¯ããªãš
ãšããæãæ¹ã§è¯ãããã§ãïŒå³å¯ã«ã¯ããã§ã¯ãªããšæžãããŠã¯ããŸããïŒã
ããã±ãŒãžã¯ãã¡ã€ã«ã·ã¹ãã ã®ãã£ã¬ã¯ããªãã¢ãžã¥ãŒã«ã¯ãã£ã¬ã¯ããªã«ãããã¡ã€ã«ãšèããããšãã§ããŸãããããã±ãŒãžãã¢ãžã¥ãŒã«ã¯ãã¡ã€ã«ã·ã¹ãã ããçãŸããå¿ èŠã¯ãªãã®ã§ããã®æ¯å©ãé¡é¢éãã«åãåã£ãŠã¯ãããŸããããã®ææžã®ç®çã®ããã«ããã£ã¬ã¯ããªãšãã¡ã€ã«ãšãã䟿å©ãªæ¯å©ã䜿ãããšã«ããŸãã
ã¢ãžã¥ãŒã«ã¯ãäœæãã.py
ãã¡ã€ã«ã®ããšããšããæãã§ãããã€ãŸãããã¡ã€ã«åãã¢ãžã¥ãŒã«ã®ååãšããŠ
åæ ãããŸãããšã
ããã±ãŒãžã«ã€ããŠã®èª¬æã«ã¯ç¶ãããããéåžžã®ããã±ãŒãžãšãåå空éããã±ãŒãžã®2ã€ãããããã§ãã
ãã¹ãŠã®ããã±ãŒãžã¯ã¢ãžã¥ãŒã«ã§ããããã¹ãŠã®ã¢ãžã¥ãŒã«ãããã±ãŒãžãšã¯éããªãããšãå¿ã«çããŠããã®ãéèŠã§ãããããã¯ä»ã®èšãæ¹ããããšãããã±ãŒãžã¯åãªãç¹å¥ãªçš®é¡ã®ã¢ãžã¥ãŒã«ã§ãããšèšããŸãã
絶察importãšçžå¯Ÿimport
importã«ã¯ã絶察importãšçžå¯Ÿimportã®2ã€ãããããã§ãã
çžå¯Ÿimportã¯ããããªæãã«æžãæžãæ¹ã¿ããã§ãã
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
.
ã..
ã䜿ã£ãæžãæ¹ã§ããã
import / Package Relative Imports
ãçŸåšã®ããã±ãŒãžäœçœ®ããèŠãããµãããã±ãŒãžïŒãµããã£ã¬ã¯ããªïŒãçãªèãã®ããšãèšãã®ããšãæã£ãã®ã§ããã
ããããããã§ããªãããã§ãã
Effective PythonãèŠããšãPEP 8ã«åŸã£ãŠçµ¶å¯Ÿimportã䜿ãã¹ãã ãšæžãããŠããŸããã
絶察import ãæšå¥šããŸãããªããªãã絶察import ã®æ¹ãéåžžã¯èªã¿ããããimportã·ã¹ãã ãæ£ããèšå®ãããªãã£ã(ããšãã°ããã±ãŒãžå éšã®ãã£ã¬ã¯ããªã sys.path ã§çµãã£ãŠãã) å Žåã§ããããè¯ãæ¯ãèãããã(ãŸãã¯å°ãªããšãããè¯ããšã©ãŒã¡ãã»ãŒãžãåºã)ããã§ã
確ãã«ã.
ã..
ã䜿ããããã絶察importã§æžããæ¹ãæå¿«ã§ããããã
ä»åã¯ãçžå¯Ÿimportã¯äœ¿ããªãããšã«ããŸãã
sys.pathãšPYTHONPATH
次ã«ãPythonããã°ã©ã ã§ãã¢ãžã¥ãŒã«ãã©ãããæ¢ããã«ã€ããŠã
ãã¡ãã«é¢ãã話é¡ããsys.pathãšPYTHONPATHã®ããã§ãã
ãŸãã¯ãsys.pathã®èª¬æãããPythonã€ã³ã¹ããŒã«æã®å€ãšãPYTHONPATHç°å¢å€æ°ã§æ€çŽ¢ãã¹ã決ãŸãããã§ãã
ã¢ãžã¥ãŒã«ãæ€çŽ¢ãããã¹ã瀺ãæååã®ãªã¹ãã PYTHONPATH ç°å¢å€æ°ãšãã€ã³ã¹ããŒã«æã«æå®ããããã©ã«ããã¹ã§åæåãããŸãã
sys --- ã·ã¹ãã ãã©ã¡ãŒã¿ãšé¢æ° / sys.path
ããã«ããã®å é ã«ã¯èµ·åã¹ã¯ãªãããé 眮ãããŠãããã£ã¬ã¯ããªãå ¥ãããã§ãã
èµ·åæã«åæåãããåŸããªã¹ãã®å é (path[0]) ã«ã¯ Python ã€ã³ã¿ããªã¿ãèµ·åããã¹ã¯ãªããã®ãããã£ã¬ã¯ããªãæ¿å ¥ãããŸããã¹ã¯ãªããã®ãã£ã¬ã¯ããªããªã (ã€ã³ã¿ããªã¿ã察話ã»ãã·ã§ã³ã§èµ·åãããæããã¹ã¯ãªãããæšæºå ¥åããèªã¿èŸŒãã å Žåãªã©) å Žåã path[0] ã¯ç©ºæååãšãªããPython ã¯ã«ã¬ã³ããã£ã¬ã¯ããªããã¢ãžã¥ãŒã«ã®æ€çŽ¢ãéå§ããŸãã
次ã«ãPYTHONPATHã«ã€ããŠããã¡ãã¯ãsys.pathã«ã¢ãžã¥ãŒã«ã®æ€çŽ¢ãã¹ãè¿œå ã§ããç°å¢å€æ°ã®ããã§ãã
ã¢ãžã¥ãŒã«ãã¡ã€ã«ã®ããã©ã«ãã®æ€çŽ¢ãã¹ãè¿œå ããŸãããã®ç°å¢å€æ°ã®ãã©ãŒãããã¯ã·ã§ã«ã® PATH ãšåãã§ã os.pathsep (Unix ãªãã³ãã³ã Windows ãªãã»ãã³ãã³) ã§åºåããã1ã€ä»¥äžã®ãã£ã¬ã¯ããªãã¹ã§ããååšããªããã£ã¬ã¯ããªã¯èŠåãªãã«ç¡èŠãããŸãã
ã³ãã³ãã©ã€ã³ãšç°å¢ / PYTHONPATH
ãã®çµæãsys.pathã¯
- èµ·åã¹ã¯ãªããã®é 眮ãã£ã¬ã¯ããª
- PYTHONPATH
- ã€ã³ã¹ããŒã«æã«æå®ãããããã©ã«ããã¹
ã§æ§æããããªãããã§ãã
ã¹ã¯ãªãããã£ã¬ã¯ããªã¯ã PYTHONPATH ã§æå®ãããã£ã¬ã¯ããªã® å ã«æ¿å ¥ãããŸãã®ã§æ³šæãå¿ èŠã§ãã
ãããã«ãããèµ·åã¹ã¯ãªãããé 眮ãããŠãããã£ã¬ã¯ããªãæãåã«æ¥ããšããããšã§ããã
ãŸããPYTHONPATHã«ã€ããŠã¯ãPYTHONHOMEã®å 容ãããã©ã«ãã§è¿œå ãããããã§ãã
ããã©ã«ãã®æ€çŽ¢ãã¹ã¯ã€ã³ã¹ããŒã«äŸåã§ãããé垞㯠prefix/lib/pythonversion ã§å§ãŸããŸãã (äžã® PYTHONHOME ãåç §ããŠãã ããã) ãã㯠垞㫠PYTHONPATH ã«è¿œå ãããŸãã
PYTYHONHOMEã¯ã
ã³ãã³ãã©ã€ã³ãšç°å¢ / PYTHONHOME
ãããsys.pathã®ã©ãã«å
¥ãããšãããšãPythonã©ã€ãã©ãªã®ã€ã³ã¹ããŒã«å
ã§prefix/lib/pythonversion
ãš
exec_prefix/lib/pythonversion
ã察象ã«ãªããŸãã
ãããŸã§ãèžãŸããŠãsys.pathã®å 容ãã¢ãžã¥ãŒã«æ€çŽ¢ã®ã«ãŒã«ãç解ããã®ã«éèŠã«ãªã£ãŠããã®ããªããšæããŸãã
ããããæ å ±ã調ã¹ããšããã§ãä»åºŠã¯å®éã«è©ŠããŠç¢ºèªããŠã¿ãŸãããã
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã§ãã
$ python3 -V Python 3.8.5
ããã€ããã¿ãŒã³ã亀ããªãããimportã®ç¢ºèªãããŠãããããšæããŸãã
sys.pathã確èªãã
æåã«ãsys.pathã確èªããŠã¿ãŸãããã
One Linerã
$ python3 -c 'import sys; print(sys.path)' ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '$HOME/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
æåã空æåãªã®ã¯ãsys.pathã®ããã¥ã¡ã³ãã«èª¬æããããŸãããã®å Žåãã«ã¬ã³ããã£ã¬ã¯ããªãæ¢çŽ¢ãã¹ã«
ãªãããã§ããã
ã¹ã¯ãªããã®ãã£ã¬ã¯ããªããªã (ã€ã³ã¿ããªã¿ã察話ã»ãã·ã§ã³ã§èµ·åãããæããã¹ã¯ãªãããæšæºå ¥åããèªã¿èŸŒãã å Žåãªã©) å Žåã path[0] ã¯ç©ºæååãšãªããPython ã¯ã«ã¬ã³ããã£ã¬ã¯ããªããã¢ãžã¥ãŒã«ã®æ€çŽ¢ãéå§ããŸãã
sys --- ã·ã¹ãã ãã©ã¡ãŒã¿ãšé¢æ° / sys.path
次ã«ãã¹ã¯ãªãããã¡ã€ã«ãçšæããŠã¿ãŸãã
$ echo 'import sys; print(sys.path)' > run.py $ python3 run.py ['/path/to/script-location-directory', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '$HOME/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
ãã®å Žåãã¹ã¯ãªãããã¡ã€ã«ãé 眮ããããã£ã¬ã¯ããªãsys.pathã®å é ã«å ¥ããŸãã
ãªã®ã§ããããªæãã«ãµããã£ã¬ã¯ããªã«ã¹ã¯ãªãããé
眮ããŠç¢ºèªãããšããããŸã§ã¹ã¯ãªãããé
眮ãããŠãã
ãã£ã¬ã¯ããªãèµ·ç¹ã«ãªã£ãŠããããšã確èªã§ããŸãã
$ mkdir sample && echo 'import sys; print(sys.path)' > sample/run.py $ python3 sample/run.py ['/path/to/current-directory/sample', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '$HOME/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
ããã§ãsys.pathã®é°å²æ°ã¯ããããŸããã
PYTHONPATHãèšå®ããŠã¿ã
次ã«ãPYTHONPATHãèšå®ããŠãå ã»ã©ã®ã¹ã¯ãªãããå®è¡ããŠã¿ãŸãããã
è©Šãã«ã/tmp
ãš/var
ãè¿œå ããŠã¿ãŸãã
$ PYTHONPATH=/tmp:/var python3 run.py ['/path/to/script-location-directory', '/tmp', '/var', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '$HOME/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages'] $ PYTHONPATH=/tmp:/var python3 sample/run.py ['/path/to/current-directory/sample', '/tmp', '/var', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '$HOME/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
確ãã«ãsys.pathã®ãã¹ã¯ãªããã®é 眮ãã£ã¬ã¯ããªã®æ¬¡ã®äœçœ®ãã«è¿œå ãããŸãããã
èšå®æ¹æ³ã¯ããããŸããã
ããã±ãŒãžã䜿ã£ãã¹ã¯ãªãããäœæãã
次ã«ãããã±ãŒãžã䜿ã£ãã¹ã¯ãªãããäœæããŠã¿ãŸãããã
ãããªæãã®ãã£ã¬ã¯ããªæ§æãçšæã
. âââ main.py âââ sub.py âââ one â  âââ say.py âââ two âââ calc.py
ããã±ãŒãžå
ã«ã¯ãmain.py
ããã³å¥ããã±ãŒãžã®ã¢ãžã¥ãŒã«ãåŒã³åºãã¹ã¯ãªãããçšæã
one/say.py
from two import calc def hello(): print('Hello World!! from package one') def plus(a, b): return calc.plus(a, b)
two/calc.py
def plus(a, b): return a + b
èµ·åã¹ã¯ãªãããšåããã£ã¬ã¯ããªã«ããã¢ãžã¥ãŒã«ãçšæã
sub.py
def message(): print('Hello World!! from main directory')
èµ·åã¹ã¯ãªããã¯ãããã±ãŒãžå ã®ã¢ãžã¥ãŒã«ãåŒã³åºããæåŸã«sys.pathã®å 容ã衚瀺ããŸãã
main.py
import sys from one import say import sub if __name__ == '__main__': sub.message() say.hello() print('1 + 3 = ' + str(say.plus(1, 3))) print() print('print sys.path:') for path in sys.path: print(f' {path}')
å®è¡ããŠã¿ãŸãããã
$ python3 main.py Hello World!! from main directory Hello World!! from package one 1 + 3 = 4 print sys.path: /path/to/script-location-directory /usr/lib/python38.zip /usr/lib/python3.8 /usr/lib/python3.8/lib-dynload $HOME/.local/lib/python3.8/site-packages /usr/local/lib/python3.8/dist-packages /usr/lib/python3/dist-packages
ãããªæãã«å®è¡ã§ããŸããã
ãããŸã§ã¯è¯ãããªããšã
èµ·åã¹ã¯ãªãããããã±ãŒãžå ã«çœ®ããå Žå
次ã«ãèµ·åã¹ã¯ãªãããå«ããŠããã±ãŒãžå ã«é 眮ããå Žåã
. âââ app âââ main.py âââ one â  âââ say.py âââ sub.py âââ two âââ calc.py
ãã®app
ãšãããã£ã¬ã¯ããªããããã±ãŒãžãšããŠæ±ãããšã«ããŸãã
åãã¡ã€ã«ã§ä»ã®ã¢ãžã¥ãŒã«ã䜿ãéã®importæã«ã¯ãapp
ãè¿œå ããŸãã
app/main.py
import sys from app.one import say from app import sub if __name__ == '__main__': sub.message() say.hello() print('1 + 3 = ' + str(say.plus(1, 3))) print() print('print sys.path:') for path in sys.path: print(f' {path}')
main.py
ã«ã€ããŠã¯ãimportã®ããã ãfrom
ãimport
ã䜿ãããã«ä¿®æ£ããŸããã
from app import sub
app/sub.py
def message(): print('Hello World!! from main directory')
app/one/say.py
from app.two import calc def hello(): print('Hello World!! from package one') def plus(a, b): return calc.plus(a, b)
app/two/calc.py
def plus(a, b): return a + b
ãããšãé端ã«ããã¯ããŸãåããªããªããŸãã
$ python3 app/main.py Traceback (most recent call last): File "app/main.py", line 3, in <module> from app.one import say ModuleNotFoundError: No module named 'app'
ã¹ã¯ãªãããé 眮ãããŠãããã£ã¬ã¯ããªã«ç§»ã£ãŠå®è¡ããŠããããŸããããŸããã
$ cd app $ python3 main.py Traceback (most recent call last): File "main.py", line 3, in <module> from app.one import say ModuleNotFoundError: No module named 'app'
ã©ã¡ããapp
ãããããªãããšèšã£ãŠããŸãã
ãããæåããããããªãã£ãã®ã§ãããsys.pathãšPYTHONPATHã®èª¬æãèŠãŠãããšããªããšãªãããã£ãŠããŸãã
sys.pathã«è¿œå ãããã®ã¯ããèµ·åã¹ã¯ãªãããé
眮ãããŠãããã£ã¬ã¯ããªãã§ãããä»åã®ãããªæ§æã ãš
app
ããã±ãŒãžã¯sys.pathã®å€åŽã«ããããã§ããã
ãã£ãããå ã®ãã£ã¬ã¯ããªã«æ»ã£ãŠ
$ cd ..
ä»åã®ã±ãŒã¹ã§ããœãŒã¹ã³ãŒããå€æŽããã«ãã®ãŸãŸåããããå Žåã¯ãPYTHONPATHã«app
ããã±ãŒãžã察象ã«å
¥ãããã«
sys.pathã«å«ããã°ããããšããããšã«ãªããŸãã
$ PYTHONPATH=/path/to python3 app/main.py Hello World!! from main directory Hello World!! from package one 1 + 3 = 4 print sys.path: /path/to/app /path/to /usr/lib/python38.zip /usr/lib/python3.8 /usr/lib/python3.8/lib-dynload /home/kazuhira/.local/lib/python3.8/site-packages /usr/local/lib/python3.8/dist-packages /usr/lib/python3/dist-packages
ããã§ã/path/to
ã¯ã«ã¬ã³ããã£ã¬ã¯ããªãšããŸããä»åã¯ã«ã¬ã³ããã£ã¬ã¯ããªã®äžã«app
ããã±ãŒãžã
é
眮ãããŠããããšèªãã§ãã ããã
ãããã¯ãèµ·åã¹ã¯ãªããã ãããã±ãŒãžã®å€ã«çœ®ãïŒèµ·åã¹ã¯ãªãããšåã䞊ã³ã«app
ããã±ãŒãžãé
眮ããïŒã
ãšãã§ããããã
$ mv app/main.py main.py
ãã®å Žåãèµ·åã¹ã¯ãªããããèŠããã£ã¬ã¯ããªå
ã«app
ããã±ãŒãžãå«ãŸãããããã¢ãžã¥ãŒã«ã®æ¢çŽ¢ãã¹ã«å
¥ãã
ãšããããšã«ãªããŸãã
$ python3 main.py Hello World!! from main directory Hello World!! from package one 1 + 3 = 4 print sys.path: /path/to/script-location-directory /usr/lib/python38.zip /usr/lib/python3.8 /usr/lib/python3.8/lib-dynload $HOME/.local/lib/python3.8/site-packages /usr/local/lib/python3.8/dist-packages /usr/lib/python3/dist-packages
çå±ã¯ããã£ãæ°ãããŸãã
ããšãå¥è§£ãšããŠèµ·åã¹ã¯ãªãããapp
ããã±ãŒãžã«çœ®ãããŸãŸãèµ·åã¹ã¯ãªããã®èŠªãã£ã¬ã¯ããªãsys.pathã«è¿œå ããã
ã¿ãããªæ¹æ³ããããšæããŸããã
app/main.py
import os import sys parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(parent_dir) from app.one import say from app import sub if __name__ == '__main__': sub.message() say.hello() print('1 + 3 = ' + str(say.plus(1, 3))) print() print('print sys.path:') for path in sys.path: print(f' {path}')
ããã§ããåãã«ã¯åããŸãã
$ python3 app/main.py Hello World!! from main directory Hello World!! from package one 1 + 3 = 4 print sys.path: /path/to/app /usr/lib/python38.zip /usr/lib/python3.8 /usr/lib/python3.8/lib-dynload $HOME/.local/lib/python3.8/site-packages /usr/local/lib/python3.8/dist-packages /usr/lib/python3/dist-packages /path/to
ãããµã€ãã®ã¹ã¯ãªããã ãšããããŸããããããªãã§ããâŠã
ãŸãšã
Pythonã®importãããããããªãã£ãã®ã§ã調ã¹ãŠã¿ãŸããã
èµ·åã¹ã¯ãªããã®äœçœ®ãšããã±ãŒãžã®æ§æã«æ°ãã€ããªããšãç°¡åã«ããããã ãªãããšã
ã©ããªãã§ãããããPYTHONPATHã§èª¿æŽããã®ãããã®ã§ããããããããšãèµ·åã¹ã¯ãªããã¯ããã±ãŒãžã«å«ããªãæ¹ã
楜ã§ããããïŒ