• python 源码模块打包发布


    python 源码模块打包发布

    使用 setuptools Cython 打包2进制包 wheel

    1 安装依赖:
    pip install setuptools
    pip install Cython
    pip install wheel
    
    • 1
    • 2
    • 3
    2 某源码模块 my_module
    my_module/
    __init__.py  setup.py  test1.py  test2.py  test3.py
    
    
    • 1
    • 2
    • 3

    其中 test1.py

    def fun1(p):
        print(f"fun1:{p}")
    
    • 1
    • 2

    其中 test2.py

    def fun2(p):
        print(f"fun2:{p}")
    
    • 1
    • 2

    其中 test3.py

    def fun3(p):
        print(f"fun3:{p}")
    
    • 1
    • 2
    3 编写setup.py文件
    from setuptools import Extension, find_packages, setup
    from Cython.Build import cythonize
    
    # 必须与 my_module 模块文件夹名相同
    pkg_name = "my_module"
    
    # 模块下需要打包的py文件
    files = ["test1.py" , "test2.py" , "test3.py"]
    
    setup(
        name=pkg_name,
        version="0.0.1",
        author="wmx",
        author_email="wmx@wmx.com",
        packages=find_packages(),
        ext_modules=cythonize(files),
        zip_safe=False,
        requires=[# 这里填写模块的所有依赖包
            'requests',
            'json',
            'typing'
        ]
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    4 编译生成 whl 安装文件

    python setup.py bdist_wheel  
    
    • 1

    在my_module/dist 目录下生成 my_module-0.0.1-cp39-cp39-win_amd64.whl

    5 安装

    pip install ./my_module-0.0.1-cp39-cp39-win_amd64.whl
    
    • 1

    6 测试

    新建 use_module.py

    from my_module import test1
    from my_module import test2
    from my_module import test3
    
    # print("help:")
    # help(test1)
    # help(test2)
    # help(test3)
    
    test1.fun1("sadf")
    test2.fun2("xxx")
    test2.fun3("jjj")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 参考命令 :
      python setup.py --help
    PS D:\workspace\Python\test_freeotp\my_module> python setup.py --help   
      
    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
      --command-packages  list of packages that provide distutils commands
    
    Information display options (just display information, ignore any commands)
      --help-commands     list all available commands
      --name              print package name
      --version (-V)      print package version
      --fullname          print -
      --author            print the author's name
      --author-email      print the author's email address
      --maintainer        print the maintainer's name
      --maintainer-email  print the maintainer's email address
      --contact           print the maintainer's name if known, else the author's
      --contact-email     print the maintainer's email address if known, else the
                          author's
      --url               print the URL for this package
      --license           print the license of the package
      --licence           alias for --license
      --description       print the package description
      --long-description  print the long package description
      --platforms         print the list of platforms
      --classifiers       print the list of classifiers
      --keywords          print the list of keywords
      --provides          print the list of packages/modules provided
      --requires          print the list of packages/modules required
      --obsoletes         print the list of packages/modules made obsolete
    
    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

    python setup.py --help-commands

    
    PS D:\workspace\Python\test_freeotp\my_module> 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.)
      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:
      bdist_wheel       create a wheel distribution
      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
    
    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
  • 相关阅读:
    底层码农重返创作者行列
    安装IntelliJ IDEA
    分布式与微服务的区别
    【Java项目介绍和界面搭建】拼图小游戏——打乱图片顺序
    K8S中的网络
    如何开通 chatGPT4 会员
    视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
    全文来了!中低速蜂窝物联网LTE Cat.1行业发展白皮书发布
    Redis内存不足解决方案
    使用JMeter进行MySQL的压力测试
  • 原文地址:https://blog.csdn.net/WMX843230304WMX/article/details/132882874