• python程序打包——基础准备、源代码打包、二进制打包、setuptools基础



    活动地址:CSDN21天学习挑战赛

    1.setuptools用途

    • 创建基于脚本的Python安装程序
    • 编译扩展
    • 与py2exe和py2app结合起来使用
    • 创建独立的Windows和macOS可执行程序

    2.相关网站

    3.安装

    • anaconda已经自动包含了,anaconda安装可参考这里
    • 如果没有,需要pip下载

    4.打包方式

    • 源码包sdist:包含所有所需源码文件和一些静态文件的压缩包(Windows:.zip,类Unix(Linux,macOS:.tar,gz))
    • 二进制bdist:主流是wheel(.whl后缀),二进制包不用再编译,安装更快

    5.安装源码包

    • 解压源码包后,再执行:
    python setup.py install
    
    • 1

    这个命令的作用等效于先install再build

    • 直接pip安装
    pip install xxx.zip
    
    • 1
    • 软连接:开发阶段,执行下列代码创建软连接,在修改包后不用再次安装就能生效,便于调试
    pip install -e .
    
    • 1

    等效于:

    python setup.py develop
    
    • 1

    6.setup.py

    • 新建文件夹
    • 新建一个带打包文件main.py
    • main.py内容随意,我的是画出一个安卓小人:
    #!/usr/bin/env python
    import turtle
    aj=turtle.Pen()
    y=0
    aj.speed(5)
    #turtle.screensize(200,800)
    turtle.bgcolor("black")
    #aj.shape("turtle")
    def head():
        aj.color("green")
        aj.fd(160)
        x=aj.xcor()
        aj.seth(90)
        aj.begin_fill()
        #aj.color("green")
        aj.circle(x/2,180)
        aj.end_fill()
        aj.penup()
        aj.goto(33,37)
        aj.pendown()
        aj.dot(13,"black")
        aj.penup()
        aj.goto(126,37)
        aj.pendown()
        aj.dot(13,"black")
        aj.penup()
        aj.home()
        aj.pendown()
        aj.hideturtle()
        aj.fd(160)
        aj.seth(90)
        aj.circle(x/2,60)
        aj.right(90)
        aj.pensize(5)
        aj.fd(30)
    
        aj.penup()
        aj.home()
        #aj.pendown()
        aj.hideturtle()
        aj.fd(160)
        aj.seth(90)
        aj.circle(x/2,120)
        aj.right(90)
        aj.pensize(5)
        aj.pendown()
        aj.fd(30)
        aj.penup()
        aj.home()
        aj.penup()
    
    def body():
        aj.pensize(0)
    
        aj.home()
        aj.showturtle()
        aj.goto(0,-7)
        aj.pendown()
        aj.begin_fill()
        aj.fd(160)
        aj.right(90)
        aj.fd(120)
        aj.right(90)
        aj.fd(160)
        y=aj.ycor()
        aj.right(90)
        aj.fd(120)
        aj.end_fill()
    
    def legs():
        aj.penup()
        #turtle.color("red")
        aj.goto(33,-169)
        aj.pendown()
        aj.pensize(32)
        aj.fd(43)
        aj.penup()
        aj.goto(130,-169)
        aj.pendown()
        aj.fd(43)
        aj.penup()
    
    def hands():
        aj.home()
        aj.pensize(30)
        aj.goto(-18,-77)
        aj.pendown()
        aj.left(90)
        aj.fd(65)
        aj.penup()
        aj.goto(179,-77)
        aj.pendown()
        aj.fd(65)
        aj.penup()
        aj.hideturtle
        aj.fd(100)
        aj.hideturtle()
        aj.circle(100)
        aj.circle(100,360,59)
        aj.reset()
        turtle.bgcolor("black")
        turtle.pencolor("green")
        turtle.hideturtle()
        turtle.goto(-300,0)
        aj.hideturtle
        turtle.write("Thank you for watching....", font = ("Bodoni MT Black", 28, "bold"))
        turtle.penup()
        turtle.goto(-40,-170)
        turtle.pendown()
        turtle.pencolor("yellow")
        turtle.write("Developed by 一个超会写Bug的安太狼", font = ("Palatino Linotype", 22, "bold"))
    
    head()
    body()
    legs()
    hands()
    turtle.done()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 运行效果:
      在这里插入图片描述

    • 新建文件setup.py

    • setup.py内容如下:

    from setuptools import setup
    
    setup(
        name='demo',
        version='0.0',
        description='A simple example',
        author='A Hang 626',
        author_email='534232652@qq.com',
        url='None',
        py_modules=['main']
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 查看可输入参数:
    python setup.py --help-commands
    
    • 1
    • 得到输出:
    Standard commands:
      build             build everything needed to install
      build_py          "build" pure Python modules (copy to build directory)
      build_ext         build C/C++ and Cython 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
    
    Extra commands:
      alias             define a shortcut to invoke one or more commands
      bdist_egg         create an "egg" distribution
      develop           install package in 'development mode'
      dist_info         create a .dist-info directory
      easy_install      Find/get/install Python packages
      egg_info          create a distribution's .egg-info directory
      install_egg_info  Install an .egg-info directory for the package
      rotate            delete older distributions, keeping N newest files
      saveopts          save supplied options to setup.cfg or other config file
      setopt            set an option in setup.cfg or another config file
      test              run unit tests after in-place build (deprecated)
      upload_docs       Upload documentation to sites other than PyPi such as devpi
      isort             Run isort on modules registered in setuptools
      bdist_wheel       create a wheel distribution
      compile_catalog   compile message catalogs to binary MO files
      extract_messages  extract localizable strings from the project code
      init_catalog      create a new catalog based on a POT file
      update_catalog    update message catalogs from a POT file
      flake8            Run Flake8 on modules registered in setup.py
      build_sphinx      Build Sphinx documentation
    
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 其中Standard commands是标准命令,Extra commands是额外命令
  • 相关阅读:
    JS——数字字符串的比较逻辑分析
    doccano 文本标注工具使用
    使用C++实现MySQL数据库编程
    计算机视觉系列 -OpenMMLab 之 MMRazor 模型轻量化瑞士军刀 蒸馏、剪枝、网络结构搜索全方向覆盖
    设计模式——七大设计原则
    移动端适配单位vw和px的转换
    关于淘宝多个关键词权重快速提升的方法介绍
    【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据
    接口自动化Requests+Pytest基础实现
    【开始刷题啦——Leetcode《初级算法》(Go语言)】
  • 原文地址:https://blog.csdn.net/weixin_53610475/article/details/126450063