TIL26. Python : About Modules

ID์งฑ์žฌยท2021๋…„ 10์›” 8์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
33/39
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Python์˜ modules์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. module์€ ๊ฐ์ข… ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์„ ๋‹ด๊ณ  ์žˆ๋Š” .py ํŒŒ์ผ์ด๋ฉฐ, ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์ด ๊ทธ๋ฃนํ™”๋˜์–ด์žˆ๋Š” ๊ฒƒ์ด ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค.



๐ŸŒˆ About Modules

๐Ÿ”ฅ sys.modules ์™€ sys.path์˜ ์ฐจ์ด์ 

๐Ÿ”ฅ Absolute path์™€ Relative path์˜ ์ฐจ์ด

๐Ÿ”ฅ calculator ํŒจํ‚ค์ง€



1. sys.modules ์™€ sys.path์˜ ์ฐจ์ด์ 

๐Ÿค” module์„ ์–ด๋–ป๊ฒŒ ์ฐพ์•„์˜ฌ๊นŒ?

โœ”๏ธ ํ•ด๋‹น ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์–ด๋””์žˆ๋Š”์ง€ ๊ฒฝ๋กœ๋ฅผ ์•Œ๊ณ , ๊ทธ ๊ฒฝ๋กœ์—์„œ importํ•จ์œผ๋กœ์จ ๋ชจ๋“ˆ ๋˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ python์€ ๋‹ค์Œ 3๊ฐ€์ง€ ๋ฐฉ์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ณด๋ฉด์„œ ๋ชจ๋“ˆ ๋˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

1) sys.modules
2) built-in modules
3) sys.path

โœ”๏ธ sys.modules์€ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ณ , python์ด ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ์šฐ์„ ํ•ด์„œ ํ™•์ธํ•˜๋Š” ์˜์—ญ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ, sys.modules์€ ํ•œ๋ฒˆ import๋œ ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€๋Š” ๋‹ค์‹œ ์ฐพ์ง€ ์•Š๋„๋ก ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ built-in modules์€ pythob์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ณต์‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ python์— ๋‚ด์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ importํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(), len(), append() ์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋“ค์€ ๋ชจ๋‘ built-in module์— ์†ํ•ด ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ sys.path์€ ๋งˆ์ง€๋ง‰์œผ๋กœ ํƒ์ƒ‰๋˜๋Š” ์˜์—ญ์œผ๋กœ list์— ๋‹ด๊ฒจ ๊ฒฝ๋กœ๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. sys.path์— site-packages์—๋Š” pip๋กœ ์„ค์น˜๋œ ์™ธ๋ถ€ ๋ชจ๋“ˆ์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ sys.path๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import sys
print(sys.path)
"""
[
    '/Users/jangjaewon/Documents/C',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
    '/Users/jangjaewon/Documents/A/z_env/lib/python3.9/site-packages'
]
"""

โœ”๏ธ sys.path ์—์„œ๋„ ๋ชป์ฐพ์œผ๋ฉด "ModuleNotFoundError" ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— "ModuleNotFoundError"๋ฅผ ๋ณด๊ฒŒ๋˜๋ฉด ๋ชจ๋“ˆ ๋˜๋Š” ํŒจํ‚ค์ง€ ์ง€์ •์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๐Ÿค” sys.modules ์™€ sys.path์˜ ์ฐจ์ด

โœ”๏ธ sys.module์€ ํ•œ๋ฒˆ import๋œ ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅํ•ด๋‘” ๊ณณ์ด๊ธฐ ๋•Œ๋ฌธ์— "ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€"๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” key๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋น ๋ฅด๊ณ  ์กด์žฌํ•˜๋Š”์ง€ ์—†๋Š”์ง€ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— ๋ฐ˜ํ•ด sys.path๋Š” ๋ฆฌ์ŠคํŠธ์— ๊ฒฝ๋กœ๋กœ์จ ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€๊ฐ€ ๋‹ด๊ฒจ์žˆ๊ณ , ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๊ฐ€ ์•„๋‹Œ ์–ด๋””(Where)์— ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ํƒ์ƒ‰ ํƒ์ƒ‰ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋”•์…”๋„ˆ๋ฆฌ ๋ณด๋‹ค ๋ฐฐ์—ด์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด์— built-in module์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ชจ๋“ˆ์˜ ์ฒซ import์—์„  sys.path๋ฅผ ์ผ์ผํžˆ ํ™•์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.



2. Absolute path์™€ Relative path์˜ ์ฐจ์ด

