Search

νŒ¨ν‚€μ§€

β€’
νŒ¨ν‚€μ§€λž€
β€’
μ„€μΉ˜
β€’
import

νŒ¨ν‚€μ§€ (package)

λͺ¨λ“ˆ(파일)듀을 λ¬Άμ–΄ 놓은 디렉토리 ꡬ쑰
μ—¬λŸ¬ κΈ°λŠ₯이 ν¬ν•¨λœ 라이브러리의 μ§‘ν•©μž…λ‹ˆλ‹€.

λͺ¨λ“ˆ vs νŒ¨ν‚€μ§€

ꡬ뢄
λͺ¨λ“ˆ
νŒ¨ν‚€μ§€
λ‹¨μœ„
파일 ν•˜λ‚˜
디렉토리(폴더)
ꡬ성
단일 .py 파일
__init__.py + μ—¬λŸ¬ λͺ¨λ“ˆ νŒŒμΌλ“€
μ˜ˆμ‹œ
module.py
package/__init__.py + module1.py + module2.py
import 방식
import module
from package import module

νŒ¨ν‚€μ§€ μ„€μΉ˜

pip install νŒ¨ν‚€μ§€λͺ…
Shell
볡사
파이썬 νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜ν•˜λŠ” 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:
β€’
터미널/λͺ…λ Ή ν”„λ‘¬ν”„νŠΈλ₯Ό μ—½λ‹ˆλ‹€
β€’
pip install λͺ…λ Ήμ–΄ 뒀에 μ„€μΉ˜ν•˜κ³  싢은 νŒ¨ν‚€μ§€ 이름을 μž…λ ₯ν•©λ‹ˆλ‹€
μ„€μΉ˜κ°€ μ™„λ£Œλ˜λ©΄ 성곡 λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€. λ§Œμ•½ 이미 μ„€μΉ˜λœ νŒ¨ν‚€μ§€λΌλ©΄ 'Requirement already satisfied' λ©”μ‹œμ§€κ°€ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.
μ—¬λŸ¬ νŒ¨ν‚€μ§€λ₯Ό ν•œ λ²ˆμ— μ„€μΉ˜ν•˜κ³  μ‹Άλ‹€λ©΄ 곡백으둜 κ΅¬λΆ„ν•˜μ—¬ μž…λ ₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€:
pip install requests pandas matplotlib
Shell
볡사
νŠΉμ • 버전을 μ„€μΉ˜ν•˜κ³  μ‹Άλ‹€λ©΄ λ‹€μŒκ³Ό 같이 μž…λ ₯ν•©λ‹ˆλ‹€:
pip install pandas==1.5.3
Shell
볡사

import

import λͺ¨λ“ˆ
Bash
볡사

1. νŒ¨ν‚€μ§€ 전체 import

import mypackage.greetings
Python
볡사
μ‚¬μš© μ‹œ:
mypackage.greetings.hello()
Python
볡사

2. λͺ¨λ“ˆλ§Œ import

from mypackage import greetings
Python
볡사
μ‚¬μš© μ‹œ:
greetings.hello()
Python
볡사

3. ν•¨μˆ˜ λ˜λŠ” 클래슀만 import

from mypackage.greetings import hello
Python
볡사
μ‚¬μš© μ‹œ:
hello()
Python
볡사

4. ν•˜μœ„ νŒ¨ν‚€μ§€ import

from mypackage.subpackage import submodule
Python
볡사

5. λͺ¨λ“  ν•­λͺ© import (ꢌμž₯ )

from mypackage.greetings import *
Python
볡사
__all__을 μ„€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μ˜ˆμƒλŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•Šμ„ 수 μžˆμ–΄μš”.

μ°Έκ³ : __init__.py μ—­ν• 

