λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Python

파이썬 λͺ¨λ“ˆκ³Ό νŒ¨ν‚€μ§€μ˜ λͺ¨λ“  것

by μ„œμ•„λž‘πŸ˜ 2023. 12. 9.

 

 

λͺ¨λ“ˆ

λͺ¨λ“ˆμ€ λ‹€λ₯Έ νŒŒμΌμ—μ„œ μ •μ˜ν•œ λ³€μˆ˜ 및 ν•¨μˆ˜λ“€μ„ λΆˆλŸ¬μ˜€λŠ” 것을 λœ»ν•©λ‹ˆλ‹€.

import mod

print(mod.add(3,4))

 

from λͺ¨λ“ˆ_이름 import λͺ¨λ“ˆ_ν•¨μˆ˜ ν˜•νƒœλ‘œ λͺ¨λ“ˆ 이름 없이 ν•¨μˆ˜ μ΄λ¦„μœΌλ‘œλ§Œ ν˜ΈμΆœν•  수 μžˆμŠ΅λ‹ˆλ‹€.

from mod import add
add(3,4)    # λͺ¨λ“ˆ 이름 없이 ν•¨μˆ˜ μ΄λ¦„μœΌλ‘œλ§Œ μ‚¬μš©

 

from λͺ¨λ“ˆ_이름 import * ν˜•νƒœλ‘œ ν•΄λ‹Ή λͺ¨λ“ˆ μ•ˆμ˜ λͺ¨λ“  ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ νŽΈν•˜λ‹€κ³  import *λ‘œλŠ” 쓰지 μ•ŠλŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.(항상 λͺ…μ‹œμ  호좜이 κ°€μž₯ 쒋은 ν˜•νƒœμž…λ‹ˆλ‹€)

from mod import *
add(3,4)    # import *λŠ” mod 파일의 λͺ¨λ“  ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 있음

 

if __name __ = “main”

# mod.py
def add(a, b): 
    return a+b

def sub(a, b): 
    return a-b

print(add(1, 4))
print(sub(4, 2))
# test.py
import mod

pass

5
2      # modλ₯Ό import만 ν•˜λ”λΌλ„ printκ°€ 싀행됨


mod λͺ¨λ“ˆμ„ import만 ν•˜λ”λΌλ„ printκ°€ μ‹€ν–‰λ˜κΈ° λ•Œλ¬Έμ— μ•„λž˜μ™€ 같이 μˆ˜μ •ν•©λ‹ˆλ‹€.

# mod.py
def add(a, b): 
    return a+b

def sub(a, b): 
    return a-b

if __name__ = "__main__":    # mod.pyλ₯Ό μ‹€ν–‰ν•  λ•Œλ§Œ if둜 λ“€μ–΄μ˜΄(importλŠ” μ•ˆλ“€μ–΄μ˜΄)
	print(add(1, 4))
	print(sub(4, 2))

 


λͺ¨λ“ˆ include path μΆ”κ°€ν•˜κΈ°

μ—¬νƒœκΉŒμ§€λŠ” νŠΉμ • λͺ¨λ“ˆμ„ μ‚¬μš©ν•˜λ €λ©΄ ν•΄λ‹Ή λ””λ ‰ν„°λ¦¬λ‘œ μ΄λ™ν•œ 후에야 μ‚¬μš©ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€. λͺ¨λ“ˆμ„ μ €μž₯ν•œ λ””λ ‰ν„°λ¦¬λ‘œ μ΄λ™ν•˜μ§€ μ•Šκ³  λΆˆλŸ¬μ™€μ„œ μ‚¬μš©ν•˜λŠ” 방법을 μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.

  1. sys.path.append μ‚¬μš©
import sys
sys.path

sys.path둜 파이썬 λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ„€μΉ˜λ˜μ–΄ μžˆλŠ” 디렉터리λ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

