วิธีการสร้างโมดูล library ในภาษา Python ให้สามารถติดตั้งได้ด้วย pip แบ่งปันโมดูลให้คนอื่นใช้
หลายๆ คนอาจคุ้นเคยกับ การติดตั้งโมดูลภายนอกเข้ามาใช้งานในโปรเจ็คเราด้วยคำสั่ง pip วันนี้ทางเว็บ
mindphp.com จะมาแนะนำวิธีการทำโมดูล หรือ library เพื่อนำไปใช้งานที่ไหนก็ได้ เพียงแค่ติดตั้ง และ Import โมดูลเข้ามาใช้งาน
library คือ ฟังก์ชั่นหรือ
module ที่สามารถเรียกเข้ามาใช้งานในโปรแกรมของเรา ซึ่งจะต้องทำการติดตั้งก่อนหรือ สามารถสร้างขึ้นมาเองได้ โดยการเรียกใช้นั้นจะต้องการ
import เข้ามา และถ้าใครที่กำลังหาวิธีหรืออยากสร้าง libraryของภาษา
python ขึ้นมาใช้เองผมมีวิธีมาแชร์ให้ครับ ขั้นตอนแรกนะครับต้องไปทำการสมัคร username ของเว็บไซต์ http://www.pypi.org กันก่อนนะครับ
วิธีการสมัคร
คลิกที่ register
กรอกข้อมูลในการสมัครให้ครบ
จากนั้นทำการยืนยันใน email ที่กรอกในหน้าสมัคร
ก็เป็นการเสร็จสิ้น
ให้มาเขียนโค้คคำสั่งภาษา Python ที่เราต้องการที่จะใช้
สร้างไฟล์ init และ ไฟล์ module ขึ้นมา
ไฟล์ __init__
ไฟล์ module python
โค้ด: เลือกทั้งหมด
class testpypi:
def __init__(self, num,num2):
self.number = num
self.number2=num2
def sum(self):
number = self.number
number2 = self.number2
sum = number+number2
return sum
จากนั้นให้สร้างไฟล์ setup.py, LICENCE.txt และ README.md
ไฟล์ setup.py
โค้ด: เลือกทั้งหมด
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="test1234pypi",
version="0.0.1",
author="test",
author_email="[email protected]",
description="A small testpypi package",
long_description=long_description,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 2.7",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
ไฟล์ LICENCE.txt
โค้ด: เลือกทั้งหมด
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.
ไฟล์ README.md
จะเป็นการเขียนวิธีใช้ module ของเรา
โค้ด: เลือกทั้งหมด
# 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.
เสร็จแล้วให้รันคำสั่ง terminel ใน pycharm
ผลที่ได้
และจะมีไฟล์สร้างขึ้นมา
ให้ทำการติดตั้ง twine
ผลที่ได้
แล้วให้พิมพ์คำสั่ง
ขึ้นแบบนี้
แล้วทำการกรอก username/password ที่ได้สมัครไปเมื่อตอนแรก
ผลที่ได้
เข้าไปดูในเว็บไซต์ pypi จะmodule library ที่เรา upload
และสามารถเรียกใช้ได้โดย
อ้างอิง
https://medium.com/@joel.barmettler/how-to-upload-your-python-package-to-pypi-65edc5fe9c56
https://stackoverflow.com/questions/15746675/how-to-write-a-python-module-package
https://packaging.python.org/tutorials/packaging-projects/
[size=200]วิธีการสร้างโมดูล library ในภาษา Python ให้สามารถติดตั้งได้ด้วย pip แบ่งปันโมดูลให้คนอื่นใช้[/size]
หลายๆ คนอาจคุ้นเคยกับ การติดตั้งโมดูลภายนอกเข้ามาใช้งานในโปรเจ็คเราด้วยคำสั่ง pip วันนี้ทางเว็บ [url=https://www.mindphp.com]mindphp.com[/url] จะมาแนะนำวิธีการทำโมดูล หรือ library เพื่อนำไปใช้งานที่ไหนก็ได้ เพียงแค่ติดตั้ง และ Import โมดูลเข้ามาใช้งาน
library คือ ฟังก์ชั่นหรือ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python/2494-modules-%E0%B9%83%E0%B8%99-python.html]module[/url] ที่สามารถเรียกเข้ามาใช้งานในโปรแกรมของเรา ซึ่งจะต้องทำการติดตั้งก่อนหรือ สามารถสร้างขึ้นมาเองได้ โดยการเรียกใช้นั้นจะต้องการ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python/2495-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89-from-import-modules.html]import[/url] เข้ามา และถ้าใครที่กำลังหาวิธีหรืออยากสร้าง libraryของภาษา [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]python[/url] ขึ้นมาใช้เองผมมีวิธีมาแชร์ให้ครับ ขั้นตอนแรกนะครับต้องไปทำการสมัคร username ของเว็บไซต์ http://www.pypi.org กันก่อนนะครับ
[b]วิธีการสมัคร[/b]
คลิกที่ register
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d18883a02c[/img]
กรอกข้อมูลในการสมัครให้ครบ
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d1939105f5[/img]
จากนั้นทำการยืนยันใน email ที่กรอกในหน้าสมัคร
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d19f24f646[/img]
ก็เป็นการเสร็จสิ้น
ให้มาเขียนโค้คคำสั่งภาษา Python ที่เราต้องการที่จะใช้
สร้างไฟล์ init และ ไฟล์ module ขึ้นมา
ไฟล์ __init__
[code]
from test1234pypi import testpypi
[/code]
ไฟล์ module python
[code]
class testpypi:
def __init__(self, num,num2):
self.number = num
self.number2=num2
def sum(self):
number = self.number
number2 = self.number2
sum = number+number2
return sum
[/code]
จากนั้นให้สร้างไฟล์ setup.py, LICENCE.txt และ README.md
ไฟล์ setup.py
[code]
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="test1234pypi",
version="0.0.1",
author="test",
author_email="
[email protected]",
description="A small testpypi package",
long_description=long_description,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 2.7",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
[/code]
ไฟล์ LICENCE.txt
[code]
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.
[/code]
ไฟล์ README.md
จะเป็นการเขียนวิธีใช้ module ของเรา
[code]
# 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.
[/code]
เสร็จแล้วให้รันคำสั่ง terminel ใน pycharm
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d261752937[/img]
[code]
python setup.py bdist_wheel
[/code]
ผลที่ได้
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d2704b743d[/img]
และจะมีไฟล์สร้างขึ้นมา
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d283b7a95e[/img]
ให้ทำการติดตั้ง twine
[code]
pip install twine
[/code]
ผลที่ได้
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d292f1df59[/img]
แล้วให้พิมพ์คำสั่ง
[code]
twine upload dist/*
[/code]
ขึ้นแบบนี้
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d29bbaf77f[/img]
แล้วทำการกรอก username/password ที่ได้สมัครไปเมื่อตอนแรก
ผลที่ได้
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d2ba1dda6e[/img]
เข้าไปดูในเว็บไซต์ pypi จะmodule library ที่เรา upload
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d2bfe38c5f[/img]
[img]http://snap.mindphp.com/index.php?view=2019Aug09_5d4d2f13276b4[/img]
และสามารถเรียกใช้ได้โดย
[code]
pip install test1234pypi
[/code]
อ้างอิง
https://medium.com/@joel.barmettler/how-to-upload-your-python-package-to-pypi-65edc5fe9c56
https://stackoverflow.com/questions/15746675/how-to-write-a-python-module-package
https://packaging.python.org/tutorials/packaging-projects/