reload(module) โหลดโมดูลซ้ำ (Re-execute Code) ใช้ตอน Develop แก้โค้ดทดสอบทันที Python 2 >>> import mymodule >>> # แก้ mymodule.py >>> reload(mymodule) Python 3 (imp.reload → importlib.reload) >>> import importlib >>> import mymodule >>> # แก้
reload(module) โหลดโมดูลซ้ำ (Re-execute Code) ใช้ตอน Develop แก้โค้ดทดสอบทันที Python 2 >>> import mymodule >>> # แก้ mymodule.py >>> reload(mymodule) Python 3 (imp.reload → importlib.reload) >>> import importlib >>> import mymodule >>> # แก้
โมดูล Pure Python: ดู module.__file__ ตัวอย่าง >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py Built-in Modules (C Extension) __file__ ชี้ไป .so (Linux/macOS) หรือ .pyd (Windows) Windows บางกรณีไม่มี __file__ Source ไม่อยู่ในไฟล์นั้น (อยู่ใน CPython Source
Module = ไฟล์ .py ที่มี Definitions ให้ Import ใช้ 1. สร้าง helloworld.py def hello_world(): print(Hello world) 2. สร้าง script.py ใช้งาน import helloworld helloworld.hello_world() 3. รัน $ python script.py Hello world จัดกลุ่มหลาย Module ด้วย Package my_package/ __init__.py # ท
Python ไม่ต้องประกาศตัวแปร (No Declaration) ตัวแปรสร้างขึ้นตอน Assignment ครั้งแรก Static vs Dynamic Typing Static (C, Java, C#)Dynamic (Python) ต้องประกาศ Type ก่อนใช้ int x;ไม่ต้องประกาศ Type Compiler เช็ค Type Compile-timeType ถูก 결정 Runtime x = Hello → Compile Errorx = Hello → OK (x เป็น st
ดาวน์โหลดจาก python.org/downloads (32-bit / 64-bit MSI Installer) ขั้นตอนติดตั้ง Run Installer ด้วย Admin Rights ☑ Add Python to PATH (สำคัญมาก) ☑ Install for all users (แนะนำ) เลือก Customize Installation → เลือก Features ทั้งหมด Advanced Options: ☑ Install for all users, ☑ Add to PATH, ☑ Precomp
Identifier = ชื่อตัวแปร ฟังก์ชัน คลาส โมดูล แพ็กเกจ กฎ Syntax ขึ้นต้นด้วย: ตัวอักษร (a-z, A-Z) หรือ Underscore (_) ตามด้วย: ตัวอักษร, ตัวเลข (0-9), Underscore ได้ไม่จำกัด Case-sensitive: name ≠ Name ≠ NAME ห้ามใช้ Keywords Convention (PEP 8) ประเภทStyleตัวอย่าง ClassCapWords (PascalCase)MyCl
Keywords มีความหมายพิเศษใน Syntax ไม่สามารถใช้เป็น Identifier (ชื่อตัวแปร ฟังก์ชัน คลาส) ได้ รายการ Keywords Python 3 (33 คำ) andexceptlambdawith asfinallynonlocalwhile assertfalseNoneyield breakfornot classfromor continueglobalpass defifraise delimportreturn elifinTrue elseistry Python 2 v
Python ใช้ Indentation (Whitespace) กำหนด Block ของโค้ด แทน Curly Braces {} ใน C/Java กฎหลัก ก่อน Block ใช้ : (Colon) Statement ใน Block ต้อง Indent เท่ากัน (ปกติ 4 Spaces) Dedent (ลด Indent) = จบ Block ตัวอย่าง num = int(input(enter number: )) if num % 2 == 0: if num % 3 == 0: print
json ใน Standard Library (Python 2.6+) เป็น Fork ของ simplejson ความต่างหลัก ด้านjson (stdlib)simplejson Python Version2.6+ / 3.0+2.4+ Update Frequencyตาม Python Releaseบ่อยกว่า (Independent) Performanceพื้นฐานOptimized ใหม่กว่า APIเหมือนกันเกือบทั้งหมดเหมือนกัน + Extensions Best Practice: F
ใช้ dir(module) ดู Attribute ทั้งหมด แล้ว Filter ด้วย inspect.isfunction Basic: dir() >>> import math >>> dir(math) [__doc__, __name__, acos, acosh, asin, ..., sqrt, tan, tanh, trunc] Filter เฉพาะ Function (User-defined) import helloworld >>> [name for name, obj in get
pip คือ Package Manager มาตรฐาน ติดตั้ง Dependency อัตโนมัติ 1. เตรียม pip # Python 2.7.9+ / 3.4+ มาพร้อม pip # อัปเกรด: # Linux/macOS pip install -U pip setuptools # Windows python -m pip install -U pip setuptools 2. ติดตั้ง pip หากไม่มี curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py py
ใช้ pip uninstall ถอนแพ็กเกจที่ติดตั้งผ่าน pip คำสั่งพื้นฐาน $ pip uninstall numpy กรณีที่ pip Uninstall ไม่ได้ Pure distutils (python setup.py install) - ไม่มี Metadata Editable Install (pip install -e / python setup.py develop) - Script Wrapper วิธีลบ Manual (Last Resort) Reinstall พร้อม Re
IntelliJ/PyCharm ต้องการ Python SDK เพื่อ Index โมดูล ให้ IntelliSense/Documentation ตั้งค่า Project SDK File → Project Structure → Project → Project SDK → New # เลือก Path ของ Python Interpreter # Windows: C:\Python39\python.exe # Linux/macOS: /usr/bin/python3 หายังไม่ Recognize Builtins File →
Python รัน Zip File ได้โดยตรง (Python 2.6+) 1. Zip App (Built-in) __main__.py $ zip my_app.zip __main__.py $ python my_app.zip 2. Stickytape (รวม Script + Deps เป็น 1 ไฟล์ .py) sample-standalone.py # หรือ $ stickytape scripts/sample --add-python-path . --output-file sample-standalone.py 3. To
ใช้ sys.modules Dictionary ที่ Map Module Name → Module Object ดูโมดูลที่โหลดแล้วทั้งหมด import sys >>> print(sys.modules.keys()) # [copy_reg, sre_compile, datetime, sys, os, ...] Filter เฉพาะ Package ที่สนใจ >>> [m for m in sys.modules if m.startswith(django.)] [django, django
คู่มือสั้น สำหรับติดตั้ง pip และใช้งานพื้นฐาน 1. อัปเกรด pip # Linux/macOS pip install -U pip setuptools # Windows python -m pip install -U pip setuptools 2. ติดตั้ง pip หากไม่มี curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py 3. ติดตั้งแพ็กเกจ =1,<2 # ช่วงเวอร์ชัน
สมมติ: Python 3.6, Pipenv, Heroku CLI ติดตั้งและ Login แล้ว 1. ต้องมี Git Repo $ cd my-project $ heroku create Creating lit-bastion-5032... done https://lit-bastion-5032.herokuapp.com/ | https://git.heroku.com/lit-bastion-5032.git 2. Push Code Deploy $ git push heroku master # Heroku Detect Pytho
เครื่องมือที่ต้องมี: Python, pip, virtualenv, AWS EB CLI, SSH Client ติดตั้ง EB CLI: Official Guide 1. สร้าง Virtual Environment $ virtualenv -p python2.7 /tmp/hello-world $ source /tmp/hello-world/bin/activate 2. สร้าง requirements.txt requirements.txt # หรือเขียนเอง Flask==0.8 Jinja2==2.6 Werk
อ้างอิง samplemod โครงสร้างมาตรฐานที่ยอมรับกว้าง โครงสร้าง README.rst LICENSE setup.py requirements.txt sample/ __init__.py core.py helpers.py docs/ conf.py index.rst tests/ test_basic.py test_advanced.py รายละเอียด README.rst: Overview, Install, Usage LICENSE: Licen
ตอนติดตั้ง Cygwin เลือก Package: python3, python3-setuptools (จะได้ easy_install) ติดตั้ง pip ผ่าน easy_install $ easy_install-3.8 pip # แทน 3.8 ด้วย Version ที่ติดตั้ง ใช้ pip ติดตั้งโมดูล =1,<2 # ช่วงเวอร์ชัน หมายเหตุ Cygwin ให้ Python แบบ POSIX บน Windows บาง Package ที่มี C Extension