๋ชฉ์ฐจ
์ผ์ฉ์ง ์๋์ธ ๊ณ์ฐ๋ฐฉ๋ฒ, ํ์ด์ฌ ์ผ์ฉ์ง ์๋์ธ ์๋ ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ
์ผ์ฉ์ง ๊ทผ๋ก์๋ ์๊ธ์ ๊ฐ ์๋ ์ผ๊ธ์ฌ × ๊ทผ๋ฌด์ผ์๋ก ์๋์ด ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์, ๋งค๋ฒ ์ง๊ธ ์ ์์ฒ์ง์์ธ์ก์ ์ ํํ ๊ณ์ฐํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ์์์ ์ผ๋ก ๊ณ์ฐํ๋ค ๋ณด๋ฉด ์ค์๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก, ๊ฐ๋จํ ํ์ด์ฌ ์คํฌ๋ฆฝํธ๋ฅผ ํตํด ์๋์ผ๋ก ์ธ์ก์ ์ฐ์ถํ๋ฉด ์ ๋ฌด ํจ์จ์ฑ๊ณผ ์ ํ์ฑ์ ๋์์ ๋์ผ ์ ์์ต๋๋ค. ์ผ์ฉ์ง ์๋์ธ ๊ณ์ฐ๋ฐฉ๋ฒ์ผ๋ก ๋ณธ ํฌ์คํ ์์๋ ์ผ์ฉ์ง ์๋์ธ ๊ณ์ฐ๊ธฐ๋ฅผ ํ์ด์ฌ์ผ๋ก ๊ตฌํํ๊ณ , PyInstaller๋ฅผ ์ด์ฉํด exe ํ์ผ๋ก ๋ฐฐํฌํ๋ ๋ฐฉ๋ฒ๊น์ง ๋จ๊ณ๋ณ๋ก ์๋ด๋๋ฆฝ๋๋ค.
์ผ์ฉ์ง ์๋์ธ ๊ณ์ฐ๋ฐฉ๋ฒ ๊ฐ๋ฐ ํ๊ฒฝ ๋ฐ ์ค๋น์ฌํญ
Python ์ค์น
- ๊ณต์ ํํ์ด์ง(python.org)์์ Python 3.8 ์ด์ ๋ฒ์ ์ ์ค์นํด ์ฃผ์ธ์.
- ์ค์น ์ “Add Python to PATH” ์ต์
์ ์ฒดํฌํด์ผ ํฐ๋ฏธ๋(๋๋ ๋ช
๋ น ํ๋กฌํํธ)์์ ๋ฐ๋ก
python
๋ช ๋ น์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ณธ ์์ ๋ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ง ์ฌ์ฉํ๋ฏ๋ก, ๋ณ๋์ ํจํค์ง ์ค์น๊ฐ ํ์ ์์ต๋๋ค.
- exe ๋ฐฐํฌ๋ฅผ ์ํ PyInstaller๋ง ์ถ๊ฐ๋ก ์ค์นํฉ๋๋ค.
pip install pyinstaller
์๋ํฐ ๋ฐ ์คํ ํ๊ฒฝ
- ์ฝ๋ ์์ฑ์ VS Code, PyCharm, Sublime Text ๋ฑ ํธํ ํธ์ง๊ธฐ๋ฅผ ์ฌ์ฉํ์ธ์.
- ํฐ๋ฏธ๋(cmd, PowerShell, bash)์์
python tax_calculator.py
๋ช ๋ น์ผ๋ก ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ ์ ์์ต๋๋ค.
์ผ์ฉ์ง ์๋์ธ ์๋ ๊ณ์ฐ๊ธฐ ์ ์ฒด ์์ค์ฝ๋
import tkinter as tk
from tkinter import messagebox
def calculate_tax():
"""
1. ๊ณผ์ธ์๋๊ธ์ก = max(์ผ๊ธ์ฌ - 150,000์, 0)
2. ์ฐ์ถ์ธ์ก = ๊ณผ์ธ์๋๊ธ์ก * 6%
3. ๊ฒฐ์ ์ธ์ก = ์ฐ์ถ์ธ์ก * (1 - 55%)
4. ์์ฒ์ง์์ธ์ก = ๊ฒฐ์ ์ธ์ก * 1.1 (์ง๋ฐฉ์๋์ธ ํฌํจ)
"""
try:
daily_wage = int(entry_wage.get())
# 1. ๊ณผ์ธ์๋๊ธ์ก
taxable_income = max(daily_wage - 150_000, 0)
# 2. ์ฐ์ถ์ธ์ก
basic_tax = taxable_income * 0.06
# 3. ๊ฒฐ์ ์ธ์ก
final_tax = basic_tax * (1 - 0.55)
# 4. ์ง๋ฐฉ์๋์ธ ํฌํจ
withholding_tax = round(final_tax * 1.1)
label_result.config(text=f"์์ฒ์ง์์ธ์ก: {withholding_tax}์")
except ValueError:
messagebox.showerror("์
๋ ฅ ์ค๋ฅ", "์ผ๊ธ์ฌ์๋ ์ซ์๋ง ์
๋ ฅํด์ฃผ์ธ์.")
# ๋ฉ์ธ ์๋์ฐ ์ค์
root = tk.Tk()
root.title("์ผ์ฉ์ง ์๋์ธ ๊ณ์ฐ๊ธฐ")
root.geometry("320x180")
root.resizable(False, False)
# ์
๋ ฅ ํ๋ ์
frame = tk.Frame(root, padx=10, pady=10)
frame.pack(fill="both", expand=True)
# ์์ ฏ ๋ฐฐ์น
tk.Label(frame, text="์ผ๊ธ์ฌ(์):").grid(row=0, column=0, sticky="e")
entry_wage = tk.Entry(frame)
entry_wage.grid(row=0, column=1, pady=5)
btn_calc = tk.Button(frame, text="๊ณ์ฐํ๊ธฐ", width=20, command=calculate_tax)
btn_calc.grid(row=1, column=0, columnspan=2, pady=10)
label_result = tk.Label(frame, text="์์ฒ์ง์์ธ์ก: ", font=("๋ง์ ๊ณ ๋", 10, "bold"))
label_result.grid(row=2, column=0, columnspan=2)
# ์คํ
root.mainloop()
exe ์ปดํ์ผ ๋ฐฉ๋ฒ
PyInstaller ์ค์น
pip install pyinstaller
๋จ์ผ ํ์ผ exe๋ก ๋ฌถ๊ธฐ
- ํฐ๋ฏธ๋์์ ์์ค ์ฝ๋๊ฐ ์๋ ํด๋๋ก ์ด๋
- ์๋ ๋ช
๋ น ์คํ
pyinstaller --onefile tax_calculator.py
dist
ํด๋ ์์tax_calculator.exe
ํ์ผ์ด ์์ฑ๋ฉ๋๋ค.- ์์ฑ๋ exe ํ์ผ์ ๋๋ธํด๋ฆญํ๊ฑฐ๋,
์ผ๋ก ์คํํ ์ ์์ต๋๋ค../dist/tax_calculator.exe
๋ฐฐํฌ ํ
--name
์ต์ ์ผ๋ก exe ํ์ผ๋ช ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.pyinstaller --onefile --name ์ผ์ฉ์ง์ธ๊ธ๊ณ์ฐ๊ธฐ tax_calculator.py
- ์์ด์ฝ(.ico) ํ์ผ์ ์ง์ ํ๋ ค๋ฉด
--icon=myicon.ico
์ต์ ์ ์ถ๊ฐํฉ๋๋ค.
PyInstaller ์ปดํ์ผ ์๋ฌ(UnicodeDecodeError) ํด๊ฒฐ ๋ฐฉ๋ฒ
6364 INFO: Analyzing D:\WorkPY\CalcTax_nogada\calculate_daily_tax.py
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
sys.exit(_console_script_run())
~~~~~~~~~~~~~~~~~~~^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\__main__.py", line 231, in _console_script_run
run()
~~~^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\__main__.py", line 215, in run
run_build(pyi_config, spec_file, **vars(args))
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\__main__.py", line 70, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\building\build_main.py", line 1270, in main
build(specfile, distpath, workpath, clean_build)
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\building\build_main.py", line 1208, in build
exec(code, spec_namespace)
~~~~^^^^^^^^^^^^^^^^^^^^^^
File "D:\WorkPY\CalcTax_nogada\calculate_daily_taxGUI.spec", line 4, in <module>
a = Analysis(
['calculate_daily_tax.py'],
...<9 lines>...
optimize=0,
)
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\building\build_main.py", line 582, in __init__
self.__postinit__()
~~~~~~~~~~~~~~~~~^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
self.assemble()
~~~~~~~~~~~~~^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\building\build_main.py", line 712, in assemble
program_scripts.append(self.graph.add_script(script))
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\depend\analysis.py", line 283, in add_script
self._top_script_node = super().add_script(pathname)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "C:\Users\hidec\AppData\Local\Programs\Python\Python313\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1170, in add_script
contents = importlib.util.decode_source(contents)
File "<frozen importlib._bootstrap_external>", line 825, in decode_source
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 64: invalid start byte
์ปดํ์ผ ์ค ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 64: invalid start byte
์์ธ
- ์์ค ํ์ผ์ด UTF-8์ด ์๋ ๋ค๋ฅธ ์ธ์ฝ๋ฉ(CP949, ANSI ๋ฑ)์ผ๋ก ์ ์ฅ๋์ด ์์ด PyInstaller๊ฐ ๋์ฝ๋ฉ์ ์คํจํจ.
ํด๊ฒฐ ๋ฐฉ๋ฒ
- ์ธ์ฝ๋ฉ ์ ์ธ ์ถ๊ฐ
- ์ฝ๋ ์ต์๋จ์ # -*- coding: utf-8 -*-๋ฅผ ๋ฃ์ด ์ธ์ฝ๋ฉ์ ๋ช ์
- ํ์ผ ์ธ์ฝ๋ฉ์ UTF-8๋ก ๋ณํ
- VS Code: ์ฐ์ธก ํ๋จ ์ธ์ฝ๋ฉ ๋ถ๋ถ ํด๋ฆญ → “Save with Encoding” → “UTF-8” ์ ํ
- Notepad++: ์ธ์ฝ๋ฉ ๋ฉ๋ด → “UTF-8๋ก ๋ณํ” → ์ ์ฅ
- ๋ค์ ์ปดํ์ผ
pyinstaller --onefile tax_calculator.py
- ํ์ผ ์ธ์ฝ๋ฉ์ UTF-8๋ก ํต์ผํ๊ณ ,
- # -*- coding: utf-8 -*- ์ ์ธ์ ์ถ๊ฐํ๋ฉด PyInstaller ์ปดํ์ผ ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ ์ ์์ต๋๋ค.
- ์ ๊ณผ์ ์ ๊ฑฐ์น๋ฉด ๋๊ตฌ๋ ์ค์น ์์ด ์คํ ๊ฐ๋ฅํ EXE ํํ๋ก ์ผ์ฉ์ง ์ธ๊ธ ๊ณ์ฐ๊ธฐ๋ฅผ ๋ฐฐํฌํ ์ ์์ต๋๋ค.
๊ฒฐ๋ก
์ค๋์ ํ์ด์ฌ์ผ๋ก ์ผ์ฉ์ง ๊ทผ๋ก์์ ์์ฒ์ง์์ธ์ก์ ์๋์ผ๋ก ๊ณ์ฐํ๋ ์ธ๊ธ ๊ณ์ฐ๊ธฐ๋ฅผ ๋ง๋ค์ด ๋ณด์์ต๋๋ค.
- ๊ฐ๋จํ ํจ์ ๊ตฌ์กฐ๋ก ์ ์ง๋ณด์๊ฐ ์ฉ์ดํ๊ณ ,
- PyInstaller๋ฅผ ํ์ฉํด ์๋์ฐ์ฉ exe๋ก ๋ฐฐํฌํ ์ ์์ด ๋๊ตฌ๋ ์ค์น ์์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ธ์ฌ·๋ ธ๋ฌด ๋ด๋น์๋ผ๋ฉด ์ด ๊ณ์ฐ๊ธฐ๋ฅผ ํ์ฉํด ์ผ์ฉ์ง ์ธ์ก ์ฐ์ถ ์ ๋ฌด๋ฅผ ํฌ๊ฒ ์ค์ผ ์ ์์ ๊ฒ์ ๋๋ค.
'Lect & Tip > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ RuntimeError: lost sys.stdin ์ค๋ฅ ํด๊ฒฐ (0) | 2025.04.26 |
---|---|
ํ์ด์ฌ ์ธ์คํ๊ทธ๋จ ์ฌ์ง ๋ค์ด๋ก๋ ์คํฌ๋ฆฝํธ ๋ง๋ค๊ธฐ (0) | 2025.04.24 |
๋๊ธ