Pythonããã°ã©ãã³ã°ãããŠãããšãæã ãããããã®ã¹ã¯ãªããã«æž¡ãããåŒæ°ã£ãŠã©ããã£ãŠåããã ã£ãïŒããšãããå®è¡ç°å¢ã®æ å ±ãç¥ããããªããšæãããšã¯ãããŸãããïŒãããªæã«æŽ»èºããã®ããPythonã®æšæºã©ã€ãã©ãªã«å«ãŸãã`sys`ã¢ãžã¥ãŒã«ã§ãã
`sys`ã¢ãžã¥ãŒã«ã¯ãPythonã€ã³ã¿ããªã¿ãå®è¡ç°å¢ãã®ãã®ã«é¢ããæ å ±ã«ã¢ã¯ã»ã¹ããããããããæäœãããããããã®å€æ°ãé¢æ°ãæäŸããŸããäžèŠå°å³ã«èŠãããããããŸããããPythonããã°ã©ã ãšãããåããã·ã¹ãã ãšã®éã®æ©æž¡ã圹ãšããŠãéåžžã«éèŠãªåœ¹å²ãæ ã£ãŠããŸããç¹ã«ãã¹ã¯ãªããã®æè»æ§ãé«ããããç°å¢ã«å¿ããåäœãããããããéã«ã¯å¿ é ã®ç¥èãšãªããŸããâš
ãã®ããã°èšäºã§ã¯ã`sys`ã¢ãžã¥ãŒã«ã®åºæ¬çãªäœ¿ãæ¹ãããå°ãå¿çšçãªæŽ»çšæ¹æ³ãŸã§ãå ·äœäŸã亀ããªãã詳ãã解説ããŠãããŸãããããèªãã°ãããªãã`sys`ã¢ãžã¥ãŒã«ã䜿ãããªãããããã¯ãã«ãªPythonããã°ã©ãã³ã°ãã§ããããã«ãªãã¯ãã§ãïŒðª
`sys`ã¢ãžã¥ãŒã«ã®äž»èŠãªå±æ§ãã¡
`sys`ã¢ãžã¥ãŒã«ã«ã¯ãçŸåšã®Pythonç°å¢ã«é¢ããæ§ã ãªæ å ±ãæ ŒçŽããããå±æ§ãããããŸããããã§ã¯ãç¹ã«ãã䜿ããããã®ãããã€ãèŠãŠãããŸãããã
1. `sys.argv`: ã³ãã³ãã©ã€ã³åŒæ°ã®ãªã¹ã
Pythonã¹ã¯ãªãããå®è¡ãããšããã¿ãŒããã«ïŒã³ãã³ãããã³ããïŒããè¿œå ã®æ å ±ïŒåŒæ°ïŒãæž¡ãããšãã§ããŸãã`sys.argv`ã¯ããããã®ã³ãã³ãã©ã€ã³åŒæ°ããªã¹ããšããŠä¿æããŠããŸãã
ãªã¹ãã®æåã®èŠçŽ `sys.argv[0]` ã«ã¯ãå®è¡ããã¹ã¯ãªããèªèº«ã®ååïŒãã¹ïŒãæ ŒçŽãããŸãããããŠã`sys.argv[1]` 以éã«ãã³ãã³ãã©ã€ã³ã§æå®ããåŒæ°ãé çªã«æååãšããŠæ ŒçŽãããŸãã
# sample_script.py
import sys
print(f"ã¹ã¯ãªããå: {sys.argv[0]}")
print(f"åŒæ°ã®æ°: {len(sys.argv) - 1}")
if len(sys.argv) > 1:
print("æž¡ãããåŒæ°:")
for i, arg in enumerate(sys.argv[1:]):
print(f" åŒæ°{i+1}: {arg} (å: {type(arg)})")
else:
print("åŒæ°ã¯æž¡ãããŸããã§ããã")
ãã®ã¹ã¯ãªãããã¿ãŒããã«ãã以äžã®ããã«å®è¡ããŠã¿ãŸãã
python sample_script.py Hello World 123
å®è¡çµæïŒ
ã¹ã¯ãªããå: sample_script.py
åŒæ°ã®æ°: 3
æž¡ãããåŒæ°:
åŒæ°1: Hello (å: <class 'str'>)
åŒæ°2: World (å: <class 'str'>)
åŒæ°3: 123 (å: <class 'str'>)
ç°¡åãªã¹ã¯ãªããã§ããã° `sys.argv` ã§ååã§ãããããè€éãªåŒæ°ïŒãªãã·ã§ã³åŒæ°ããã©ã°ãªã©ïŒãæ±ãããå Žåã¯ã`argparse` ã¢ãžã¥ãŒã«ã䜿ãæ¹ã䟿å©ã§ãã
2. `sys.path`: ã¢ãžã¥ãŒã«æ€çŽ¢ãã¹ã®ãªã¹ã
Pythonã `import` æã§ã¢ãžã¥ãŒã«ãæ¢ãéã«ãã©ã®ãã£ã¬ã¯ããªãæ€çŽ¢ãããã瀺ããªã¹ãã `sys.path` ã§ãããã®ãªã¹ãã«ã¯ã以äžã®ãã¹ãé çªã«å«ãŸããŠããŸãã
- ã¹ã¯ãªããã眮ãããŠãããã£ã¬ã¯ããªïŒãŸãã¯ã«ã¬ã³ããã£ã¬ã¯ããªïŒ
- ç°å¢å€æ° `PYTHONPATH` ã§æå®ããããã£ã¬ã¯ããª
- Pythonã€ã³ã¹ããŒã«æã«èšå®ãããæšæºã©ã€ãã©ãªãªã©ã®ãã¹
import sys
import pprint # pprintã䜿ããšãªã¹ããèŠããã衚瀺ããã
print("ã¢ãžã¥ãŒã«æ€çŽ¢ãã¹:")
pprint.pprint(sys.path)
`sys.path` ã¯éåžžã®ãªã¹ããªã®ã§ã`append()` ã¡ãœãããªã©ã䜿ã£ãŠããã°ã©ã å®è¡äžã«åçã«æ€çŽ¢ãã¹ãè¿œå ããããšãå¯èœã§ããããã¯ããããžã§ã¯ãåºæã®ã¢ãžã¥ãŒã«ããããã£ã¬ã¯ããªãäžæçã«æ€çŽ¢å¯Ÿè±¡ã«å ãããå Žåãªã©ã«åœ¹ç«ã¡ãŸãã
import sys
# ãããžã§ã¯ãã® 'libs' ãã£ã¬ã¯ããªãæ€çŽ¢ãã¹ã«è¿œå
custom_lib_path = '/path/to/your/project/libs' # å®éã®ãã¹ã«çœ®ãæããŠãã ãã
if custom_lib_path not in sys.path:
sys.path.append(custom_lib_path)
print(f"'{custom_lib_path}' ãæ€çŽ¢ãã¹ã«è¿œå ããŸããã")
# ãã㧠libs ãã£ã¬ã¯ããªå
ã®ã¢ãžã¥ãŒã«ã import ã§ããããã«ãªã
# import my_custom_module
3. `sys.platform`: ãã©ãããã©ãŒã èå¥å
ã¹ã¯ãªãããå®è¡ãããŠãããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ïŒOSïŒã®çš®é¡ãç¥ãããå Žåã«äœ¿ããŸããOSã«ãã£ãŠåŠçãåãããå Žåã«äŸ¿å©ã§ãã
import sys
print(f"å®è¡ç°å¢ã®ãã©ãããã©ãŒã : {sys.platform}")
if sys.platform.startswith('win'):
print("ããã¯Windowsç°å¢ã§ãã")
elif sys.platform.startswith('linux'):
print("ããã¯Linuxç°å¢ã§ãã")
elif sys.platform == 'darwin':
print("ããã¯macOSç°å¢ã§ãã")
else:
print("ãã®ä»ã®ç°å¢ã§ãã")
äžè¬çãªèå¥åã«ã¯ä»¥äžã®ãããªãã®ããããŸãã
- `’win32’` (Windows)
- `’linux’` (Linux)
- `’darwin’` (macOS)
4. `sys.version`: Pythonã€ã³ã¿ããªã¿ã®ããŒãžã§ã³
䜿çšããŠããPythonã®ããŒãžã§ã³æ å ±ãæååãšããŠååŸã§ããŸããããŒãžã§ã³ã«ãã£ãŠæåãç°ãªãæ©èœã䜿ãããå Žåãªã©ã«ç¢ºèªã§ããŸãã
import sys
print(f"Pythonã®ããŒãžã§ã³æ
å ±:\n{sys.version}")
# ããŒãžã§ã³çªå·ã ããã¿ãã«ã§ååŸãããå Žå㯠sys.version_info ã䜿ã
print(f"\nããŒãžã§ã³æ
å ± (ã¿ãã«): {sys.version_info}")
print(f"ã¡ãžã£ãŒããŒãžã§ã³: {sys.version_info.major}")
print(f"ãã€ããŒããŒãžã§ã³: {sys.version_info.minor}")
5. `sys.executable`: Pythonã€ã³ã¿ããªã¿ã®å®è¡ãã¡ã€ã«ã®ãã¹
çŸåšã¹ã¯ãªãããå®è¡ããŠããPythonã€ã³ã¿ããªã¿ïŒ`python` ã `python3` ã³ãã³ããªã©ïŒã®çµ¶å¯Ÿãã¹ãååŸããŸããä»®æ³ç°å¢ã䜿ã£ãŠããå Žåãªã©ãã©ã®Pythonå®è¡ãã¡ã€ã«ã䜿ãããŠãããã確èªããã®ã«åœ¹ç«ã¡ãŸãã
import sys
print(f"Pythonã€ã³ã¿ããªã¿ã®ãã¹: {sys.executable}")
ç°å¢ã«ãã£ãŠã¯ïŒäŸãã°Pythonãå®è¡ãã¡ã€ã«ã®å®éã®ãã¹ãååŸã§ããªãå ŽåïŒã空æååã `None` ãè¿ãããããšããããŸãã
6. `sys.modules`: ããŒãæžã¿ã¢ãžã¥ãŒã«ã®èŸæž
Pythonã€ã³ã¿ããªã¿ãçŸåšããŒãããŠããïŒã€ã³ããŒãæžã¿ã®ïŒã¢ãžã¥ãŒã«ããã¢ãžã¥ãŒã«åãããŒãã¢ãžã¥ãŒã«ãªããžã§ã¯ããå€ãšããèŸæžãšããŠä¿æããŠããŸãããããã°æãªã©ã«ãã©ã®ã¢ãžã¥ãŒã«ãèªã¿èŸŒãŸããŠãããã確èªããã®ã«äœ¿ããŸãã
import sys
import math # mathã¢ãžã¥ãŒã«ãã€ã³ããŒã
import pprint
print("ããŒãæžã¿ã¢ãžã¥ãŒã«ã®äžéš:")
# å
šéšè¡šç€ºãããšéåžžã«å€ããªãã®ã§ã'math' ãå«ãŸããŠããã確èªããäŸ
if 'math' in sys.modules:
print("'math' ã¢ãžã¥ãŒã«ã¯ããŒããããŠããŸãã")
# pprint.pprint(sys.modules) # å
šãŠè¡šç€ºãããå Žåã¯ã³ã¡ã³ãã¢ãŠããå€ã
else:
print("'math' ã¢ãžã¥ãŒã«ã¯ãŸã ããŒããããŠããŸããã")
print(f"\nããŒãæžã¿ã¢ãžã¥ãŒã«ã®ç·æ°: {len(sys.modules)}")
`sys`ã¢ãžã¥ãŒã«ã®äŸ¿å©ãªé¢æ°ãã¡
`sys`ã¢ãžã¥ãŒã«ã¯æ å ±ãæäŸããå±æ§ã ãã§ãªããã€ã³ã¿ããªã¿ã®åäœã«åœ±é¿ãäžããé¢æ°ãæäŸããŠããŸãã
1. `sys.exit([arg])`: ããã°ã©ã ã®çµäº
ã¹ã¯ãªããã®å®è¡ãéäžã§çµäºãããããšãã«äœ¿ããŸããéåžžãããã°ã©ã ã¯æåŸãŸã§å®è¡ããããããšã©ãŒãçºçãããšçµäºããŸããã`sys.exit()` ãåŒã³åºããšãã®æç¹ã§ããã°ã©ã ãåæ¢ããŸãã
åŒæ° `arg` ãæå®ããããšã§ãçµäºã¹ããŒã¿ã¹ïŒéåžžã0ã¯æ£åžžçµäºããã以å€ã¯ãšã©ãŒçµäºã瀺ãæŽæ°ïŒãOSã«è¿ãããšãã§ããŸãããŸããæååãæž¡ããšããããæšæºãšã©ãŒåºå (`sys.stderr`) ã«åºåãããŠããçµäºããŸãã
import sys
print("åŠçãéå§ããŸãã")
user_input = input("ç¶è¡ããŸããïŒ (yes/no): ")
if user_input.lower() != 'yes':
print("ãŠãŒã¶ãŒãåŠçã®äžæãéžæããŸããã")
sys.exit("ããã°ã©ã ãçµäºããŸãã") # ã¡ãã»ãŒãžãåºåããŠçµäº (çµäºã¹ããŒã¿ã¹ 1)
# sys.exit(0) # æ£åžžçµäºãšããŠã¹ããŒã¿ã¹ 0 ã§çµäºããå Žå
# sys.exit(5) # ç¹å®ã®ãšã©ãŒã¹ããŒã¿ã¹ 5 ã§çµäºããå Žå
print("ãã®ã¡ãã»ãŒãžã¯è¡šç€ºãããŸããã")
2. `sys.stdin`, `sys.stdout`, `sys.stderr`: æšæºã¹ããªãŒã
ãããã¯ãããããæšæºå ¥åãæšæºåºåãæšæºãšã©ãŒåºåãè¡šããã¡ã€ã«ã©ã€ã¯ãªããžã§ã¯ãã§ãã
- `sys.stdin`: éåžžã¯ããŒããŒãããã®å ¥åãåãä»ããŸãã`input()` é¢æ°ãå éšçã«ã¯ãããå©çšããŠããŸãã
- `sys.stdout`: éåžžã¯ã¿ãŒããã«ïŒã³ã³ãœãŒã«ç»é¢ïŒãžã®åºåã«äœ¿ãããŸãã`print()` é¢æ°ã®ããã©ã«ãã®åºåå ã§ãã
- `sys.stderr`: ãšã©ãŒã¡ãã»ãŒãžã®åºåã«äœ¿ãããŸããé垞㯠`sys.stdout` ãšåããã¿ãŒããã«ã«åºåãããŸããããªãã€ã¬ã¯ãïŒåºåå å€æŽïŒã«ãã£ãŠå¥ã ã®ãã¡ã€ã«ã«æ¯ãåããããšãã§ããŸãã
ãããã®ã¹ããªãŒã ã¯ããã¡ã€ã«ã®ããã« `read()` ã `write()` ã¡ãœãããæã€ããããªãã€ã¬ã¯ãã®ã·ãã¥ã¬ãŒã·ã§ã³ããåºåå ããã¡ã€ã«ã«å€æŽãããªã©ã®æäœãå¯èœã§ãã
import sys
# æšæºãšã©ãŒåºåã«ã¡ãã»ãŒãžãæžã蟌ã
sys.stderr.write("ããã¯ãšã©ãŒã¡ãã»ãŒãžã§ãã\n")
# æšæºåºåããã¡ã€ã«ã«ãªãã€ã¬ã¯ãããäŸïŒäžæçïŒ
original_stdout = sys.stdout # å
ã®æšæºåºåãä¿å
try:
with open('output.log', 'w', encoding='utf-8') as f:
sys.stdout = f # æšæºåºåããã¡ã€ã«ã«å€æŽ
print("ãã®ã¡ãã»ãŒãžã¯ output.log ã«æžã蟌ãŸããŸãã")
print("ããããã¡ã€ã«ã«æžã蟌ãŸããŸãã")
finally:
sys.stdout = original_stdout # æšæºåºåãå
ã«æ»ã
print("ãã®ã¡ãã»ãŒãžã¯ã³ã³ãœãŒã«ã«è¡šç€ºãããŸãã")
# output.log ã®äžèº«ã確èª
try:
with open('output.log', 'r', encoding='utf-8') as f:
print("\n--- output.log ã®å
容 ---")
print(f.read())
print("--- ãããŸã§ ---")
except FileNotFoundError:
print("\noutput.log ãèŠã€ãããŸããã")
3. `sys.getrecursionlimit()` / `sys.setrecursionlimit(limit)`: å垰深床ã®å¶é
Pythonã§ã¯ãé¢æ°ãèªåèªèº«ãåŒã³åºããååž°ãã®æ·±ãã«ã¯äžéãèšããããŠããŸããããã¯ãç¡éã«ãŒãã®ãããªååž°ã«ãã£ãŠã¹ã¿ãã¯ãªãŒããŒãããŒãçºçããããã°ã©ã ãã¯ã©ãã·ã¥ããã®ãé²ãããã§ãã
- `sys.getrecursionlimit()`: çŸåšã®å垰深床ã®äžéå€ãååŸããŸãã
- `sys.setrecursionlimit(limit)`: å垰深床ã®äžéå€ãæ°ãã `limit` ã«èšå®ããŸãã
import sys
current_limit = sys.getrecursionlimit()
print(f"çŸåšã®å垰深床ã®äžé: {current_limit}")
# äžéãå°ãäžããŠã¿ãïŒæ³šæããŠäœ¿çšããããšïŒïŒ
try:
new_limit = current_limit + 500
sys.setrecursionlimit(new_limit)
print(f"å垰深床ã®äžéã {new_limit} ã«å€æŽããŸããã")
print(f"å€æŽåŸã®äžé: {sys.getrecursionlimit()}")
# æ·±ãååž°ãè¡ãé¢æ°ã®äŸïŒäžéãè¶
ãããš RecursionErrorïŒ
def deep_recursion(n):
# print(n) # ãããã°çš
if n <= 0:
return
try:
deep_recursion(n - 1)
except RecursionError:
print(f"Error: å垰深床ã®äžé ({sys.getrecursionlimit()}) ã«éããŸããã")
sys.exit(1) # ãšã©ãŒãšããŠçµäº
print("\næ·±ãååž°é¢æ°ãåŒã³åºããŸã...")
deep_recursion(new_limit - 10) # äžéè¿ããŸã§ååž°
print("ååž°é¢æ°ãç¡äºçµäºããŸããã")
except ValueError as e:
print(f"Error: å垰深床ã®èšå®ã«å€±æããŸãã - {e}")
finally:
# å
ã®å¶éå€ã«æ»ãã®ãæãŸããå Žåããã
# sys.setrecursionlimit(current_limit)
pass
`sys`ã¢ãžã¥ãŒã«ã®æŽ»çšäŸ ð¡
ãããŸã§èŠãŠããå±æ§ãé¢æ°ã䜿ã£ãŠãå ·äœçã«ã©ã®ãããªããšãã§ããã§ããããïŒããã€ãã®ãŠãŒã¹ã±ãŒã¹ã玹ä»ããŸãã
- ã³ãã³ãã©ã€ã³ããŒã«ã®äœæ: `sys.argv` ã䜿ã£ãŠããŠãŒã¶ãŒãæå®ãããã¡ã€ã«ãã¹ããªãã·ã§ã³ã«åºã¥ããŠåäœããããŒã«ãäœæã§ããŸãã
- ç°å¢ããšã®èšå®åãæ¿ã: `sys.platform` ã§OSãå€å¥ããWindowsçšãmacOSçšãLinuxçšã§ç°ãªãèšå®ãã¡ã€ã«ãã©ã€ãã©ãªãèªã¿èŸŒãããã«å¶åŸ¡ã§ããŸãã
- ç°¡æãã®ã³ã°: `sys.stdout` ã `sys.stderr` ããã¡ã€ã«ã«ãªãã€ã¬ã¯ãããããšã§ãããã°ã©ã ã®åºåããšã©ãŒã¡ãã»ãŒãžãç°¡åã«ãã°ãã¡ã€ã«ã«èšé²ã§ããŸãã
- åçãªã¢ãžã¥ãŒã«èªã¿èŸŒã¿: `sys.path` ãæäœããŠãç¹å®ã®æ¡ä»¶äžã§ã®ã¿å¿ èŠãšãªãã«ã¹ã¿ã ã¢ãžã¥ãŒã«ãã€ã³ããŒããã¹ã«è¿œå ããå©çšå¯èœã«ããããšãã§ããŸãã
- ããã°ã©ã ã®çµäºå¶åŸ¡: ç¹å®ã®æ¡ä»¶ïŒãšã©ãŒçºçãå¿ é ãã¡ã€ã«ã®äžè¶³ãªã©ïŒãæ€ç¥ããå Žåã«ã`sys.exit()` ã䜿ã£ãŠé©åãªçµäºã¹ããŒã¿ã¹ãšå ±ã«ããã°ã©ã ãå®å šã«åæ¢ãããããšãã§ããŸãã
- ãããã°æ å ±ã®è¡šç€º: `sys.version`, `sys.executable`, `sys.path` ãªã©ã®æ å ±ããããã°ãã°ã«åºåããããšã§ãããã°ã©ã ãã©ã®ãããªç°å¢ã§å®è¡ãããŠããããææ¡ãããããªããŸãã
ãŸãšã
ä»åã¯ãPythonã®æšæºã©ã€ãã©ãªã®äžã§ãç¹ã«éèŠãª `sys` ã¢ãžã¥ãŒã«ã«ã€ããŠããã®äž»èŠãªå±æ§ãšé¢æ°ããããŠæŽ»çšäŸã詳ããèŠãŠããŸããã
`sys` ã¢ãžã¥ãŒã«ã¯ãPythonããã°ã©ã ãåäœãããå€åŽã®äžçããã€ãŸãã€ã³ã¿ããªã¿èªèº«ãå®è¡ç°å¢ãšå¯Ÿè©±ããããã®åºæ¬çãªã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããŠãããŸããã³ãã³ãã©ã€ã³åŒæ°ã®ååŸ (`sys.argv`)ãã¢ãžã¥ãŒã«æ€çŽ¢ãã¹ã®ç®¡ç (`sys.path`)ããã©ãããã©ãŒã æ å ±ã®ååŸ (`sys.platform`)ãããã°ã©ã ã®çµäºå¶åŸ¡ (`sys.exit`) ãªã©ããã®æ©èœã¯å€å²ã«ããããŸãã
æ®æ®µããŸãæèããããšã¯ãªããããããŸãããã`sys` ã¢ãžã¥ãŒã«ãç解ã䜿ãããªãããšã§ãããæè»ã§ãç°å¢ã«é©å¿ãããããå ç¢ãªPythonããã°ã©ã ãäœæã§ããããã«ãªããŸãããã²ãããªãã®ãããžã§ã¯ãã§ã `sys` ã¢ãžã¥ãŒã«ã掻çšããŠã¿ãŠãã ãããïŒð
ã³ã¡ã³ã