Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Best Practice: จัดโครงสร้าง Python Module/Package ให้ดี

อ้างอิง samplemod ของ Kenneth Reitz (สร้าง Requests)

โครงสร้างมาตรฐาน


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คำอธิบายโปรเจกต์ วิธีติดตั้ง ใช้งาน
LICENSELicense & Copyright
setup.pyBuild/Install Script (python setup.py install)
requirements.txtDependency สำหรับ Dev/Test/Docs
sample/Source Code จริง (Module/Package)
docs/Documentation (Sphinx, MkDocs)
tests/Unit/Integration Tests (pytest)

หลักการสำคัญ

  • Source Code อยู่ในโฟลเดอร์ชื่อ Package (ไม่ใช่ src/ หรือ python/)
  • Single Module → วาง sample.py ที่ Root ได้
  • Tests โครงสร้างเหมือน Package