β€’
__init__.pyλŠ” ν•΄λ‹Ή 디렉토리λ₯Ό "νŒ¨ν‚€μ§€"둜 μΈμ‹ν•˜κ²Œ ν•΄μ€λ‹ˆλ‹€.
β€’
λ‚΄λΆ€μ—μ„œ μ΄ˆκΈ°ν™” μ½”λ“œλ‚˜ κΈ°λ³Έ λ…ΈμΆœ ν•¨μˆ˜ 등을 μ„€μ •ν•  수 μžˆμ–΄μš”.
예:
# mypackage/__init__.py from .greetings import hello
Python
볡사
μ΄λ ‡κ²Œ ν•˜λ©΄:
from mypackage import hello hello()
Python
볡사

λͺ¨λ“ˆ import μ˜ˆμ‹œ

mymodule.pyλΌλŠ” ν•˜λ‚˜μ˜ .py 파일이 μžˆλ‹€κ³  κ°€μ •:
# mymodule.py def say_hello(): print("Hello from module!")
Python
볡사
# main.py import mymodule mymodule.say_hello()
Python
볡사

νŒ¨ν‚€μ§€ import μ˜ˆμ‹œ

# mypackage/ # β”œβ”€β”€ __init__.py # β”œβ”€β”€ greetings.py # └── utils.py
Python
볡사

greetings.py

def hello(): print("Hello from package!")
Python
볡사

main.py

from mypackage import greetings greetings.hello()
Python
볡사
ν•„μš”ν•˜λ©΄ μƒλŒ€ κ²½λ‘œλ‚˜ μ ˆλŒ€ 경둜 import도 μ˜ˆμ‹œλ‘œ 보여쀄 수 μžˆμ–΄μš”. 더 μΆ”κ°€ν• κΉŒμš”?

μ˜ˆμ‹œ μ½”λ“œ

예 1: requests νŒ¨ν‚€μ§€ μ‚¬μš© – μ›Ή νŽ˜μ΄μ§€ μš”μ²­

import requests res = requests.get("https://example.com") print(res.status_code) print(res.text)
Python
볡사

예 2: matplotlib νŒ¨ν‚€μ§€ μ‚¬μš© – κ·Έλž˜ν”„ 그리기

import matplotlib.pyplot as plt x = [1, 2, 3] y = [10, 20, 30] plt.plot(x, y) plt.title("Sample Graph") plt.show()
Python
볡사

νŒ¨ν‚€μ§€ ν”„λ‘œμ νŠΈ ꡬ쑰 μ˜ˆμ‹œ

my_package/ β”œβ”€β”€ __init__.py β”œβ”€β”€ module1.py β”œβ”€β”€ module2.py └── subpackage/ β”œβ”€β”€ __init__.py └── submodule.py
Markdown
볡사
이런 ꡬ쑰일 λ•Œ μ‚¬μš© μ˜ˆμ‹œ:
from my_package import module1 from my_package.subpackage import submodule
Python
볡사

νŒ¨ν‚€μ§€ λ§Œλ“€κΈ° μ‹€μŠ΅

1.
νŒ¨ν‚€μ§€ μ΄λ¦„μ˜ 폴더 생성
2.
__init__.py 파일 생성
"νŒ¨ν‚€μ§€"둜 μΈμ‹ν•˜κ²Œ ν•˜λŠ” νŠΉλ³„ν•œ 파일
3.
.py λͺ¨λ“ˆ 파일 μΆ”κ°€
4.
__init__.py μ—μ„œ κ³΅κ°œν•  λͺ¨λ“ˆ μ§€μ •
5.
νŒ¨ν‚€μ§€ κ°€μ Έμ™€μ„œ μ‚¬μš©ν•˜κΈ°

νŒ¨ν‚€μ§€ μ΄λ¦„μ˜ 폴더 생성

JSON 파일 데이터λ₯Ό 쑰회, 필터링, μ €μž₯
πŸ“¦ jsonutils
Python
볡사

__init__.py 파일 생성

"νŒ¨ν‚€μ§€"둜 μΈμ‹ν•˜κ²Œ ν•˜λŠ” νŠΉλ³„ν•œ 파일
πŸ“¦ jsonutils └── πŸ“œ __init__.py
Python
볡사

.py λͺ¨λ“ˆ 파일 μΆ”κ°€