๐Ÿค” Absolute path์™€ Relative path๊ฐ€ ๋ฌด์—‡์ผ๊นŒ?

โœ”๏ธ Absolute path๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์ตœ์ƒ๋‹จ ๋””๋ ‰ํ† ๋ฆฌ(Base Directory) ์ดํ›„์˜ ๊ฒฝ๋กœ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ช…์‹œํ•จ์œผ๋กœ์จ ๋ชจ๋“ˆ ๋˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
โœ”๏ธ Absolute path์˜ ๋‹จ์ ์€ ์ตœ์ƒ๋‹จ ๋””๋ ‰ํ† ๋ฆฌ(Base Directory)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์žก๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฝ๋กœ๊ฐ€ ๋งค์šฐ ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ Relative path๋Š” ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•จ์— ์žˆ์–ด ๊ธฐ์ค€์ ์„ ์ตœ์ƒ๋‹จ ๋””๋ ‰ํ† ๋ฆฌ(Base Directory)๊ฐ€ ์•„๋‹Œ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ์œ„์น˜๋กœ ๋ถ€ํ„ฐ ์„ค์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Absolute path์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ Relative path ๋ฐฉ์‹ ๋˜ํ•œ ๋‹จ์ ์ด ์กด์žฌํ•˜๋Š”๋ฐ ์ƒ๋Œ€์ ์ธ ๊ฒฝ๋กœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€์˜ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ๊ฒฝ๋กœ ๋˜ํ•œ ๋ณ€๊ฒฝํ•ด์ค˜์•ผํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.



3. calculator ํŒจํ‚ค์ง€

+-- calculator
|        +-- __init__.py
|        +-- add_and_multiply.py
|        +-- multiplication.py
+-- main.py

๐Ÿค” main module์„ ์ง€์ •ํ•˜์˜€์„ ๋•Œ ํŒจํ‚ค์ง€์˜ ๋ชจ๋“ˆ์„ ์–ด๋–ป๊ฒŒ import ํ•ด์•ผ ํ• ๊นŒ?

โœ”๏ธ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์‹คํ–‰์‹œํ‚ค๋ฉด ๋ฌธ์ œ ์—†์ด ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from calculator.add_and_multiply import add_and_multiply 
if __name__ == '__main__':
    print(add_and_multiply(2,5)) # 17
"""

โœ”๏ธ ์ƒ๋Œ€๊ฒฝ๋กœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ "ImportError" ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ __main__์„ ์ด๋ฆ„์œผ๋กœ ๊ฐ–๋Š” main module์—์„œ ๋ชจ๋“ˆ์„ import ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

from .calculator.add_and_multiply import add_and_multiply
if __name__ == '__main__':
    print(add_and_multiply(2,5))
"""
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    from .calculator.add_and_multiply import add_and_multiply
ImportError: attempted relative import with no known parent package
"""

๐Ÿค” main module์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ํŒจํ‚ค์ง€์˜ ๋ชจ๋“ˆ์„ ์–ด๋–ป๊ฒŒ import ํ•ด์•ผ ํ• ๊นŒ?

โœ”๏ธ main module์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ์™€ ์ƒ๋Œ€๊ฒฝ๋กœ ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

# absolule path
from calculator.multiplication import multiply
def add_and_multiply(a,b):
    return multiply(a,b) + (a+b)
print(multiply(3,9)) # 27
# relative path
from .multiplication import multiply
def add_and_multiply(a,b):
    return multiply(a,b) + (a+b)
print(multiply(3,9)) # 27

โœ”๏ธ ์ฆ‰, main module(__main__)์ด ์ง€์ •๋œ ์ƒํƒœ์—์„œ๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ importํ•ด์•ผํ•˜๊ณ , main module์ด ์ง€์ •๋œ ์ƒํƒœ๊ฐ€ ์•„๋‹Œ๊ฒฝ์šฐ๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ๋“  ์ƒ๋Œ€๊ฒฝ๋กœ๋“  importํ•˜๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿค” init.py ์˜ ์—ญํ™œ

โœ”๏ธ ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€์˜ ์ผ๋ถ€์ž„์„ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค
โœ”๏ธ 3.3๋ถ€ํ„ฐ๋Š” init์ด ์—†์–ด๋„ ์ธ์‹ํ•œ๋‹ค๊ณ  ํ•˜์ง€๋งŒ, ์ด์ „ ๋ฒ„์ „์—์„œ init.py ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด ํŒจํ‚ค์ง€๋กœ ์ธ์‹๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€