想将自己写的python3 代码部署到另外一台VPS上,自己最常用的方式就是用`scp`复制了,但是总是感觉这样很繁琐也不优雅。你看别的项目用pip install安装多好呀。 ![](https://cdn.pixabay.com/photo/2017/06/02/14/38/package-2366468_960_720.jpg) (图源 :[pixabay](https://pixabay.com/)) 于是去学了一下怎么打包,怎么弄pip,发现网上的文章太多,并且很多都已经过时了,最后参考[Packaging Python Projects](https://packaging.python.org/tutorials/packaging-projects/),大致搞明白了怎么做,在这记录一下。 # 打包操作 打包操作就是把python项目放到一个安装包/分发包里,这样无论是传输还是安装使用时,都只需和一个文件打交道就好了,不必面对一大堆目录。 #### 安装支持工具 在进行打包之前,首先需要安装相应的支持工具,主要是`setuptools`与`wheel`。 >`python3 -m pip install --user --upgrade setuptools wheel` 我的系统中这两个已经存在了。 #### setup.py 之后就是弄好项目的目录结构以及创建setup.py。 项目的目录结构大致这样: >![image.png](https://images.hive.blog/DQmQRRP8ZYGJcC2oMk32HkhAHYh1kBvT4VupsLG2ZFqQus6/image.png) setup.py的内容以及各项内容的意义请参考这里:https://packaging.python.org/tutorials/packaging-projects/#creating-setup-py 除了setup.py外,弄个README.md以及LICENSE会让项目看起来更加正规与友好。当然了,像我这种自己用的小项目,其实没有这俩文件也没啥。 #### 生成分发包 做好上述工作以后,就可以生成分发包了。 生成分发包的命令如下: >`python3 setup.py sdist bdist_wheel` 可以用如下命令查看setup.py后边具体命令的解释: >`python3 setup.py --help-commands` 返回信息如下: >![image.png](https://images.hive.blog/DQmfHyJpYagcAQx4CZBVjcSsi7FfT9bBQQcGz4BEfdd5UXN/image.png) 所以我们知道`sdist`是创建源码分发包,而`bdist_wheel`是创建wheel格式分发包。 执行完成后,我们就会在dist目录下发现与`sdist`和`bdist_wheel`相对应的两个文件,这两个文件就可以用于安装/分发等操作啦。 # 发布项目 其实项目打包好,并且能安装,对我而言已经足够了,不过还是探索了一下发布项目的流程。 首先强调一点,仅仅是测试的话,应该使用[Test PyPI](https://test.pypi.org/)。以下内容以pypi为例,大部分同样适用于TestPyPI。 #### 创建API token 在上传项目之前,先创建API token。 访问如下链接: >https://pypi.org/manage/account/ 点击***`Add API token`*** >![image.png](https://images.hive.blog/DQmeaQdczMN4guVqRGmuS4Lz4oyhcSDgYF3dWf8ANGzdoWn/image.png) 起个名字: >![image.png](https://images.hive.blog/DQmSLAdxshu6gmN4YBeQSX9ad9SQKoHRzy597vgvKSEbgPj/image.png) 查看生成的token,主要保存好: >![image.png](https://images.hive.blog/DQmRp29UAC4ncr3eRcmRqEEMRBfJhM2MRKaM2fHpiQXXVdc/image.png) #### 使用Token pypi 上给出如下提示: >To use this API token: >* Set your username to __token__ >* Set your password to the token value, including the pypi- prefix 所以我们编辑`$HOME/.pypirc`文件,加入如下内容: ``` [pypi] username = __token__ password = pypi-xxxxxxxx ``` #### 上传项目 在上传项目之前,需要安装twine >`python3 -m pip install --user --upgrade twine` 关于[twine的介绍](https://packaging.python.org/key_projects/#twine)(主要解决加密上传的问题): >Twine is the primary tool developers use to upload packages to the Python Package Index or other Python package indexes. It is a command-line program that passes program files and metadata to a web API. Developers use it because it’s the official PyPI upload tool, it’s fast and secure, it’s maintained, and it reliably works. 然后使用如下指令就可以上传/发布项目啦: >`python3 -m twine upload --repository pypi dist/*` # 其它补充 上传项目可以上传源码版或者wheel版(或者其它版本),都可以用pip安装的,推荐wheel版。 setup.py文件还是挺复杂的,我现在还有点晕头转向,弄好还真不容易呢。关于setup.py更详细内容,可以参考:[Writing the Setup Script](https://docs.python.org/3.7/distutils/setupscript.html) # 参考链接 * [Python Packaging User Guide](https://packaging.python.org/) * [Packaging Python Projects](https://packaging.python.org/tutorials/packaging-projects/) * [Writing the Setup Script](https://docs.python.org/3.7/distutils/setupscript.html) * [Building and Distributing Packages with Setuptools](https://setuptools.readthedocs.io/en/latest/setuptools.html) * https://packaging.python.org/key_projects/#twine * https://test.pypi.org/ * https://packaging.python.org/guides/using-testpypi/
post_id | 86,009,275 |
---|---|
author | oflyhigh |
permlink | 6fcxmq-python |
category | cn |
json_metadata | {"tags":["cn","cutehive","cn-programming","python","setup","pypi"],"image":["https:\/\/cdn.pixabay.com\/photo\/2017\/06\/02\/14\/38\/package-2366468_960_720.jpg","https:\/\/images.hive.blog\/DQmQRRP8ZYGJcC2oMk32HkhAHYh1kBvT4VupsLG2ZFqQus6\/image.png","https:\/\/images.hive.blog\/DQmfHyJpYagcAQx4CZBVjcSsi7FfT9bBQQcGz4BEfdd5UXN\/image.png","https:\/\/images.hive.blog\/DQmeaQdczMN4guVqRGmuS4Lz4oyhcSDgYF3dWf8ANGzdoWn\/image.png","https:\/\/images.hive.blog\/DQmSLAdxshu6gmN4YBeQSX9ad9SQKoHRzy597vgvKSEbgPj\/image.png","https:\/\/images.hive.blog\/DQmRp29UAC4ncr3eRcmRqEEMRBfJhM2MRKaM2fHpiQXXVdc\/image.png"],"links":["https:\/\/pixabay.com\/","https:\/\/packaging.python.org\/tutorials\/packaging-projects\/","https:\/\/packaging.python.org\/tutorials\/packaging-projects\/#creating-setup-py","https:\/\/test.pypi.org\/","https:\/\/pypi.org\/manage\/account\/","https:\/\/packaging.python.org\/key_projects\/#twine","https:\/\/docs.python.org\/3.7\/distutils\/setupscript.html","https:\/\/packaging.python.org\/","https:\/\/setuptools.readthedocs.io\/en\/latest\/setuptools.html","https:\/\/packaging.python.org\/guides\/using-testpypi\/"],"app":"steemit\/0.2","format":"markdown"} |
created | 2020-05-15 04:04:06 |
last_update | 2020-05-15 04:04:06 |
depth | 0 |
children | 0 |
net_rshares | 63,891,127,234,127 |
last_payout | 2020-05-22 04:04:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 17.240 SBD |
curator_payout_value | 14.684 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 3,365 |
author_reputation | 1,148,153,621,496,884 |
root_title | 每天进步一点点:Python项目的打包与发布 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
adm | 0 | 30,120,964,857,019 | 100% | ||
thecryptodrive | 0 | 543,731,384,331 | 25% | ||
gerber | 0 | 81,135,610,294 | 6.8% | ||
kosimoos | 0 | 557,475,185 | 50% | ||
daan | 0 | 70,120,069,888 | 8% | ||
ezzy | 0 | 1,525,918,085,109 | 50% | ||
gidlark | 0 | 5,537,366,400 | 10% | ||
deanliu | 0 | 559,491,445,037 | 100% | ||
exyle | 0 | 829,594,214,336 | 10% | ||
sharker | 0 | 2,409,933,758 | 11% | ||
rea | 0 | 1,508,413,232,674 | 40% | ||
yogi | 0 | 10,880,630,858 | 25% | ||
joythewanderer | 0 | 53,872,227,044 | 50% | ||
lemooljiang | 0 | 608,845,884,835 | 88% | ||
ace108 | 0 | 269,553,008,292 | 25% | ||
kibela | 0 | 3,018,830,956 | 11% | ||
laoyao | 0 | 40,543,517,438 | 100% | ||
somebody | 0 | 9,769,767,930 | 100% | ||
skysunny | 0 | 125,619,266 | 88% | ||
midnightoil | 0 | 144,377,038,231 | 100% | ||
xiaohui | 0 | 803,578,576,069 | 100% | ||
jphamer1 | 0 | 1,993,669,075,414 | 100% | ||
oflyhigh | 0 | 1,268,616,431,802 | 100% | ||
yulan | 0 | 14,816,862,098 | 100% | ||
rivalhw | 0 | 413,767,563,018 | 100% | ||
nextgen622 | 0 | 652,069,878,751 | 100% | ||
helene | 0 | 438,314,202,150 | 100% | ||
ffcrossculture | 0 | 458,964,430,195 | 100% | ||
leduc1984 | 0 | 159,258,901 | 80% | ||
netaterra | 0 | 208,558,342,546 | 16% | ||
funnyman | 0 | 230,182,862,261 | 25% | ||
jang | 0 | 214,448,312,781 | 100% | ||
lexikon082 | 0 | 1,224,516,497 | 100% | ||
dapeng | 0 | 35,665,829,136 | 100% | ||
timool | 0 | 1,125,885,274 | 88% | ||
lucknie | 0 | 17,633,236,208 | 88% | ||
dumping | 0 | 1,768,047,625 | 88% | ||
elizacheng | 0 | 16,269,708,139 | 9% | ||
lordneroo | 0 | 2,123,101,249 | 6.46% | ||
teachblogger | 0 | 2,116,719,658 | 50% | ||
htliao | 0 | 23,849,957,771 | 20% | ||
curly-q | 0 | 2,257,663,107 | 100% | ||
nuagnorab | 0 | 77,642,483,407 | 20% | ||
eval | 0 | 729,914,629 | 100% | ||
maxer27 | 0 | 26,724,325,748 | 20% | ||
blacklux | 0 | 14,038,843,124 | 25% | ||
techken | 0 | 85,481,667 | 0.48% | ||
susanli3769 | 0 | 147,211,344,131 | 100% | ||
runicar | 0 | 21,713,548,964 | 6.46% | ||
otom | 0 | 7,871,365,470 | 20% | ||
dado13btc | 0 | 694,268,664 | 6.46% | ||
travelgirl | 0 | 62,457,687,056 | 40% | ||
catwomanteresa | 0 | 59,785,887,995 | 50% | ||
nicolemoker | 0 | 16,250,435,604 | 24% | ||
oldman28 | 0 | 21,080,167,200 | 48% | ||
liangfengyouren | 0 | 3,359,142,742 | 50% | ||
sailor01 | 0 | 4,652,790,538 | 100% | ||
cn-reader | 0 | 5,378,437,077 | 50% | ||
pardeepkumar | 0 | 3,875,926,884 | 6.46% | ||
tvb | 0 | 19,138,833,512 | 30% | ||
skenan | 0 | 6,394,370,248 | 50% | ||
rocky1 | 0 | 3,964,845,119,435 | 4% | ||
kimzwarch | 0 | 7,545,020,425 | 4% | ||
bboyady | 0 | 853,780,762 | 5% | ||
saltiberra | 0 | 1,129,400,007 | 10% | ||
davidke20 | 0 | 11,962,685,045 | 10% | ||
rosatravels | 0 | 29,305,289,917 | 5% | ||
robertoueti | 0 | 1,807,238,195 | 3% | ||
roleerob | 0 | 6,099,149,223 | 2.04% | ||
arcnet | 0 | 92,708,579 | 100% | ||
xiaoshancun | 0 | 24,851,570,484 | 100% | ||
chaisnaturalprod | 0 | 184,998,919 | 100% | ||
toocurious | 0 | 6,780,231,328 | 6.73% | ||
metten | 0 | 9,435,316,738 | 88% | ||
pataty69 | 0 | 27,871,517,471 | 20% | ||
fknmayhem | 0 | 724,582,507 | 3.4% | ||
blockbrothers | 0 | 13,307,083,307 | 5% | ||
cn-book | 0 | 978,562,323 | 88% | ||
emrebeyler | 0 | 31,170,728,029 | 6.8% | ||
windowglass | 0 | 211,220,966,545 | 30% | ||
cnbuddy | 0 | 1,802,578,334 | 0.1% | ||
cn-movie | 0 | 108,872,176 | 88% | ||
etherpunk | 0 | 1,420,317,176 | 25% | ||
ocupation | 0 | 6,290,067,200 | 6.46% | ||
itchyfeetdonica | 0 | 110,472,909,214 | 50% | ||
vivia | 0 | 3,026,462,240 | 88% | ||
enjoyinglife | 0 | 2,307,823,650 | 6.46% | ||
fourfourfun | 0 | 1,679,697,240 | 6.25% | ||
alauddinsee | 0 | 103,743,907,379 | 100% | ||
girolamomarotta | 0 | 33,662,046,083 | 50% | ||
yakubenko | 0 | 30,819,572,652 | 100% | ||
mmmmkkkk311 | 0 | 447,641,364,714 | 100% | ||
johnhooi | 0 | 9,876,862 | 10% | ||
nealmcspadden | 0 | 89,607,002,591 | 6.8% | ||
maiyude | 0 | 136,287,089,399 | 50.04% | ||
suhunter | 0 | 946,636,979 | 50% | ||
xiaoli | 0 | 283,805,118 | 88% | ||
yumisee | 0 | 83,536,211 | 10% | ||
casberp | 0 | 4,557,459,707 | 3% | ||
joeliew | 0 | 309,122,477 | 10% | ||
investprosper | 0 | 1,958,197,310 | 3.4% | ||
hkit98 | 0 | 0 | 10% | ||
elvinmas001 | 0 | 0 | 10% | ||
watersoo | 0 | 283,113,715 | 10% | ||
vamos-amigo | 0 | 271,225,402 | 10% | ||
cadawg | 0 | 5,640,850,908 | 4.75% | ||
sudefteri | 0 | 13,943,076,972 | 100% | ||
happy-soul | 0 | 32,061,193,724 | 10% | ||
iipoh06 | 0 | 7,862,826 | 10% | ||
annabellenoelle | 0 | 3,605,582 | 10% | ||
mindtrap | 0 | 31,906,603,571 | 6.46% | ||
totopapa | 0 | 30,960,791,870 | 100% | ||
yethui | 0 | 24,701,039 | 10% | ||
riczlook | 0 | 11,785,580 | 10% | ||
ethanlee | 0 | 4,000,339,805 | 100% | ||
andrewnoel | 0 | 0 | 10% | ||
fishbb | 0 | 1,425,614,385 | 25.02% | ||
fredo77200 | 0 | 3,665,154,696 | 100% | ||
bestboom | 0 | 2,888,598,884 | 6.8% | ||
onepercentbetter | 0 | 1,416,619,448 | 0.5% | ||
abrockman | 0 | 11,415,912,829 | 3.4% | ||
ryenneleow | 0 | 4,513,330,004 | 90% | ||
therising | 0 | 4,443,879,816,516 | 20% | ||
fishdd | 0 | 1,020,324,005 | 25.02% | ||
garrettwallace | 0 | 1,364,307,266 | 18% | ||
payroll | 0 | 1,294,208,519,684 | 10% | ||
jaydih | 0 | 242,304,858,730 | 5% | ||
andrewma | 0 | 5,040,444,036 | 20% | ||
promobot | 0 | 50,685,032,038 | 25% | ||
hranhuk | 0 | 6,347,030,250 | 40% | ||
kimseun | 0 | 33,437,314,453 | 5% | ||
mynotsofitlife | 0 | 916,677,310 | 10% | ||
pladozero | 0 | 18,638,883,600 | 10% | ||
nateaguila | 0 | 76,640,020,559 | 8% | ||
hmayak | 0 | 23,557,376,478 | 100% | ||
archisteem | 0 | 4,710,279,473 | 7.5% | ||
cn-malaysia | 0 | 2,908,018,946 | 10% | ||
merlion | 0 | 146,731,047 | 0.5% | ||
moneybaby | 0 | 830,610,279 | 2.5% | ||
kanadaramagi123 | 0 | 22,749,453,553 | 5% | ||
abcallen | 0 | 16,454,482,895 | 80% | ||
loveecho | 0 | 42,067,577,439 | 5% | ||
bambugrove | 0 | 545,334,997 | 100% | ||
money-way | 0 | 555,397,281 | 100% | ||
angelslake | 0 | 8,635,503,771 | 5% | ||
yestermorrow | 0 | 8,996,695,273 | 31% | ||
dalz | 0 | 7,390,281,502 | 10% | ||
blues-wclouds | 0 | 1,752,009,336 | 100% | ||
julian2013 | 0 | 6,947,598,608 | 5% | ||
dlike | 0 | 73,145,395,363 | 2.8% | ||
triptolemus | 0 | 6,628,916,646 | 6.8% | ||
voxmortis | 0 | 52,822,876,612 | 20% | ||
ataliba | 0 | 5,034,083,067 | 40% | ||
wordit | 0 | 8,360,082,347 | 5% | ||
munhenhos | 0 | 2,002,189,353 | 20% | ||
sj-jeong | 0 | 269,040,877,174 | 5% | ||
wondumyungga | 0 | 18,522,575,609 | 5% | ||
bobby.madagascar | 0 | 4,767,260,316 | 3.4% | ||
aquawink | 0 | 21,307,883,115 | 5% | ||
jamzmie | 0 | 930,467,497 | 15% | ||
merlin7 | 0 | 32,417,642,844 | 6.8% | ||
sasaadrian | 0 | 32,202,156,784 | 20% | ||
pollygonewild | 0 | 0 | 10% | ||
ringit | 0 | 14,233,806,160 | 5% | ||
new-steemit | 0 | 5,626,633,371 | 10% | ||
coreabeforekorea | 0 | 173,182,589,661 | 5% | ||
talkative-bk | 0 | 215,606,129,370 | 5% | ||
starrouge | 0 | 1,038,593,131 | 50% | ||
zerofive | 0 | 830,059,358 | 50% | ||
samsemilia7 | 0 | 7,043,181,330 | 40% | ||
primeradue | 0 | 2,273,192,201 | 6.8% | ||
steemegg | 0 | 769,092,544 | 2.5% | ||
cnstm | 0 | 907,975,286,353 | 100% | ||
permaculturedude | 0 | 1,315,163,090 | 3.4% | ||
likuang007 | 0 | 36,956,606,303 | 100% | ||
jacoblai | 0 | 0 | 10% | ||
lianjingmedia | 0 | 979,415,938 | 100% | ||
bobdavids | 0 | 0 | 10% | ||
ladyyunajay | 0 | 3,468,099,767 | 90% | ||
chocolatelover | 0 | 3,997,662,554 | 40% | ||
instagram-models | 0 | 23,096,297,684 | 4% | ||
cecilian | 0 | 7,692,267,324 | 99.62% | ||
hungrybear | 0 | 113,782,281 | 0.5% | ||
electrodo | 0 | 71,423,697,882 | 10% | ||
hungryharish | 0 | 4,617,822,953 | 100% | ||
elektroyazilim | 0 | 618,269,463 | 60% | ||
cnbuddy-cnstm | 0 | 24,017,748,391 | 100% | ||
kryptogames | 0 | 106,068,384,717 | 20% | ||
lovelemon | 0 | 59,044,065,126 | 100% | ||
theinspiration | 0 | 535,716,093 | 100% | ||
epic4chris | 0 | 550,238,449 | 100% | ||
rj-photo | 0 | 1,431,734,617 | 100% | ||
blockchainpeople | 0 | 17,266,541 | 3% | ||
whatdidshewear | 0 | 32,372,182,316 | 5% | ||
sjgod4018 | 0 | 104,083,819,154 | 5% | ||
mustard-seed | 0 | 16,342,206,495 | 5% | ||
morwen | 0 | 18,475,661,380 | 11% | ||
likwid | 0 | 4,728,153,322,967 | 25% | ||
diceshark | 0 | 205,722,068 | 50% | ||
moneyman19 | 0 | 536,375,409 | 100% | ||
moonhodler | 0 | 1,946,803,200 | 20% | ||
goldentouch | 0 | 176,977,695 | 50% | ||
steem.leo | 0 | 32,226,139,492 | 6.73% | ||
leo.voter | 0 | 47,753,278,406 | 6.8% | ||
hyborian-strain | 0 | 1,984,029,898 | 30% | ||
perrymine | 0 | 843,262,068 | 5% | ||
freddio.sport | 0 | 1,064,211,075 | 15% | ||
wenjiewu | 0 | 97,706,638 | 25% | ||
leo.syndication | 0 | 1,201,759,122 | 6.8% | ||
one.life | 0 | 4,728,432,249 | 6.78% | ||
maxuvd | 0 | 2,881,865,574 | 6.8% | ||
teresa.yaiyai | 0 | 7,997,725,001 | 100% | ||
therealyme | 0 | 1,758,637,527 | 8% | ||
jaykayw | 0 | 11,578,329,004 | 5% | ||
cnbuddy-reward | 0 | 648,471,138 | 0.1% | ||
sacrosanct | 0 | 564,606,344 | 6.46% | ||
friendlystranger | 0 | 271,897,731,525 | 16% | ||
starnote | 0 | 203,492,648 | 88% | ||
reward.tier1 | 0 | 576,974,189 | 100% | ||
spx | 0 | 102,235,381,498 | 100% | ||
arrit | 0 | 36,974,994,970 | 100% | ||
oblivioncubed | 0 | 537,094,888 | 10% | ||
simply2koool | 0 | 10,665,335,738 | 25% | ||
ribary | 0 | 408,461,388,275 | 33% | ||
razer7 | 0 | 280,839,717,772 | 28% | ||
aiplusfinance | 0 | 1,237,290,013 | 100% | ||
moochain.net | 0 | 205,115,504 | 88% | ||
urduhaqaiq | 0 | 547,403,115 | 100% | ||
madeehaali | 0 | 542,237,995 | 100% | ||
behzad2pk | 0 | 536,879,163 | 100% | ||
bcm.leo | 0 | 1,020,432,324 | 3.4% | ||
steemcityrewards | 0 | 16,699,684,527 | 6.8% | ||
dev.supporters | 0 | 54,589,073,236 | 10% | ||
openbazzar | 0 | 0 | 88% | ||
littleksroad | 0 | 549,104,441 | 50% | ||
akimik | 0 | 0 | 100% |