distutils 实现对package 包的发布
import math def showMsg(a): return a * a * a a = 10 print('%d 的三次方是 %d' % (a, showMsg(a)))
1. 在同级目录下建立setup.py
# encoding=utf-8 from distutils.core import setup,Extension # 打包软件脚本文件必须采用 setup 名称 # 打包函数 setup( name='package', # 安装包名 version='1.0', # 打包安装软件的版本号 description="实现对数的三次方运算", long_description="实现对数的三次方运算", author= 'feiquan123', author_email= '[email protected]', # maintainer="None", # 提供与包相关的其他维护者的名字 # maintainer_email="None", # 其他维护者的邮箱 url="", # 包相关网站主页的的访问地址 download_url="", # 下载安装包(zip , exe)的url keywords="math", py_modules=['package'], # 设置打包模块,可以多个 # 对于C,C++,Java 等第三方扩展模块一起打包时,需要指定扩展名、扩展源码、以及任何编译/链接 要求(包括目录、链接库等) ext_modules = [Extension('data',['data.c'])], )
注意:如果你的setup.py 中包含中文字符,第一行的代码必须写
如何扩展和嵌入 Python 解释器 : https://docs.python.org/zh-cn/3/extending/index.html
2. 编写安装配置文件 setup.cfg
[sdist]
dist-dir = source
dist-dir : 指定发布源码的路径,默认 dist
如何编写setup.cfg:
Common commands: (see '--help-commands' for more) setup.py build will build the package underneath 'build/' setup.py install will install the package Global options: --verbose (-v) run verbosely (default) --quiet (-q) run quietly (turns verbosity off) --dry-run (-n) don't actually do anything --help (-h) show detailed help message --no-user-cfg ignore pydistutils.cfg in your home directory Options for 'sdist' command: --template (-t) name of manifest template file [default: MANIFEST.in] --manifest (-m) name of manifest file [default: MANIFEST] --use-defaults include the default file set in the manifest [default; disable with --no-defaults] --no-defaults don't include the default file set --prune specifically exclude files/directories that should not be distributed (build tree, RCS/CVS dirs, etc.) [default; disable with --no-prune] --no-prune don't automatically exclude anything --manifest-only (-o) just regenerate the manifest and then stop (implies --force-manifest) --force-manifest (-f) forcibly regenerate the manifest and carry on as usual. Deprecated: now the manifest is always regenerated. --formats formats for source distribution (comma-separated list) --keep-temp (-k) keep the distribution tree around after creating archive file(s) --dist-dir (-d) directory to put the source distribution archive(s) in [default: dist] --medata-check Ensure that all required elements of meta-data are supplied. Warn if any missing. [default] --owner (-u) Owner name used when creating a tar file [default: current user] --group (-g) Group name used when creating a tar file [default: current group] --help-formats list available distribution formats usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: setup.py --help [cmd1 cmd2 ...] or: setup.py --help-commands or: setup.py cmd --help
3. 发布软件 (压缩包)
linux : python setup.py sdist
windows : setup.py sdist
指定发布格式,同时生成两个压缩包: python setup.py sdist --formats=gztar,zip
windows exe : python setup.py bdist_wininst
--formats:
zip -> .zip gztar -> .tar.gz bztar -> .tar.bz2 ztar -> .tar.Z tar -> .tar
4. 安装源码包,然后你就可以导入了
解压后cd 到解压目录
安装命令 python setup.py install
或者安装时保存安装日志: python setup.py install --record log
5. 安装后删除
1. 安装时记录日志 python setup.py install --record log 2. windows : for /F %i in (log) do del %i linux : cat log | xagrs rm -rf
其中: log文件内容是安装目录:
E:\...\Lib\site-packages\package.py E:\...\Lib\site-packages\__pycache__\package.cpython-37.pyc E:\...\Lib\site-packages\package-1.0-py3.7.egg-info