πŸ“¦ jsonutils/ β”œβ”€β”€ πŸ“œ __init__.py β”œβ”€β”€ πŸ“¦ io/ β”‚ β”œβ”€β”€ πŸ“œ __init__.py β”‚ └── πŸ“œ json_loader.py β”œβ”€β”€ πŸ“¦ processing/ β”‚ β”œβ”€β”€ πŸ“œ __init__.py β”‚ └── πŸ“œ filter.py └── πŸ“¦ storage/ β”œβ”€β”€ πŸ“œ __init__.py └── πŸ“œ json_saver.py
Markdown
볡사
이런 ꡬ쑰둜 폴더와 νŒŒμΌμ„ λ§Œλ“€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€:
β€’
io: JSON 파일 읽기 κ΄€λ ¨ λͺ¨λ“ˆ
β€’
processing: JSON 데이터 처리/필터링 κ΄€λ ¨ λͺ¨λ“ˆ
β€’
storage: JSON 파일 μ €μž₯ κ΄€λ ¨ λͺ¨λ“ˆ
import json """ json 파일 읽기 """ def read_json(filepath): # 파일 μ—΄κΈ° with open(filepath, 'r', encoding='utf-8') as f: # JSON 파일 읽기 return json.load(f)
Python
볡사
''' 리슀트 필터링 ν•¨μˆ˜ ''' def filter_by_field(data, field, value): # list μΈμŠ€ν„΄μŠ€κ°€ 아닐 λ•Œ if not isinstance(data, list): # raise : μ˜ˆμ™Έ κ°•μ œ λ°œμƒ ν‚€μ›Œλ“œ # ValueError : μ μ ˆν•œ 값이 μ•„λ‹Œ 경우 λ°œμƒν•˜λŠ” μ˜ˆμ™Έ raise ValueError("리슀트 ν˜•μ‹μ΄ μ•„λ‹™λ‹ˆλ‹€.") return [ item for item in data if item.get(field) == value ]
Python
볡사
import json ''' JSON 데이터 μ €μž₯ ν•¨μˆ˜ ''' def save_json(data, filepath): # μ“°κΈ° λͺ¨λ“œλ‘œ 파일 μ—΄κΈ° with open(filepath, 'w', encoding='utf-8') as f: # data λ₯Ό JSON ν˜•μ‹μœΌλ‘œ 파일 μ €μž₯ # ensure_ascii : False μ•„μŠ€ν‚€ λ¬Έμžκ°€ μ•„λ‹Œ 것도 κ·ΈλŒ€λ‘œ μ €μž₯ # indent : λ“€μ—¬μ“°κΈ°λ₯Ό 2칸으둜 μ§€μ • json.dump(data, f, ensure_ascii=False, indent=2)
Python
볡사

__init__.py μ—μ„œ κ³΅κ°œν•  λͺ¨λ“ˆ μ§€μ •

from .io.json_loader import read_json from .processing.filter import filter_by_field from .storage.json_saver import save_json __all__ = ['read_json', 'filter_by_field', 'save_json']
Python
볡사
β€’
μ—¬κΈ°μ„œλŠ” read_json, filter_by_field, save_json만 μ™ΈλΆ€μ—μ„œ μ ‘κ·Ό κ°€λŠ₯ν•˜λ„λ‘ μ„€μ •ν•©λ‹ˆλ‹€.

νŒ¨ν‚€μ§€ κ°€μ Έμ™€μ„œ μ‚¬μš©ν•˜κΈ°

β€’
main.py
from jsonutils import read_json, filter_by_field, save_json data = read_json('student.json') filtered = filter_by_field(data, 'gender', 'female') save_json(filtered, 'filtered.json')
Python
볡사

__init__.py 파일이 λΉ„μ–΄ μžˆμ–΄λ„ λ˜λ‚˜μš”?

