
ํ์ด์ฌ์ผ๋ก ํ๋ก๊ทธ๋จ์ ์งค ๋, ๋ค๋ฅธ ๋ชจ๋์ ํด๋์ค๋ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๋๊ฐ ์์ ๊ฒ์ด๋ค.
ํ์ ์ญ์ ๊ทธ๋ฌํ๊ณ , ๊ทธ๋ ๊ฒ ํ๋ก๊ทธ๋จ์ ์ ์ํ๊ณ ์์๋ค.
๊ทธ๋ฌ๋ ํ๋ก์ ํธ์ ๊ตฌ์กฐ๋ฅผ ๋ฐ๊พธ๊ณ ๋๋, ์ ๋๋ก ๋์ํ์ง ์๋ ๊ฒฝ์ฐ๊ฐ ์์๋ค. ์๋ ๊ฒฝ์ฐ๋ฅผ ์ดํด๋ณด์.
๐ซ. ๋ฌธ์ ์ํฉ

์ฌ๊ธฐ์ file1.py๊ฐ file2.py ๋ชจ๋์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ด๋ป๊ฒ ํด์ผํ ๊น?
์ฌ๊ธฐ์ ๋ํ ํด๋ต์ ๋ง์ ๊ณณ์์ ๋์์๋ค. ์๋์ ๊ฐ์ด ํด๊ฒฐํ๋ฉด ๋๋ค.
# file2.py
class Class2:
def __init__(self):
print('This is class in Class2')
# file1.py
# ์์ ํด๋๋ฅผ ์ฐธ์กฐํ ์ ์๋๋ก ์ค์ ํ๋ค.
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from folder2 import file2
file2.Class2()

์์ ๊ฐ์ด ์คํํ๋ฉด ๋ชจ๋์ด ์ ๋๋ก ๋ถ๋ฌ์ ธ Class๊ฐ ์ด๊ธฐํ๋๋ฉฐ ์ฌ๋ฐ๋ฅด๊ฒ ์คํ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์๋ ์ํฉ์ ์ด๋จ๊น? ๐
# file3.py
class Class3:
def __init__(self):
print('This is class in Class3')
# file2.py
# ๋์ผํ ํด๋์ด๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฅ import ํด์ฃผ๋ฉด ๋๋ค.
import file3
class Class2:
def __init__(self):
print('This is class in Class2')
file3.Class3()
์ด ๊ฒฝ์ฐ, file1.py์์ file2๋ฅผ ๋ถ๋ฌ์จ๋ค๋ฉด ์ ๋๋ก ๋ถ๋ฌ์์ง๊ฒ ์ง?? ๋ผ๊ณ ์๊ฐํ๋ค.
ํ์ง๋ง...

์...? file2์์ ์๋ง ๋ถ๋ฌ์์ง๋ file3๋ฅผ ๋ถ๋ฌ์ฌ ์ ์๋ค๋.....
๐ค. ๊ณ ๋ฏผ๊ณผ ํด๊ฒฐ
์ธํฐ๋ท์ ์ฐพ์๋ดค๋๋ฐ, ๋์ค์ง๊ฐ ์์๋ค. ์ ์ด์ ์ด ์ํฉ์ ๋ญ๋ผ ์ค๋ช ํ ์ง๋ ์ ๋งคํ๋ค.. ๐
๊ทธ๋์ ๊ณ ๋ฏผ์ ํด๋ดค๋ค. ์์ ํด๋๋ฅผ ์ฐธ์กฐํ ์ ์๋๋ก ์ค์ ํ๋ ์ด ํจ์๋ ๋ฌด์์ผ๊น...
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
import sys, os
print(os.path.dirname(__file__))
print(os.path.abspath(os.path.dirname(__file__)))
print(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))

์ด๋ ๊ฒ ๋ณด๋, ํ์ฌ ํ์ผ์ ํด๋์ ์์ ํด๋๋ฅผ ์ฐพ๋ ๊ฒ์ผ ๋ฟ์ด์๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ์์ํด๋๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด์๋ค.
๐ก ๊ทธ๋ผ ์ด ๊ณผ์ ์ file2์ ์งํํ๋ฉด ์ ์ฐธ์กฐํ ์ ์์ง๋์ง ์์๊น?
๋ฌผ๋ก ์ด๋ฒ์ ์์ ํด๋๊ฐ ์๋, ํ์ฌ ํด๋๋ก ํ๋ ๊ฒ์ด๋ค.
# file2.py
# ํ์ฌ ํด๋๋ฅผ ์ฐธ์กฐํ ์ ์๋๋ก ์ค์ ํ๋ค. (์ถ๊ฐ๋จ)
import sys, os
sys.path.append(os.path.dirname(__file__))
# ๋์ผํ ํด๋์ด๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฅ import ํด์ฃผ๋ฉด ๋๋ค.
import file3
class Class2:
def __init__(self):
print('This is class in Class2')
file3.Class3()

ํ๋ฅญํ๊ฒ ์คํ๋๋ค!