全文参考:https://packaging.python.org/specifications/core-metadata,照做基本都可以成功。
使用python时,经常会用到一些第三方模块,安装第三方的模块使用pip install 模块名称”,其实也可以自己整个模块,让别人安装后直接可以使用,就会方便很多,同时也高大上了不少。
1、安装软件:
pip3 install --user --upgrade twine setuptools wheel
2、注册python 帐号,需要邮箱验证
https://pypi.org/
3、准备目录
mkdir gsc_package
4、建立项目目录
cd gsc_package && mkdir xxlGsc
cd xxlGsc
5、创建核心文件xxlGsc.py和初始化文件__init__.py在xxlGsc目录:
__init__.py,内容:
1 from __future__ import absolute_import 2 from .xxlGsc import * 3 4 name = "xxlGsc"
xxlGsc.py,内容:
1 #!/usr/bin/env python 2 def output(): 3 print("星宿老仙,法力无边")
6、创建辅助文件,在gsc_package:
LICENSE,内容是我随意发挥的,请根据实际情况,自己写吧:
1 这只是个演示而已,不要过于较真
README.md,内容是我随意发挥的,请根据实际情况,自己写吧:
1 #xxlGSC 2 这个模块老牛拜了,星宿老仙,法力无边
setup.py,内容基本是抄来的,哈哈,请根据实际情况,自己该吧:
1 #!/usr/bin/env python 2 import setuptools 3 4 with open("README.md", "r") as fh: 5 long_description = fh.read() 6 7 setuptools.setup( 8 name="xxlGsc", 9 version="0.01", 10 author="GSC", 11 author_email="guochaoxxl@163.com", 12 description="xing xiu lao xian, fa li wu bian!", 13 long_description=long_description, 14 long_description_content_type="text/markdown", 15 url="https://github.com/guochaoxxl/learnPython", 16 packages=setuptools.find_packages(), 17 classifiers=[ 18 "Programming Language :: Python :: 3", 19 "License :: OSI Approved :: MIT License", 20 "Operating System :: OS Independent", 21 ], 22 )
基本就这些了吧。
7、python打包,进入包目录gsc_package,执行:
python setup.py sdist bdist_wheel
1 /usr/lib/python3.9/site-packages/setuptools/dist.py:461: UserWarning: Normalizing '0.01' to '0.1' 2 warnings.warn(tmpl.format(**locals())) 3 running sdist 4 running egg_info 5 writing xxlGsc.egg-info/PKG-INFO 6 writing dependency_links to xxlGsc.egg-info/dependency_links.txt 7 writing top-level names to xxlGsc.egg-info/top_level.txt 8 reading manifest file 'xxlGsc.egg-info/SOURCES.txt' 9 writing manifest file 'xxlGsc.egg-info/SOURCES.txt' 10 running check 11 creating xxlGsc-0.1 12 creating xxlGsc-0.1/xxlGsc 13 creating xxlGsc-0.1/xxlGsc.egg-info 14 copying files to xxlGsc-0.1... 15 copying README.md -> xxlGsc-0.1 16 copying setup.py -> xxlGsc-0.1 17 copying xxlGsc/__init__.py -> xxlGsc-0.1/xxlGsc 18 copying xxlGsc/xxlGsc.py -> xxlGsc-0.1/xxlGsc 19 copying xxlGsc.egg-info/PKG-INFO -> xxlGsc-0.1/xxlGsc.egg-info 20 copying xxlGsc.egg-info/SOURCES.txt -> xxlGsc-0.1/xxlGsc.egg-info 21 copying xxlGsc.egg-info/dependency_links.txt -> xxlGsc-0.1/xxlGsc.egg-info 22 copying xxlGsc.egg-info/top_level.txt -> xxlGsc-0.1/xxlGsc.egg-info 23 Writing xxlGsc-0.1/setup.cfg 24 Creating tar archive 25 removing 'xxlGsc-0.1' (and everything under it) 26 running bdist_wheel 27 running build 28 running build_py 29 installing to build/bdist.linux-x86_64/wheel 30 running install 31 running install_lib 32 creating build/bdist.linux-x86_64/wheel 33 creating build/bdist.linux-x86_64/wheel/xxlGsc 34 copying build/lib/xxlGsc/__init__.py -> build/bdist.linux-x86_64/wheel/xxlGsc 35 copying build/lib/xxlGsc/xxlGsc.py -> build/bdist.linux-x86_64/wheel/xxlGsc 36 running install_egg_info 37 Copying xxlGsc.egg-info to build/bdist.linux-x86_64/wheel/xxlGsc-0.1-py3.9.egg-info 38 running install_scripts 39 adding license file "LICENSE" (matched pattern "LICEN[CS]E*") 40 creating build/bdist.linux-x86_64/wheel/xxlGsc-0.1.dist-info/WHEEL 41 creating 'dist/xxlGsc-0.1-py3-none-any.whl' and adding 'build/bdist.linux-x86_64/wheel' to it 42 adding 'xxlGsc/__init__.py' 43 adding 'xxlGsc/xxlGsc.py' 44 adding 'xxlGsc-0.1.dist-info/LICENSE' 45 adding 'xxlGsc-0.1.dist-info/METADATA' 46 adding 'xxlGsc-0.1.dist-info/WHEEL' 47 adding 'xxlGsc-0.1.dist-info/top_level.txt' 48 adding 'xxlGsc-0.1.dist-info/RECORD' 49 removing build/bdist.linux-x86_64/wheel
我这里的过程,
8、上传:
twine upload dist/*
这个过程才会检查setup.py,有错误就会停下,直到修改无误后,才会上传成功。
1 Uploading distributions to https://upload.pypi.org/legacy/ 2 Enter your username: guochaoxxl 3 Enter your password: 4 Uploading xxlGsc-0.1-py3-none-any.whl 5 100%|███████████████████████████████████████████| 5.72k/5.72k [00:04<00:00, 1.32kB/s] 6 Uploading xxlGsc-0.1.tar.gz 7 100%|███████████████████████████████████████████| 4.80k/4.80k [00:03<00:00, 1.23kB/s] 8 9 View at: 10 https://pypi.org/project/xxlGsc/0.1/
到此,方算安装成功。
9、让别人安装,和其他第三方模块一样的方式:
pip install xxlGsc
1 Defaulting to user installation because normal site-packages is not writeable 2 Collecting xxlGsc 3 Downloading xxlGsc-0.1-py3-none-any.whl (2.7 kB) 4 Installing collected packages: xxlGsc 5 Successfully installed xxlGsc-0.1
表示成功安装
10、应用模块;
打开python环境,终端下输入python
Python 3.9.1 (default, Dec 13 2020, 11:55:53) [GCC 10.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import xxlGsc >>> xxlGsc.output() 星宿老仙,法力无边 >>> exit() #退出python
是不是非常简单,赶紧试试吧