zoukankan      html  css  js  c++  java
  • 自定义pip 包开发简单说明

    文档主要来自官方文档,主要是为了测试学习

    创建pip 包项目

    • 项目结构
    ├── LICENSE
    ├── README.md
    ├── dalongrong_example_pkg
    └── __init__.py
    └── setup.py
     
     
    • 代码说明
      dalongrong_example_pkg/init.py
    name = "dalongrong_example_pkg" 

    setup.py

    import setuptools
    with open("README.md", "r") as fh:
        long_description = fh.read()
    setuptools.setup(
        name="dalongrong_example_pkg",
        version="0.0.1",
        author="dalongrong",
        author_email="1141591465@qq.com",
        description="A small example package",
        long_description=long_description,
        long_description_content_type="text/markdown",
        url="https://github.com/rongfengliang/pip-demo-package.git",
        packages=setuptools.find_packages(),
        classifiers=[
            "Programming Language :: Python :: 3",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
    )
     
     

    README.md 文件,就是一个简单的说明

    安装构建依赖&& 生成归档文件

    • 安装setuptools&& wheel
    python3 -m pip install --user --upgrade setuptools wheel
    • 生成归档文件
    python3 setup.py sdist bdist_wheel
     

    效果:

    python3 setup.py sdist bdist_wheel
    running sdist
    running egg_info
    creating dalongrong_example_pkg.egg-info
    writing dalongrong_example_pkg.egg-info/PKG-INFO
    writing dependency_links to dalongrong_example_pkg.egg-info/dependency_links.txt
    writing top-level names to dalongrong_example_pkg.egg-info/top_level.txt
    writing manifest file 'dalongrong_example_pkg.egg-info/SOURCES.txt'
    reading manifest file 'dalongrong_example_pkg.egg-info/SOURCES.txt'
    writing manifest file 'dalongrong_example_pkg.egg-info/SOURCES.txt'
    running check
    creating dalongrong_example_pkg-0.0.1
    creating dalongrong_example_pkg-0.0.1/dalongrong_example_pkg
    creating dalongrong_example_pkg-0.0.1/dalongrong_example_pkg.egg-info
    copying files to dalongrong_example_pkg-0.0.1...
    copying README.md -> dalongrong_example_pkg-0.0.1
    copying setup.py -> dalongrong_example_pkg-0.0.1
    copying dalongrong_example_pkg/__init__.py -> dalongrong_example_pkg-0.0.1/dalongrong_example_pkg
    copying dalongrong_example_pkg.egg-info/PKG-INFO -> dalongrong_example_pkg-0.0.1/dalongrong_example_pkg.egg-info
    copying dalongrong_example_pkg.egg-info/SOURCES.txt -> dalongrong_example_pkg-0.0.1/dalongrong_example_pkg.egg-info
    copying dalongrong_example_pkg.egg-info/dependency_links.txt -> dalongrong_example_pkg-0.0.1/dalongrong_example_pkg.egg-info
    copying dalongrong_example_pkg.egg-info/top_level.txt -> dalongrong_example_pkg-0.0.1/dalongrong_example_pkg.egg-info
    Writing dalongrong_example_pkg-0.0.1/setup.cfg
    creating dist
    Creating tar archive
    removing 'dalongrong_example_pkg-0.0.1' (and everything under it)
    running bdist_wheel
    running build
    running build_py
    creating build
    creating build/lib
    creating build/lib/dalongrong_example_pkg
    copying dalongrong_example_pkg/__init__.py -> build/lib/dalongrong_example_pkg
    installing to build/bdist.macosx-10.13-x86_64/wheel
    running install
    running install_lib
    creating build/bdist.macosx-10.13-x86_64
    creating build/bdist.macosx-10.13-x86_64/wheel
    creating build/bdist.macosx-10.13-x86_64/wheel/dalongrong_example_pkg
    copying build/lib/dalongrong_example_pkg/__init__.py -> build/bdist.macosx-10.13-x86_64/wheel/dalongrong_example_pkg
    running install_egg_info
    Copying dalongrong_example_pkg.egg-info to build/bdist.macosx-10.13-x86_64/wheel/dalongrong_example_pkg-0.0.1-py3.7.egg-info
    running install_scripts
    adding license file "LICENSE" (matched pattern "LICEN[CS]E*")
    creating build/bdist.macosx-10.13-x86_64/wheel/dalongrong_example_pkg-0.0.1.dist-info/WHEEL
    creating 'dist/dalongrong_example_pkg-0.0.1-py3-none-any.whl' and adding 'build/bdist.macosx-10.13-x86_64/wheel' to it
    adding 'dalongrong_example_pkg/__init__.py'
    adding 'dalongrong_example_pkg-0.0.1.dist-info/LICENSE'
    adding 'dalongrong_example_pkg-0.0.1.dist-info/METADATA'
    adding 'dalongrong_example_pkg-0.0.1.dist-info/WHEEL'
    adding 'dalongrong_example_pkg-0.0.1.dist-info/top_level.txt'
    adding 'dalongrong_example_pkg-0.0.1.dist-info/RECORD'
    removing build/bdist.macosx-10.13-x86_64/wheel
    ├── LICENSE
    ├── README.md
    ├── build
    ├── bdist.macosx-10.13-x86_64
    └── lib
    └── dalongrong_example_pkg
    └── __init__.py
    ├── dalongrong_example_pkg
    └── __init__.py
    ├── dalongrong_example_pkg.egg-info
    ├── PKG-INFO
    ├── SOURCES.txt
    ├── dependency_links.txt
    └── top_level.txt
    ├── dist
    ├── dalongrong_example_pkg-0.0.1-py3-none-any.whl
    └── dalongrong_example_pkg-0.0.1.tar.gz
    └── setup.py
     
     

    安装发布上传依赖

    • 安装twine
    python3 -m pip install --user --upgrade twine
     
     
    twine upload --repository-url https://test.pypi.org/legacy/ dist/*
     
    • 效果

    使用上传的包

    pip 方式安装

    • 安装
    pip install -i https://test.pypi.org/simple/ dalongrong-example-pkg
     
    • 调用代码
    app.py
    import dalongrong_example_pkg
    print(dalongrong_example_pkg.name)
     
     
    • 运行
    python app.py
    dalongrong_example_pkg
    • requirements 方式使用
      项目结构:
     
    ├── main.py
    ├── requirements.txt
    └── run.sh
     

    requirements.txt 文件:

    dalongrong-example-pkg
     
     

    main.py:

    import dalongrong_example_pkg
    print(dalongrong_example_pkg.name)
     

    run.sh:

    #!/bin/sh
    pip install -i https://test.pypi.org/simple/ -r requirements.txt
    python main.py
     

    说明

    这个只是一个简单的demo,只是为了方便学习

    参考资料

    https://packaging.python.org/tutorials/packaging-projects/
    https://test.pypi.org/project/dalongrong-example-pkg/#history
    https://github.com/rongfengliang/pip-demo-package

  • 相关阅读:
    Codeforces Round 546 (Div. 2)
    Codeforces Round 545 (Div. 2)
    Codeforces Round 544(Div. 3)
    牛客小白月赛12
    Codeforces Round 261(Div. 2)
    Codeforces Round 260(Div. 2)
    Codeforces Round 259(Div. 2)
    Codeforces Round 258(Div. 2)
    Codeforces Round 257 (Div. 2)
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/10219292.html
Copyright © 2011-2022 走看看