自定义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

posted on 2019-01-04 13:16  荣锋亮  阅读(2208)  评论(0编辑  收藏  举报

导航