第六章 分發(fā)
利用工具生成自己的setup.py,現(xiàn)有的5類工具:
1 distutils是標(biāo)準庫的一部分,能處理簡單的包的安裝。
2 setuptools 領(lǐng)先的包安裝標(biāo)準,曾經(jīng)被廢棄但現(xiàn)在又繼續(xù)開發(fā)。
3 distribute 從0.7版本開始并入了setiptools。
4 distutils2 (也稱作packaging)已經(jīng)被廢棄。
5 distlib將可能會來取代distutils。
寫到這里,突然感覺蛋蛋的憂傷,這本書實際上作者出發(fā)的角度,這一章,哦 ,你已經(jīng)會打包了,然后說明下分包。
這就很為難我了,那就只能是我來思考,補全,讓我自己能學(xué)的下去了。
setuptools,可以幫助我們創(chuàng)建跟分發(fā)包,作為當(dāng)前情況下,應(yīng)該是可行性最高的方案,作為分發(fā)庫的主要選擇。
那么原理是什么?通過寫setup.py來把當(dāng)前目錄下的目錄文件打成成壓縮格式,用于分發(fā)。
1 tar.gz格式:這個就是標(biāo)準壓縮格式,里面包含了項目元數(shù)據(jù)和代碼,可以使用python setup.py sdist命令生成。
2.egg格式:這個本質(zhì)上也是一個壓縮文件,只是擴展名換了,里面也包含了項目元數(shù)據(jù)以及源代碼。這個格式由setuptools項目引入,需要安裝才能運行??梢酝ㄟ^命令python setup.py bdist_egg命令生成。
3.whl格式:這個是Wheel包,也是一個壓縮文件,只是擴展名換了,里面也包含了項目元數(shù)據(jù)和代碼,還支持免安裝直接運行。可以通過命令python setup.py bdist_wheel生成.
1 安裝setuptools ,不過python3 以上版本已經(jīng)自帶這個庫了
pip install setuptools
2 創(chuàng)建一個包
接著在該包里面創(chuàng)建setup.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup,find_packages
setup(
name = "demo",
version = "0.1",
author="qitian",
author_email="xzwgm81@163.com",
description = "make in chinese",
url = "https://www.baidu.com/",
packages = find_packages(),
)
這里如果不知道應(yīng)該寫什么參數(shù),可以使用命令
python setup.py check
然后查看warning 就知道自己差了什么參數(shù)了
3 創(chuàng)建被分發(fā)的py包
目錄 ceshiyong666
下面建立2個文件,init.py,tianzhidao.py
再tianzhidao.py里面寫上
#!/usr/bin/env python
#-*- coding:utf-8 -*-
def test():
print("hello world! let‘s go’")
if __name__ == '__main__':
test()
4 運行命令生成壓縮包
python setup.py bdist_egg
分別生成了3個目錄:build,dist,demo.egg-info
自己查看build,demo.egg-info,確定生成物品就行
5
可以運行命令實驗
安裝命令 python setup.py install
接著進入環(huán)境python終端
>>> from ceshiyong666 import tianzhidao
>>> tianzhidao.test()
hello world! let‘s go
>>>
總算是寫完第一節(jié)的setuptools了,par我就跳過去了,個人感覺越是依賴于高整合性的工具,個人的創(chuàng)造力會越低吧。
開始上傳,
第一步 先注冊一個賬號
第二部創(chuàng)建文件
文件名就是“.pypirc”,創(chuàng)建在“C:\Users\Administrator”(所謂的根目錄)。重復(fù)一次文件名就是“.pypirc”,名字+后綴名就是“.pypirc”
[distutils]
index-servers =
testpypi
[testpypi]
username = <your username>
passwors = <your password>
repository = https://testpypi.python.prg/pypi
接著打命令行
python setup.py register -r testpypi
接著有趣的一點出現(xiàn)了返回系統(tǒng)
Registering demo to https://testpypi.python.org/pypi
Server response (308): Permanent Redirect
WARNING: Registering is deprecated, use twine to upload instead (https://pypi.org/p/twine/)
!服務(wù)器倒閉了!哈哈哈哈哈哈,也不對應(yīng)該說這種方式被徹底放棄了、
重新開始上傳步驟
第一步先注冊pypi服務(wù)器的賬號密碼,跟test的賬號密碼
https://pypi.org/account/register/
https://test.pypi.org
然后下載最新版本的twine庫
pip install -U twine
測試上傳到test測試服務(wù)器是否成功
python setup.py sdist bdist_wheel
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
之后可以上傳到主服務(wù)器pypi
twine upload dist/*
這樣為止分發(fā)基本算是結(jié)束了
結(jié)果上傳成功
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: qitianbenben
Enter your password:
Uploading shijietaidaceshi-0.1-py3.7.egg
100%|████████████████████████████████████████████████████████████████
100%|████████████████████████████████████████████████████████████████
████████████████████████████| 4.57k/4.57k [00:08<00:00, 542B/s]
Uploading shijietaidaceshi-0.1.tar.gz
100%|████████████████████████████████████████████████████████████████
██████████████████████████| 3.49k/3.49k [00:01<00:00, 2.62kB/s]
View at:
https://pypi.org/project/shijietaidaceshi/0.1/
下載也成功了
(venv) C:\Users\Administrator\PycharmProjects\lianxi5>pip install shijietaidaceshi
Collecting shijietaidaceshi
Downloading https://files.pythonhosted.org/packages/a3/c0/06d97365ef281d676feda92831e46aa3e6fc22f35e50b9e472e4f25ff8ee/shijietaidac
eshi-0.1.tar.gz
Installing collected packages: shijietaidaceshi
Running setup.py install for shijietaidaceshi ... done
Successfully installed shijietaidaceshi-0.1
如果想下載測試服務(wù)器上的話,打下面命令
pip install -i https://test.pypi.org/legacy/ shijietaidaceshi
后綴為庫名
400錯誤.這里出現(xiàn)了一個問題 他要我先驗證這賬號郵箱。。。
這里可以很清楚地看出pypi安全性為0.。。。。好無語
到此setuptools上傳分發(fā)結(jié)束
附上一份par打包上傳 資料
https://blog.csdn.net/u010571844/article/details/50498167
https://www.cnblogs.com/CaesarLinsa/p/pbr.html
之后再繼續(xù)第六章的學(xué)習(xí)吧,有點多啊
參考資料
官網(wǎng) :https://pypi.org/p/twine/
setuptools詳解:http://www.itdecent.cn/p/ea9973091fdf
文件的創(chuàng)建:https://my.oschina.net/u/3530220/blog/1608696