10.1 PyPi是什麼?
PyPi 是 Python Package Index 的首字母簡寫,其實表示的是 Python 的 Packag 索引,這個也是 Python 的官方索引。
你需要的包(Package)基本上都可以從這裡面找到。作為開源軟體,你也希望能夠貢獻你的 Package 到這裡供其他使用者使用。我們舉個栗子,如果你希望你的 Python 程式能夠下載下傳金融資料,目前比較好用的金融資料來源是 Yahoo 和 Google。你可能需要讀取這 2 個平台的 API,然後做一個下載下傳部分的代碼,然後将這個代碼整合到自己的項目中。是不是好麻煩,這樣你可以到 PyPi 到上面去找找有沒有人已經寫過這個内容了,幸運的是,你真找到了,你找到了一個 package 叫做 yfinance。但是這個代碼在遠端,沒有在本地呀,怎麼用了?
你就需要将需要的内容從 PyPi 上下載下傳下來引用即可。
安裝子產品的指令:
pip install yfinance
複制
10.2 共享你的代碼
10.2.1 函數轉換為子產品
編寫代碼,随便寫了一個判斷奇數的函數
#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
# @Author : qa.yw
# @Time : 2021/3/30 11:19
# @File : isOdd.py
# @desc : 判斷奇數函數
'''
此函數判斷num是否屬于奇數
'''
def isOddNum(num):
if num % 2 == 0:
return False
else:
return True
複制
代碼儲存到isOdd.py檔案中。
10.2.2 準備釋出
為了共享isOdd.py這個子產品,需要準備一個釋出。在Python中,釋出(distribution)是指一個檔案集合,這些檔案聯合在一起允許你建構、打包、和釋出你的代碼。
- 為子產品建立一個檔案夾 isOdd,将isOdd.py複制到檔案夾。
- 在isOdd檔案夾中建立一個"setup.py"檔案,這個檔案包含有關釋出的中繼資料。
from distutils.core import setup
setup(
name='isOdd',
version='1.0.0',
py_modules=['isOdd'],
author='qa.yw',
author_email='[email protected]',
url='https://www.baidu.com',
description='A function judging odd'
)
複制
python3 setup.py check, 檢查setup.py檔案是否規範。
10.2.3 建構釋出
現在已經有一個檔案,裡面包含兩個檔案:子產品代碼在isOdd.py檔案,子產品的中繼資料在setup.py檔案。
- 建構一個釋出檔案,在isOdd檔案夾下打開一個終端,執行指令:python3 setup.py sdist
$ python3 setup.py sdist
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt, README.rst
writing manifest file 'MANIFEST'
creating isOdd-1.0.0
making hard links in isOdd-1.0.0...
hard linking isOdd.py -> isOdd-1.0.0
hard linking setup.py -> isOdd-1.0.0
creating dist
Creating tar archive
removing 'isOdd-1.0.0' (and everything under it)
複制
- 将釋出安裝到你的Python本地副本中,執行指令:sudo python3 setup.py install
$ sudo python3 setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying isOdd.py -> build/lib
running install_lib
copying build/lib/isOdd.py -> /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
byte-compiling /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/isOdd.py to isOdd.cpython-37.pyc
running install_egg_info
Writing /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/isOdd-1.0.0-py3.7.egg-info
複制
釋出介紹
利用Python釋出工具,你的子產品已經轉換為一個釋出,并且安裝到你的Python本地副本上。
MANIFEST-這個檔案包含釋出中的檔案清單
build檔案-新增的檔案夾
dist-釋出包
- 通過上述操作,你的子產品已經可以在本地引用了。
10.2.4 注冊PyPi網站
當然上述你的子產品僅僅隻是在本地可以引用,但是其他人還不能引用你的子產品,如果能讓全世界的人都能用你的子產品,你就要向PyPi上傳你的釋出。PyPI · The Python Package Index
10.2.5 向PyPi上傳代碼
方法1:
- 執行 python3 setup.py register
$ python3 setup.py register
running register
running check
We need to know who you are, so please choose either:
1. use your existing login,
2. register as a new user,
3. have the server generate a new password for you (and email it to you), or
4. quit
Your selection [default 1]:
1
Username: qa.yw
Password:
Registering isOdd to https://upload.pypi.org/legacy/
Server response (500): <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)>
複制
- 執行 python3 setup.py sdist upload
方法2:
使用twine上傳你的項目
pip3 install twine
複制
- python3 setup.py sdist
python3 setup.py sdist
複制
- twine upload dist/*
$ twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: qa.yw
Enter your password:
Uploading isOddNum-1.0.0.tar.gz
100%|███████████████████████████████████████████████████████████████████████████████████████| 3.49k/3.49k [00:02<00:00, 1.70kB/s]
View at:
https://pypi.org/project/isOddNum/1.0.0/
複制
- 安裝
$ pip3 install isOddNum
Requirement already satisfied: isOddNum in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (1.0.0)¥
複制
10.2.6 增加README.md
import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name='lib-name',
version='1.0',
author='',
author_email='',
description='',
long_description=long_description,
long_description_content_type="text/markdown",
url='',
packages=setuptools.find_packages(),
data_files=[('diractory',['file'])],
install_requires=[
'tensorflow>=2.2.0',
'keras>=2.4.0',
'numpy',
]
)
複制