- Packaging Python Projects — Python Packaging User Guide
- Python: Creating a pip installable package – Python for HPC: Community Materials (betterscientificsoftware.github.io) – pyexample
- How to Create and Upload Your First Python Package to PyPI (freecodecamp.org)
ติดตั้ง Package
build – ใช้ build
twine – ใช้อัพโหลด
python -m pip install --upgrade pip python -m pip install --upgrade build python -m pip install --upgrade twine
สร้างโปรเจ็กส์ตัวอย่าง
packaging_tutorial/ ├── LICENSE ├── pyproject.toml ├── README.md ├── src/ │ └── example_package_phaisarn/ │ ├── __init__.py │ └── example.py └── tests/
ไฟล์ __init__.py
เป็นไฟล์เปล่า
ไฟล์ example.py
def add_one(number): return number + 1
ไดเร็กทอรี่ tests/
เป็นไดเร็กทอรี่ว่างๆ
ไฟล์ pyproject.toml
[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "example_package_phaisarn" version = "0.0.1" authors = [ { name="Phaisarn", email="phaisarn@example.com" }, ] description = "A small example package" readme = "README.md" requires-python = ">=3.7" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] [project.urls] "Homepage" = "https://github.com/pypa/phaisarn" "Bug Tracker" = "https://github.com/pypa/phaisarn/issues"
ไฟล์ README.md
# Example Package This is a simple example package. You can use [Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content.
ไฟล์ LICENSE
Copyright (c) 2018 The Python Packaging Authority Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Generating distribution archives
Now run this command from the same directory where pyproject.toml
is located:
python -m build
This command should output a lot of text and once completed should generate two files in the dist
directory:
dist/ ├── example_package_phaisarn-0.0.1-py3-none-any.whl └── example_package_phaisarn-0.0.1.tar.gz
Uploading the distribution archives
สร้าง Account ของ TestPyPI ก่อนที่ https://test.pypi.org/account/register/
อัพโหลดด้วย twine
python -m twine upload --repository testpypi dist/*
เข้าดูได้ที่ https://test.pypi.org/project/example-package-phaisarn
Installing your newly uploaded package
สร้าง virtual environment และทำการ activate
> python -m venv venv > .\venv\Scripts\activate
ติดตั้ง package จาก TestPyPI แบบไม่มี dependencies ด้วย --no-deps
python -m pip install -i https://test.pypi.org/simple/ --no-deps example-package-phaisarn
ทดสอบใช้งาน
>>> from example_package_phaisarn import example >>> example.add_one(2) 3
Upgrade package
อัพเกรดจากเวอร์ชัน 0.0.1 เป็นเวอร์ชัน 0.0.2 โดยแก้ไขไฟล์ pyproject.toml
[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "example_package_phaisarn" version = "0.0.2" authors = [ { name="Phaisarn", email="phaisarn@example.com" }, ] description = "A small example package" readme = "README.md" requires-python = ">=3.7" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] [project.urls] "Homepage" = "https://github.com/pypa/phaisarn" "Bug Tracker" = "https://github.com/pypa/phaisarn/issues"
สั่ง build
python -m build
จะได้ไฟล์
dist/ ├── example_package_phaisarn-0.0.1-py3-none-any.whl ├── example_package_phaisarn-0.0.1.tar.gz ├── example_package_phaisarn-0.0.2-py3-none-any.whl └── example_package_phaisarn-0.0.2.tar.gz
ให้ลบไฟล์เวอร์ชัน 0.0.1 ออกไป ไม่งั้น twine จะอัพโหลดเวอร์ชัน 0.0.1 แล้วจะ error แบบนี้
Uploading example_package_phaisarn-0.0.1-py3-none-any.whl 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.2/6.2 kB • 00:00 • ? WARNING Error during upload. Retry with the --verbose option for more details. ERROR HTTPError: 400 Bad Request from https://test.pypi.org/legacy/ File already exists. See https://test.pypi.org/help/#file-name-reuse for more information.
จะเหลือไฟล์เท่านี้
dist/ ├── example_package_phaisarn-0.0.2-py3-none-any.whl └── example_package_phaisarn-0.0.2.tar.gz
อัพโหลดด้วย twine
python -m twine upload --repository testpypi dist/*
ทดสอบใช้งานเวอร์ชันล่าสุด
สั่งอัพเกรด example_package_phaisarn
python -m pip install -i https://test.pypi.org/simple/ --upgrade example_package_phaisarn