使用distutils发布模块

本文详细介绍了如何使用distutils工具发布Python模块,包括创建setup.py文件、设置参数如platforms、license、py_modules和packages,以及打包格式如sdist和bdist。讲解了各种打包格式选项,如zip、gztar、bztar等,并提供了获取帮助和具体使用案例。文章还涵盖了安装过程中的自定义路径和深度定制安装选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用distutils发布模块

1.将代码文件组织到模块容器中
2.准备一个README或REMDME.txt文件
3.而后在容器中创建setup.py文件
setup文件的内容:
在这里插入图片描述

setup.py常用参数

在这里插入图片描述
基于上图:
对setup.py中setup()参数补充

platforms:平台参数
license:许可证
py_modules:各模块名称组成的列表,此些模块可能位于包的 根目录下(modname),也可能位于某子包目录中(subpkg1.modname);
packages:各子包名称的列表

大体分为两类:
元数据信息和包中的内容列表

完成打包

在这里插入图片描述

打包格式

python setup.py sdist --formats=指定格式如bztar
可以指定格式:

  1. zip: zip file
  2. gztar: tar.gz file
  3. bztar: tart.bz2 file
  4. ztar: tar.Z file
  5. tar: tar file
    python setup.py bdist --formats=指定格式如zip 二进制
    要以为bdist指定的格式:
    1.gztar: tar.gz file
    2.ztar: tar.Z file
    3.tar: tar file
    4.zip: zip file
    5.rpm: RPM Package
    6.pkgtool: Solaris pkgtool
    7.wininst: windows上自解压的zip格式的包
    8.msi: Microsoft Installer

bdist_dump:
bdist_rmp:
bdist_wininst
bdist_msi

获取帮助

python setup.py --help
python setup.py --help-commands: 所有可以使用的命令 如build,install
python setup.py COMMAND --help: 获取特定命令的帮助
python setup.py COMMAND --help-formats:获取特定命令支持使用的格式

在这里插入图片描述
在这里插入图片描述
(base) I:\ainlp\pythonlearning2>python setup.py bdist --help-formats
List of available distribution formats:
–formats=rpm RPM distribution
–formats=gztar gzip’ed tar file
–formats=bztar bzip2’ed tar file
–formats=xztar xz’ed tar file
–formats=ztar compressed tar file
–formats=tar tar file
–formats=wininst Windows executable installer
–formats=zip ZIP file
–formats=msi Microsoft Installer

(base) I:\ainlp\pythonlearning2>python setup.py sdist --help-formats
List of available source distribution formats:
–formats=bztar bzip2’ed tar-file
–formats=gztar gzip’ed tar-file
–formats=tar uncompressed tar file
–formats=xztar xz’ed tar-file
–formats=zip ZIP file
–formats=ztar compressed tar file

使用案例

1.原始目录:
ainlp 环境根目录
        pythonlearning2 项目目录
                init.py
                mainTest.py
                test1.py
2.开始打包:
pythonlearning2 项目目录下创建 :
README
setup.py
3.执行命令:
python setup.py sdist
或者python setup.py bdist 二进制形式
[注:]其中这两种命令都可以指定具体的文件格式后缀,其命令为:
python setup.py sdist --formats 具体的指定的格式如bztar
python setup.py bdist --formats 具体指定的格式 如zip

4.两种命令执行的不同的结果目录:
1. python setup.py sdist 执行后的目录为:
ainlp 环境根目录

        pythonlearning2 项目目录
                 dist目录
                        pck1-0.1.tar.gz

                __ init __.py
                mainTest.py
                test1.py
                MANIFEST
                README
                setup.py

2. python setup.py bdist 执行后的目录为:
ainlp 环境根目录

        pythonlearning2 项目目录
                 build
                        bdist.win-amd64
                        lib
                                mainTest.py
                                test1.py

                 dist
                        pck1-0.1.win-amd64.zip

                __ init __.py
                mainTest.py
                test1.py
                MANIFEST
                README
                setup.py

在这里插入图片描述

python setup.py bdist --help 获取bdist 帮助

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 ‘bdist’ command:
–bdist-base (-b) temporary directory for creating built distributions
–plat-name (-p) platform name to embed in generated filenames (default:
win-amd64)
–formats formats for distribution (comma-separated list)
–dist-dir (-d) directory to put final built distributions in [default:
dist]
–skip-build skip rebuilding everything (for testing/debugging)
–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 lists 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


(base) I:\ainlp\pythonlearning2>python setup.py --help-commands

Standard commands:
build
            build everything needed to install
build_py             “build” pure Python modules (copy to build directory)
build_ext             build C/C++ extensions (compile/link to build directory)
build_clib             build C/C++ libraries used by Python extensions
build_scripts             “build” scripts (copy and fixup #! line)
clean             clean up temporary files from ‘build’ command
install             install everything from build directory
install_lib             install all Python modules (extensions and pure Python)
install_headers             install C/C++ header files
install_scripts             install scripts (Python or otherwise)
install_data             install data files
sdist             create a source distribution (tarball, zip file, etc.)
register             register the distribution with the Python package index
bdist             create a built (binary) distribution
bdist_dumb             create a “dumb” built distribution
bdist_rpm             create an RPM distribution
bdist_wininst             create an executable installer for MS Windows
check             perform some checks on the package
upload             upload binary package to PyPI

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

安装

python包,打包
安装包:
python setup.py install
build and install:可以跳过build直接使用install,那么build结果就会放到默认的路径下面去,而build也可以自己指定build结果的目录。
python setup build:
            --build-base-/path/to/build_dir 将build的结果放到其它指定路径
                        在build中生成 lib,lib.platform目录:目录下保存即将安装的模块文件,其中lib保存纯python语言开发的模块,lib.platform则是保存的快平台的模块。
自定义安装路径:
python setup install :
           --uer= 尤其是在公共目录中没有写权限的普通用户,将模块安装到用户的家目录的某些相关位置。
           [注:]下面这两种方式通常只有被你所指定的公共目录中有写权限的用户才能够操作它。
           --prefix=安装在自己所期望的别的文件目录当中
           --exec-prefix=指定有其他语言实现的跟平台相关的文件安装目录
                                                                
对python模块的安装做深度定制:
           --install-purelib=/path/to/python_lib 纯python的库文件
           --install-platlib=/path/to/plat_lib 跟平台相关的由其他语言所实现的 路径自己定义,目录名也可以自己定义
            --install-lib=/path/to/lib 所有模块放在一起,上面两种模块不加区分地一起时可以使用安装在一块
           [注:]如果深度定制里面同时使用3种方式进行指定安装,则系统会有限选择–install-lib,即这种方式会覆盖前面的两种安装方式。
                                                                                                                                                                                                                                                                                   
           --install-scripts=/path/to/bin 二进制文件可执行文件的安装路径

            --install-data=指定数据文件的安装路径
            --install-headers=如果有c的头文件,c代码这个模块的安装路径

第三方模块的默认安装目录通常为:sitepackages

完结

下一篇 python异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值