여기에 ν˜„μž¬ 디렉터리λ₯Ό μΆ”κ°€ν•˜λ©΄ μ–΄λ””μ„œλ“  ν˜„μž¬ λ””λ ‰ν„°λ¦¬μ˜ λͺ¨λ“ˆμ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

sys.path.append("/home/groundp/my-module")


1. PYTHONPATH ν™˜κ²½ λ³€μˆ˜ μ‚¬μš©

set PYTHONPATH="/home/groundp/my-module"    # windows
export PYTHONPATH="/home/groundp/my-module"   # Unix, Mac
import mod
print(mod.add(3,4))

sysλ³΄λ‹€λŠ” PYTHONPATHλ₯Ό μ“°λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.


 

νŒ¨ν‚€μ§€

  • νŒŒμ΄μ¬μ—μ„œ λͺ¨λ“ˆμ€ ν•˜λ‚˜μ˜ νŒŒμΌμž…λ‹ˆλ‹€.
  • νŒŒμ΄μ¬μ—μ„œ νŒ¨ν‚€μ§€λŠ” λͺ¨λ“ˆμ„ λͺ¨μ•„놓은 디렉토리 κ΅¬μ‘°μž…λ‹ˆλ‹€.
    • __init __.py 파일이 μžˆμ–΄μ•Ό νŒ¨ν‚€μ§€λ‘œ μΈμ‹ν•©λ‹ˆλ‹€.
  • νŒ¨ν‚€μ§€μ—μ„œ μ–΄λ–€ λͺ¨λ“ˆμ„ 외뢀에 λ…ΈμΆœμ‹œν‚¬μ§€ κ²°μ •ν•  λ•Œ, 각 λͺ¨λ“ˆμ—μ„œ all λ³€μˆ˜λ₯Ό μ„€μ •ν•˜μ—¬ λͺ…μ‹œμ μœΌλ‘œ 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.
# __init__.py
__all__ = ['module1', 'module2']

νŒŒμ΄μ¬μ—μ„œ νŒ¨ν‚€μ§€λŠ” λͺ¨λ“ˆμ„ ν¬ν•¨ν•˜λŠ” λ””λ ‰ν† λ¦¬λ‘œ κ΅¬μ„±λœ κ΅¬μ‘°μž…λ‹ˆλ‹€. νŒ¨ν‚€μ§€λŠ” μ½”λ“œμ˜ λͺ¨λ“ˆμ„ μ‘°μ§ν™”ν•˜κ³  이름 μΆ©λŒμ„ λ°©μ§€ν•˜λ©°, μ½”λ“œλ₯Ό λ…Όλ¦¬μ μœΌλ‘œ κ·Έλ£Ήν™”ν•˜μ—¬ μœ μ§€λ³΄μˆ˜μ„±μ„ ν–₯μƒμ‹œν‚€λŠ”λ° μ‚¬μš©λ©λ‹ˆλ‹€. κ°„λ‹¨ν•œ 예제둜 μ„€λͺ…ν•˜κ² μŠ΅λ‹ˆλ‹€.

 

νŒ¨ν‚€μ§€μ˜ ꡬ쑰

νŒ¨ν‚€μ§€ 디렉토리 생성:

# module1.py
def func1():
    print("Function 1 from module1")

# module2.py
def func2():
    print("Function 2 from module2")


각 λͺ¨λ“ˆμ˜ λ‚΄μš©:

mypackage/ 
β”œβ”€β”€ __init__.py 
β”œβ”€β”€ module1.py 
└── module2.py


νŒ¨ν‚€μ§€λ₯Ό μ‚¬μš©ν•˜λŠ” 슀크립트:

# main_script.py
from mypackage import module1, module2

module1.func1()
module2.func2()

 

 

νŒ¨ν‚€μ§€λŠ” μ™œ μ‚¬μš©ν• κΉŒ?