β€’
λ„€, λ‚΄μš©μ΄ 없어도 μ „ν˜€ λ¬Έμ œμ—†κ³  μ•„μ£Ό ν”ν•œ μƒν™©μž…λ‹ˆλ‹€.
β€’
단지 폴더가 νŒ¨ν‚€μ§€μž„μ„ λͺ…μ‹œν•˜λŠ” 역할을 ν•˜λ©°, μ΄ˆκΈ°ν™” μ½”λ“œκ°€ ν•„μš”ν•œ 경우 κ·Έλ•Œ λ‚΄μš©μ„ μΆ”κ°€ν•˜λ©΄ λ©λ‹ˆλ‹€.
상황
__init__.py ν•„μš” μ—¬λΆ€
λΉ„κ³ 
일반 μ„œλΈŒνŒ¨ν‚€μ§€λ‘œ λ§Œλ“€κ³  싢을 λ•Œ
ꢌμž₯ (ν•„μˆ˜ μ•„λ‹˜)
λ‚΄μš©μ€ λΉ„μ›Œλ„ 됨
λ„€μž„μŠ€νŽ˜μ΄μŠ€ νŒ¨ν‚€μ§€ (PEP420)
없어도 λ™μž‘ κ°€λŠ₯
λ³΅μž‘ν•œ ν”„λ‘œμ νŠΈλ‚˜ λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ ν™œμš©

__all__

ν•΄λ‹Ή νŒ¨ν‚€μ§€μ—μ„œ 곡개적으둜 μ œκ³΅ν•  이름듀을 λͺ…μ‹œμ μœΌλ‘œ μ •μ˜ν•˜λŠ” λ¦¬μŠ€νŠΈμž…λ‹ˆλ‹€.
__all__의 μ£Όμš” λͺ©μ μ€:
β€’
λͺ¨λ“ˆ κ°€μ Έμ˜€κΈ°λ₯Ό μ œμ–΄ν•˜μ—¬ μ½”λ“œμ˜ λͺ…ν™•μ„±κ³Ό μ˜λ„λ₯Ό λͺ…μ‹œμ μœΌλ‘œ ν‘œν˜„
β€’
λΆˆν•„μš”ν•œ 이름 곡간 μ˜€μ—Ό λ°©μ§€ (ν•„μš”ν•œ κ²ƒλ§Œ 곡개)
β€’
API 섀계와 μœ μ§€λ³΄μˆ˜μ„± ν–₯상 (곡개 μΈν„°νŽ˜μ΄μŠ€λ₯Ό λͺ…ν™•νžˆ μ •μ˜)
λ¦¬μ†ŒμŠ€ μ†Œλͺ¨ μΈ‘λ©΄μ—μ„œλŠ” 큰 영ν–₯이 μ—†μŠ΅λ‹ˆλ‹€. Python은 μ‹€μ œλ‘œ λͺ¨λ“ˆμ„ μ‚¬μš©ν•  λ•Œλ§Œ λ©”λͺ¨λ¦¬μ— λ‘œλ“œν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
β€’
from package import * λ₯Ό μ‹€ν–‰ν•  λ•Œ μ–΄λ–€ λͺ¨λ“ˆκ³Ό ν•¨μˆ˜λ₯Ό import ν• μ§€ μ œμ–΄ν•©λ‹ˆλ‹€.
# mypackage/__init__.py __all__ = ['module1', 'function1', 'function2'] # 이 ν•­λͺ©λ“€λ§Œ 곡개됨
Python
볡사
β€’
__all__을 μ •μ˜ν•˜μ§€ μ•ŠμœΌλ©΄ λͺ¨λ“  public 이름(μ–Έλ”μŠ€μ½”μ–΄λ‘œ μ‹œμž‘ν•˜μ§€ μ•ŠλŠ” 이름)이 importλ©λ‹ˆλ‹€.
β€’
μ½”λ“œμ˜ λͺ…ν™•μ„±κ³Ό μ•ˆμ „μ„±μ„ μœ„ν•΄ __all__을 λͺ…μ‹œμ μœΌλ‘œ μ •μ˜ν•˜λŠ” 것이 쒋은 κ΄€ν–‰μž…λ‹ˆλ‹€.
예λ₯Ό λ“€μ–΄, μœ„ μ˜ˆμ œμ—μ„œλŠ” read_json, filter_by_field, save_json ν•¨μˆ˜λ§Œ μ™ΈλΆ€μ—μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ œν•œν–ˆμŠ΅λ‹ˆλ‹€.