python打包可通过pip安装的自定义库

项目目录如下

在项目下创建一个package并创建__init__.py文件(该package会被看作一个python模块)

rf-poco
|-- PocoLib
|   |-- PocoLib.py
|   `-- __init__.py
`-- setup.py

init.py内容

需要import可供调用的类或方法

from PocoLib.PocoLib import PocoLib

name = "PocoLib"

setup.py配置文件内容

from setuptools import setup, find_packages

setup(
    name="PocoLib",
    version="0.0.4",
    author="***",
    author_email="***********",
    description="robotframework poco test package",
    url="http://gitlab.**************",
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        "poco>=0.98.1",
        "pocoui>=1.0.82"
    ],
    python_requires=">=3"
)

构建

python setup.py sdist

此时项目路径下自动生成dist目录,构建完的包文件位于dist文件夹中

上传

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

需要注册test.pypi账号,链接:https://test.pypi.org/
注意:若上传时dist中存在https://test.pypi.org/上对应项目中已经存在的版本时,则会报错
如:dist中存在PocoLib0.0.2,且https://test.pypi.org/对应的项目中也存在0.0.2版本
解决方法:更新setup.py中的版本号,删除dist文件夹中对应原先版本号的包文件,确保上传的版本是pypi对应项目中不存在的版本

安装

上传成功后则可通过对应的pip命令安装

使用

安装完成后可直接通过import调用
import的包名与项目目录中__init__.py文件上一级的目录名一致

posted @ 2021-11-30 16:59  Ravenna  阅读(445)  评论(0编辑  收藏  举报