νŒ¨ν‚€μ§€λ₯Ό ν†΅ν•œ λͺ¨λ“ˆ κ·Έλ£Ήν™”: νŒ¨ν‚€μ§€λ₯Ό μ‚¬μš©ν•˜λ©΄ κ΄€λ ¨λœ λͺ¨λ“ˆμ„ λ…Όλ¦¬μ μœΌλ‘œ κ·Έλ£Ήν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ μ½”λ“œμ˜ ꡬ쑰가 ν–₯μƒλ˜μ–΄ μ΄ν•΄ν•˜κΈ° μ‰¬μ›Œμ§‘λ‹ˆλ‹€.

λ„€μž„μŠ€νŽ˜μ΄μŠ€ 뢄리: νŒ¨ν‚€μ§€λŠ” λ„€μž„μŠ€νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•˜μ—¬ λͺ¨λ“ˆ κ°„μ˜ 이름 μΆ©λŒμ„ λ°©μ§€ν•©λ‹ˆλ‹€. λͺ¨λ“ˆ 이름이 κ²ΉμΉ˜λ”λΌλ„ νŒ¨ν‚€μ§€λ₯Ό μ‚¬μš©ν•˜λ©΄ 각 νŒ¨ν‚€μ§€ λ‚΄μ—μ„œλŠ” 이름 좩돌이 λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

가독성 및 μœ μ§€λ³΄μˆ˜μ„± ν–₯상: μ½”λ“œλ₯Ό νŒ¨ν‚€μ§€λ‘œ κ΅¬μ„±ν•˜λ©΄ μ½”λ“œμ˜ 일뢀λ₯Ό 더 μž‘μ€ λΆ€λΆ„μœΌλ‘œ λ‚˜λˆ„μ–΄ 가독성을 ν–₯μƒμ‹œν‚€κ³ , μœ μ§€λ³΄μˆ˜μ„±μ„ 높일 수 μžˆμŠ΅λ‹ˆλ‹€.

 

μ‚¬μš© 팁

  1. __init__.py 파일:
  2. νŒ¨ν‚€μ§€ 디렉토리에 __init__.py νŒŒμΌμ„ λ§Œλ“€μ–΄μ•Ό ν•©λ‹ˆλ‹€. 이 νŒŒμΌμ€ νŒ¨ν‚€μ§€λ₯Ό νŒŒμ΄μ¬μ—μ„œ νŒ¨ν‚€μ§€λ‘œ μΈμ‹ν•˜κ²Œ λ§Œλ“­λ‹ˆλ‹€. 이 νŒŒμΌμ€ λΉ„μ–΄ μžˆμ–΄λ„ λ˜μ§€λ§Œ, νŒ¨ν‚€μ§€ μ΄ˆκΈ°ν™” μ½”λ“œλ₯Ό 포함할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
  3. μƒλŒ€ 경둜 μž„ν¬νŠΈ μ‚¬μš©:
    # module2.pyμ—μ„œ module1.pyλ₯Ό μž„ν¬νŠΈν•  λ•Œ
    from . import module1
    
    
  4. μƒλŒ€ 경둜 μž„ν¬νŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ λͺ¨λ“ˆ κ°„μ˜ 관계λ₯Ό λͺ…μ‹œμ μœΌλ‘œ λ‚˜νƒ€λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” μ½”λ“œ 이동 및 μœ μ§€λ³΄μˆ˜λ₯Ό 더 μ‰½κ²Œ λ§Œλ“­λ‹ˆλ‹€.
  5. __all__ ν™œμš©:
    # __init__.py
    __all__ = ['module1', 'module2']
    
    
    μ΄λ ‡κ²Œ ν•˜λ©΄ νŒ¨ν‚€μ§€λ₯Ό μž„ν¬νŠΈν•  λ•Œ λͺ…μ‹œν•œ λͺ¨λ“ˆλ§Œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  6. νŒ¨ν‚€μ§€μ—μ„œ μ–΄λ–€ λͺ¨λ“ˆμ„ 외뢀에 λ…ΈμΆœμ‹œν‚¬μ§€ κ²°μ •ν•  λ•Œ, 각 λͺ¨λ“ˆμ—μ„œ __all__ λ³€μˆ˜λ₯Ό μ„€μ •ν•˜μ—¬ λͺ…μ‹œμ μœΌλ‘œ 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄λŸ¬ν•œ ꡬ쑰λ₯Ό μ‚¬μš©ν•˜λ©΄ μ½”λ“œλ₯Ό μ²΄κ³„μ μœΌλ‘œ κ΅¬μ„±ν•˜κ³  μœ μ§€λ³΄μˆ˜ν•˜κΈ° μ‰½κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

 

