先注冊pypi賬號
去pypi官網注冊賬號
創建要打包的模塊,目錄結構如下:
packaging_tutorial 打包入口 ├── LICENSE 證書 **重要, https://choosealicense.com/ ├── README.md 文檔 ├── example_pkg 實際包名,from example_pkg import xxxx └── __init__.py
└── demo01.py 實際代碼程序文件
├── setup.py 打包腳本
└── tests 用於單元測試
安裝打包模塊:
安裝setuptools
這個模塊, 用來打包python模塊, 當然其實還有其他的工具, 這里只介紹這一種, 感興趣的話可以自行搜索.
pip install --upgrade setuptools
創建LICENSE文件,該文件可有可無
Copyright (c) [year] [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文件,該文件可有可無
# 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.
創建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版本和系統類型 "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'] # 我們的模塊所用到的依賴, 這里指定的話, 用戶安裝你的模塊時, 會自動安裝這些依賴 )
生成distribution archives(可分發歸檔包),在setup.py文件目錄下執行
python setup.py sdist bdist_wheel # 前者是tar.gz文件, 后者是構建.whl文件
該命令執行成功后,會生成三個目錄:module.egg-info、build、dist,我們進入到dist目錄下會看到生成.tra.gz和.whl后綴的文件
上傳歸檔到PyPI網站
①:使用twine交互式上傳,先安裝 pip install --upgrade twine twine upload dist/* # 在dist/目錄下執行該命令
上傳的過程中,會提示輸入pypi賬號密碼,輸入后等待上傳成功
②:檢驗是否上傳成功
pip install example-pkg
如果不想每次打包輸入pypi賬號和密碼,在當前操作用戶目錄下面新建文件【.pypirc】文件,文件內容如下:
注:mac進入到/users目錄下,再comman+shift+. 查看隱藏文件和目錄
[distutils]
index-servers=pypi
[pypi]
repository = https:
//upload.pypi.org/legacy/
username: shenzhiming注冊的pypi賬號
password: xxx注冊的pypi密碼