Tuesday_June_30
.
.
.

4๏ธโƒฃ Calculator ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ


์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

test_python ์ด๋ผ๋Š” ์ตœ์ƒ์œ„ ํด๋” ์•ˆ์— calculator๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ main.py ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ 
calculator ๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” (์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)ini(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py, add_and_multiply.py, multiplication.py ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

๊ฐ ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)init(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py ํŒŒ์ผ >> ๋‚ด์šฉ์€ ์—†๋‹ค.

add_and_multiply.py

from .multiplication import multiply
# from calculator.multiplication import multiply
def add_and_multiply(a,b):
    return multiply(a,b) + (a+b)

multiplication.py

def multiply(a,b):
    return(a*b)

main.py

# absoulte path
from calculator.add_and_multiply import add_and_multiply 

# relative path
# from .calculator.add_and_multiply import add_and_multiply

if __name__ == '__main__':
    print(add_and_multiply(1,2))

5๏ธโƒฃ main.py์—์„œ import ๊ฒฝ๋กœ์— ๋”ฐ๋ฅธ ์—๋Ÿฌ

main ํŒŒ์ผ์—์„œ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ add_and_multiply๋ฅผ import ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ์ƒ๋Œ€๊ฒฝ๋กœ์ธ
from .calculator.add_and_multiply import add_and_multiply
๋กœ import๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด,

# absoulte path
# from calculator.add_and_multiply import add_and_multiply 

# relative path
from .calculator.add_and_multiply import add_and_multiply

if __name__ == '__main__':
    print(add_and_multiply(1,2))

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

ImportError: attempted relative import with no known parent package

ํŒŒ์ด์ฌ์€ ์‹คํ–‰ํ•˜๋Š”(main)ํŒŒ์ผ์€ ๋ฌด์กฐ๊ฑด x 100 ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.
๊ณต์‹๋ฌธ์„œ์— ๊ทธ๋ ‡๊ฒŒ ๋‚˜์™€์žˆ๋‹ค. ์ดํ•ดํ•˜๋ ค๋‹ค ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์งˆ๋ป”ํ–ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€, ์ด๋ฆ„์ด main์ธ ํ•จ์ˆ˜์—์„œ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ import๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ,
์‹คํ–‰์ค‘์ธ ํŒŒ์ผ์„ main ํŒŒ์ผ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

6๏ธโƒฃ add_and_multiply.py์—์„œ multiple ํ•จ์ˆ˜๋ฅผ ๊ฒฝ๋กœ ๋ณ„๋กœ importํ•˜๊ณ  main ๋ชจ๋“ˆ๊ณผ์˜ ์ฐจ์ด์  ์•Œ์•„๋ณด๊ธฐ

add_and_multiply.py ํŒŒ์ผ์€ ์‹คํ–‰์ค‘์ธ ํŒŒ์ผ(main)์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋– ํ•œ ๊ฒฝ๋กœ๋กœ muriplication ๋ชจ๋“ˆ์˜ multiple ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€๋„ ์ƒ๊ด€์ด ์—†๋‹ค.
์—๋Ÿฌ ๋˜ํ•œ ๋‚˜์ง€ ์•Š๋Š”๋‹ค.

์ฐจ์ด์ ์ด๋ผ๊ธฐ ๋ณด๋‹จ, import์‹œ ์ ˆ๋Œ€ ๊ฒฝ๋กœ์™€ ์ƒ๋Œ€ ๊ฒฝ๋กœ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์‹คํ–‰ํ•˜๋Š” ํŒŒ์ผ(main)์—์„œ๋Š” ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ๋งŒ import๋ฅผ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

7๏ธโƒฃ (์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)init(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py ํŒŒ์ผ?

(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)init(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py ํŒŒ์ผ์€ ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€์˜ ์ผ๋ถ€๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋งŒ์•ฝ calculatorํŒจํ‚ค์ง€์— (์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)init(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด, calculator๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ํŒจํ‚ค์ง€๋ผ๊ณ  ์ธ์‹์„ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค.

**๊ทธ๋Ÿฐ๋ฐ python3.3 ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” (์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)init(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py ํŒŒ์ผ์ด ์—†์–ด๋„ ํŒจํ‚ค์ง€๋กœ ์ธ์‹ ํ•˜์ง€๋งŒ(PEP 420) ํ•˜์œ„ ๋ฒ„์ „ ํ˜ธํ™˜์„ ์œ„ํ•ด (์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”)init(์–ธ๋”๋ฐ”์–ธ๋”๋ฐ”).py ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•œ๋‹ค.

profile
๐Ÿ‡ Rabbit can take a rest, but ๐Ÿข turtle can't

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