μ‹€μ „ 예제 맛보기

λ‹€μŒμ€ ν”ŒλΌμŠ€ν¬ μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ ꡬ쑰 μ˜ˆμ‹œμž…λ‹ˆλ‹€.

mywebapp/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ authentication.py
β”‚   β”œβ”€β”€ database.py
β”‚   └── utils.py
β”œβ”€β”€ web/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ routes.py
β”‚   └── templates/
β”‚       └── index.html
└── main.py

 

1. mywebapp/__init__.py:

# mywebapp/__init__.py
from .core import *
from .web import *

 

2. mywebapp/core/__init__.py:

# mywebapp/core/__init__.py
from .authentication import authenticate_user
from .database import connect_to_database
from .utils import *

 

3. mywebapp/core/authentication.py:

# mywebapp/core/authentication.py
def authenticate_user(username, password):
    # Authentication logic
    return True  # For simplicity, always return True

 

4. mywebapp/core/database.py:

# mywebapp/core/database.py
def connect_to_database():
    # Database connection logic
    return "Connected to database"  # For simplicity, return a string

 

5. mywebapp/core/utils.py:

# mywebapp/core/utils.py
def generate_random_key():
    # Utility function to generate a random key
    pass

 

6. mywebapp/web/__init__.py:

# mywebapp/web/__init__.py
from .routes import *

 

7. mywebapp/web/routes.py:

# mywebapp/web/routes.py
from flask import Flask, render_template
from ..core import authenticate_user, connect_to_database, generate_random_key

app = Flask(__name__)

@app.route('/')
def index():
    # Web route logic
    user_authenticated = authenticate_user("user", "password")
    db_connection = connect_to_database()
    random_key = generate_random_key()
    return render_template('index.html', auth=user_authenticated, db=db_connection, key=random_key)

 

8. mywebapp/web/templates/index.html:

<!-- mywebapp/web/templates/index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Web App</title>
</head>
<body>
    <h1>Welcome to my web app</h1>
    <p>Authentication: {{ auth }}</p>
    <p>Database Connection: {{ db }}</p>
    <p>Random Key: {{ key }}</p>
</body>
</html>

 

9. mywebapp/main.py:

# mywebapp/main.py
from mywebapp.web.routes import app

if __name__ == '__main__':
    app.run(debug=True)

 

이 ν”„λ‘œμ νŠΈμ—μ„œλŠ” core νŒ¨ν‚€μ§€μ—λŠ” λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°, μ‚¬μš©μž 인증 및 μœ ν‹Έλ¦¬ν‹°μ™€ 같은 핡심 κΈ°λŠ₯이 λ“€μ–΄ μžˆμŠ΅λ‹ˆλ‹€. web νŒ¨ν‚€μ§€μ—λŠ” Flask μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λΌμš°νŠΈκ°€ μ •μ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. 이 ꡬ쑰λ₯Ό μ‚¬μš©ν•˜λ©΄ ν”„λ‘œμ νŠΈμ˜ λ‹€μ–‘ν•œ 뢀뢄을 λͺ¨λ“ˆν™”ν•˜κ³  λΆ„λ¦¬ν•˜μ—¬ 관리할 수 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ 각 λͺ¨λ“ˆμ€ λ…λ¦½μ μœΌλ‘œ ν…ŒμŠ€νŠΈν•˜κ±°λ‚˜ μž¬μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

μ°Έκ³ : 점프 투 파이썬

λŒ“κΈ€