16. ë¶ë¡Â¶
16.1. ëíí 모ë¶
There are two variants of the interactive REPL. The classic basic interpreter is supported on all platforms with minimal line control capabilities.
Since Python 3.13, a new interactive shell is used by default.
This one supports color, multiline editing, history browsing, and
paste mode. To disable color, see Controlling color for
details. Function keys provide some additional functionality.
F1 enters the interactive help browser pydoc.
F2 allows for browsing command-line history with neither output nor the
>>> and ⦠prompts. F3 enters âpaste modeâ, which
makes pasting larger blocks of code easier. Press F3 to return to
the regular prompt.
When using the new interactive shell, exit the shell by typing exit or quit. Adding call parentheses after those commands is not required.
If the new interactive shell is not desired, it can be disabled via
the PYTHON_BASIC_REPL environment variable.
16.1.1. ìë¬ ì²ë¦¬Â¶
ìë¬ê° ë°ìíë©´ ì¸í°í리í°ë ìë¬ ë©ìì§ì ì¤í í¸ë ì´ì¤ë¥¼ ì¸ìí©ëë¤. ëíí 모ëììë 기본 í롬íí¸ë¡ ëìê°ëë¤; íì¼ë¡ë¶í° ì
ë ¥ì´ ìì ëë, ì¤í í¸ë ì´ì¤ë¥¼ ì¸ìí í 0ì´ ìë ì¢
ë£ ìíë¡ ì¢
ë£í©ëë¤. (try 문ìì except ì ì ìí´ ì²ë¦¬ëë ìì¸ë ì´ ë¬¸ë§¥ìì ìë¬ê° ìëëë¤.) ì¼ë¶ ìë¬ë 무조건 ì¹ëª
ì ì´ë©° 0ì´ ìë ì¢
ë£ ìíì ì¢
ë£ë¥¼ ì ë°í©ëë¤; ì´ê²ì ë´ë¶ ë¶ì¼ì¹ì ë©ëª¨ë¦¬ ë¶ì¡±ì¼ë¡ ì¸í ê²½ì°ì ì ì©ë©ëë¤. 모ë ìë¬ ë©ìì§ë íì¤ ìë¬ ì¤í¸ë¦¼ì 기ë¡ë©ëë¤. ì¤íë ëª
ë ¹ì ì ì ì¶ë ¥ì íì¤ ì¶ë ¥ì 기ë¡ë©ëë¤.
기본 ëë ë³´ì¡° í롬íí¸ì ì¸í°ë½í¸ 문ì (ì¼ë°ì ì¼ë¡ Control-C ëë Delete)를 ì
ë ¥íë©´ ì
ë ¥ì ì·¨ìíê³ ê¸°ë³¸ í롬íí¸ë¡ ëìê°ëë¤. [1] ëª
ë ¹ì´ê° ì¤íëë ëì ì¸í°ë½í¸ë¥¼ ì
ë ¥íë©´ try 문ì ìí´ ì²ë¦¬ë ì ìë KeyboardInterrupt ìì¸ê° ë°ìí©ëë¤.
16.1.2. ì¤í ê°ë¥í íì´ì¬ ì¤í¬ë¦½í¸Â¶
BSD ì¤íì¼ì ì ëì¤ ìì¤í ìì íì´ì¬ ì¤í¬ë¦½í¸ë ì ¸ ì¤í¬ë¦½í¸ì²ë¼ ì§ì ì¤íí ì ìê² ë§ë¤ ì ììµëë¤. ë¤ìê³¼ ê°ì ì¤
#!/usr/bin/env python3
(ì¸í°í리í°ê° ì¬ì©ìì PATH ì ìë¤ê³ ê°ì í ë)ì ì¤í¬ë¦½í¸ì ìì ë¶ë¶ì ë£ê³ íì¼ì ì¤í ê°ë¥ 모ë를 ì¤ëë¤. #! ë ë°ëì íì¼ì ì²ì ë 문ìì¬ì¼ í©ëë¤. ì¼ë¶ íë«í¼ììë ì´ ì²« ë²ì§¸ ì¤ì´ ì ëì¤ ì¤íì¼ì ì¤ ì¢
ë£ ('\n')ë¡ ëëì¼ íë©°, ìëì° ì¤ ì¢
ë£('\r\n')ë íë½ëì§ ììµëë¤. íì´ì¬ìì í´ì, ëë íì´ë, 문ì '#' ë 주ìì ììíë ë° ì¬ì©ë©ëë¤.
ì¤í¬ë¦½í¸ë chmod ëª ë ¹ì ì¬ì©íì¬ ì¤í ê°ë¥í 모ë, ëë ê¶í, ì ë¶ì¬ë°ì ì ììµëë¤.
$ chmod +x myscript.py
ìëì° ìì¤í
ììë âì¤í ê°ë¥ 모ëâë¼ë ê°ë
ì´ ììµëë¤. íì´ì¬ ì¤ì¹ íë¡ê·¸ë¨ì .py íì¼ì python.exeì ìëì¼ë¡ ì°ê²°íì¬, íì´ì¬ íì¼ì ì´ì¤ í´ë¦íë©´ ì¤í¬ë¦½í¸ë¡ ì¤íí©ëë¤. íì¥ìë .pyw ì¼ ìë ììµëë¤. ì´ ê²½ì°, ì¼ë°ì ì¼ë¡ ëíëë ì½ì ì°½ì íìëì§ ììµëë¤.
16.1.3. ëíí ìì íì¼Â¶
íì´ì¬ì ëííì¼ë¡ ì¬ì©í ë, ì¢
ì¢
ì¸í°í리í°ê° ììë ëë§ë¤ ì¤íëë íì¤ ëª
ë ¹ë¤ì´ ìì¼ë©´ í¸ë¦¬í©ëë¤. PYTHONSTARTUP íê²½ ë³ì를 ìì ëª
ë ¹ì´ ë¤ì´ìë íì¼ ì´ë¦ì¼ë¡ ì¤ì íë©´ ë©ëë¤. ì´ê²ì ì ëì¤ ì
¸ì .profile 기ë¥ê³¼ ì ì¬í©ëë¤.
ì´ íì¼ì ëíí ì¸ì
ììë§ ì½íë©°, íì´ì¬ì´ ì¤í¬ë¦½í¸ìì ëª
ë ¹ì ì½ì ëë, /dev/tty ê° ëª
ë ¹ì ëª
ìì ìì¤ì¸ ê²½ì°(ëíí ì¸ì
ì²ë¼ ëìíë¤)ìë ì½ì§ ììµëë¤. ëíí ëª
ë ¹ì´ ì¤íëë ê°ì ì´ë¦ ê³µê°ìì ì¤íëë¯ë¡, ì´ íì¼ìì ì ìíê±°ë ìí¬í¸íë ê°ì²´ë¤ì ëíí ì¸ì
ìì ì ê·ííì§ ìì ì´ë¦ì¼ë¡ ì¬ì©í ì ììµëë¤. ì´ íì¼ìì sys.ps1 ë° sys.ps2 í롬íí¸ë¥¼ ë³ê²½í ìë ììµëë¤.
íì¬ ëë í°ë¦¬ìì ì¶ê° ìì íì¼ì ì½ì¼ë ¤ë©´, ì ì ìì íì¼ìì if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read()) ì ê°ì ì½ë를 ì¬ì©í´ì íë¡ê·¸ë¨í ì ììµëë¤. ì¤í¬ë¦½í¸ìì ìì íì¼ì ì¬ì©íë ¤ë©´ ì¤í¬ë¦½í¸ìì ëª
ìì ì¼ë¡ ìíí´ì¼ í©ëë¤:
import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
with open(filename) as fobj:
startup_file = fobj.read()
exec(startup_file)
16.1.4. 커ì¤í°ë§ì´ì ì´ì 모ë¶
íì´ì¬ì 커ì¤í°ë§ì´ì¦í ì ìë ë ê°ì§ í ì ì ê³µí©ëë¤: sitecustomize ì usercustomize. ì´ë»ê² ìëíëì§ ë³´ë ¤ë©´, 먼ì ì¬ë¬ë¶ì ì¬ì©ì site-packages ëë í°ë¦¬ì ìì¹ë¥¼ ì°¾ìì¼ í©ëë¤. íì´ì¬ì ììíê³ ë¤ì ì½ë를 ì¤íí©ëë¤:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.x/site-packages'
ì´ì ê·¸ ëë í°ë¦¬ì usercustomize.py ë¼ë ì´ë¦ì íì¼ì ë§ë¤ê³ ìíë ê²ë¤ì ë£ì ì ììµëë¤. ìë ìí¬í¸ë¥¼ ë¹íì±ííë -s ìµì
ì¼ë¡ ììíì§ ìë í, ì´ íì¼ì 모ë íì´ì¬ ì¤íì ìí¥ì ì¤ëë¤.
sitecustomize ë ê°ì ë°©ìì¼ë¡ ìëíì§ë§, ì¼ë°ì ì¼ë¡ ì ì site-packages ëë í°ë¦¬ì ì»´í¨í° ê´ë¦¬ìê° ë§ë¤ê³ , usercustomize ì ì ìí¬í¸ë©ëë¤. ìì¸í ë´ì©ì site 모ëì ì¤ëª
ì를 ë³´ì¸ì.
ê°ì£¼