当前位置 博文首页 > weixin_41997073的博客:【星海出品】python setup详细应用及解

    weixin_41997073的博客:【星海出品】python setup详细应用及解

    作者:[db:作者] 时间:2021-07-15 15:39

    PYTHON设置的标准打开方式

    Gentlemen and ladies!
    welcome to 1999 @ Chinese Software Developer Network

    python开始的认知

    key简述
    distutils开始于1998年python创立之初成立的标准库
    setuptools是目前安装打包的标准方式
    pip是目前管理包的事实标准

    python从setup 文件开始

    相关 write setup 文档

    https://www.python.org/community/sigs/current/distutils-sig/design/
    setup 一般建立在顶级目录(项目的根目录)

    #来源于python.org
    from distutils.core import Distribution, setup
    
    class MyDistribution (Distribution):
        name = "mydist",
        version = "1.3.4",
        author = "Greg Ward <gward@cnri.reston.va.us>"
        description = """\
    This is an example module distribution.  It provides no useful code,
    but is an interesting example of the Distutils in action."""
    
    # Dependencies
    requires = { 'python': '1.5',  # I like class-based exceptions
                 're': '*',        # and I love Perl-style regexps! ;-)
    }
    # Actual files that need to be processed and installed in some form
    py_modules = ['mymod.py', 'my_othermod.py'],
    ext_modules = {'myext.c': 
             {'other_c': ['extra1.c', 'extra2.c'],
                         'c_libraries': ['mylib']}
                  }
    
    setup (distclass = MyDistribution)
    

    使用早期的版本,讲解

    from distutils.core import setup
    
    setup(
        name='m',
        version='1.0',
        discription='my project'
        author='gebiwang'
        author_email='henkuaia@python.net'
        url='https://www.python.org'
        packages=['test' ]
        )
    #重点是packages
    #需要根目录有test pack包,自己创一个目录包含__init__.py 的目录即可
    #视觉上来说就是有个test目录,包含个__init__.py 文件,内部自定义数据描述符。
    

    # name名字
    # version 版本
    # package=[] 打包列表
    # url 包的主页,可以不写

    使用方法

    python setup.py
    #理论上说,现在你可以把虚拟环境env下的site-package下的包拷贝到根目录了
    #没有相关的名词空间标识符,需要python setup.py install 来获取描述器
    python setup.py build
    #会生成一个build目录
    

    setup做的事情
    1.running build
    2.creating build\lib
    3.copying test -> \build\lib
    4.copying test__init__py -> \build\lib\test
    5.copying #other

    python setup.py sdist
    #打包命令,生成gz文件 ,打包命令也支持一些二进制分发生成windows类型的。
    #例如:
    python setup.py bdist_msi
    还支持 bdist_rpm / bdist_zip / bdist_wininst
    #最新的模块支持的类型更多
    from setuptools import setup
    #新模块支持egg等模式,该模式也支持zip协议
    例如:python setup.py bdist_egg

    打包的意义
    打包后会多出一个dist目录,和一个gz文件
    (取决于bdist 后选择的模式,默认为gz)
    #
    解包后会多一个 PKG-INFO,该文件会保存txt版本的setup函数信息
    #该文件打包时,还可以指定格式
    例如:
    python setup.py sdist --formats=zip

    在linux bash 环境里,解压缩打包的好的setup文件,就可以进入该文件下的虚拟环境env,安装你自己编写定义的python包文件(目录下包含__init__的目录)

    pip install test-1.0.tar.gz

    然后就可以进入python env环境里就可以直接导入test模块了

    python
    >>> import test
    my name is test

    wheel模式的包
    需要安装pip install wheel
    python setup.py bdist_wheel

    python 使用解释器帮忙,调用的是底层的命令或接口等,例如shell。
    底层是编译好的代码。

    cs