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= '2283320260@qq.com', # 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