平時用別人模塊用的爽了, 有時候想自己寫個模塊上傳到pypi, 然后用的時候pip安裝. 這次簡單介紹一下如何打包自己的python模塊并上傳到pypi.
整體分以下幾步:
- 注冊pypi賬號
- 打包python模塊
- 上傳
然后就可以用pip直接安裝啦, 怎么樣是不是很炫酷!
好了, 就介紹到這里吧.

1. 注冊pypi賬號
去pypi官網注冊一個賬號

2. 打包python模塊
安裝setuptools這個模塊, 用來打包python模塊, 當然其實還有其他的工具, 這里只介紹這一種, 感興趣的話可以自行搜索.
>>> pip install --upgrade setuptools
項目目錄結構
project (總目錄)
module(模塊目錄)
__init__.py
LICENSE.txt(證書)
README.md(文檔)
setup.py(打包腳本)
各個文件/目錄說明
關于module目錄:
把相關模塊寫在這個下面就行, module是你自己的模塊名, 我這里把函數寫在init.py中, 這樣我們的模塊上傳之后, 可以直接import. 比如說init.py中有個函數為fun, 那么我們就可以這樣使用:
from module import fun
fun()
另外這里要注意一個細節(jié), 由于我們打包的時候setup.py是在上一級的目錄中, 所以, 如果這一層想要引用同一目錄下面的其他函數, (比如說我們有個utils.py的文件, 我們在里面寫了一個get_md5()的函數) , 如果要引用get_md5需要這樣寫
from module.utils import get_md5
LICENSE.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.
setup.py
這個文件是一定要有的, 它是安裝的必要文件, 具體的東西看下面的代碼, 東西不多. 自己按照需求填寫就好.
"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""
# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path
here = path.abspath(path.dirname(__file__))
# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
# Arguments marked as "Required" below must be included for upload to PyPI.
# Fields marked as "Optional" may be commented out.
setup(
name='這里是你的模塊名字, 和你的module目錄名保持一致',
version='版本號(每次更新新的版本這里需要遞增)',
author='作者名',
author_email='你的郵箱',
description='模塊描述',
long_description=long_description, # 這里是文檔內容, 讀取readme文件
long_description_content_type='text/markdown', # 文檔格式
packages=find_packages(),
classifiers=[ #這里我們指定證書, python版本和系統(tǒng)類型
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6', # 這里指定python版本號必須大于3.6才可以安裝
install_requires=['pprint', 'tablestore', 'prettytable'] # 我們的模塊所用到的依賴, 這里指定的話, 用戶安裝你的模塊時, 會自動安裝這些依賴
)
README.md
這個是文檔, 會顯示在pypi上你的模塊的主頁, 最好寫的詳細一些. 免得用的時候一頭霧水.
打包
在setup.py的目錄下, 執(zhí)行:
python setup.py bdist_wheel
這個命令會生成一個wheel包, pip可以直接安裝. 執(zhí)行成功后, 會生成三個目錄, module.egg-info , build, dist, 這里我們進入到dist目錄中, 會看到有一個后綴為.whl的文件, 這就是最后生成的安裝包了, 我們其實可以直接pip install這個文件. 當然, 這里我們選擇上傳到pypi上供其他人使用.
- 上傳
上傳使用twine上傳, 先安裝
>>> pip install twine
在我們剛剛的dist目錄執(zhí)行:
twine upload 你打包好的模塊.whl
然后會提示你輸入你的pypi賬號和密碼, 輸入完等待上傳成功就ok了.