Python私有仓库打包部署及验证方式

一、背景

最近在做 Java&Python工程化的内容,为了将自己的通用功能或基础功能打包发布到私有仓库,类似 Java Maven deploy 的功能。

二、文档结构及示例

1. 示例结构

|--com
|	|--static
|	|	|--icon.svg
|	|	|--confg.json
|	|--demo
|	|	|--__init__.py
|	|	|--core.py
|	|--__init__.py
|	|--__version__.py
|	|--api.py
|	|--utils.py
|--tests
|	|--__init__.py
|--LICENSE
|--README.md
|--requirements.txt
|--setup.py

2. setup.py 文件示例

from setuptools import setup, find_packages

with open("requirements.txt", "r", encoding="utf-8") as fh:
    requires = fh.readlines()

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="demo-py",
    version="0.0.1",
    author="lytao123",
    author_email="lytao123",
    description="a demo by python package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
    install_requires=requires
)

三、打包发布

1. 安装

python setup.py install

2. 打包

# tar.gz包(源码)
python setup.py sdist    

# whl 包(编译)
python setup.py bdist_wheel

# tar.gz包和whl 包(推荐)
python setup.py sdist bdist_wheel   

3. 上传到私有仓库

# 安装 twine     
pip install twine
  • 方式一

    • 新增 ~/.pypirc 文件,注意 window 系统的路径为C:\Users\用户名\.pypirc
      [distutils]
      index-servers=nexus
      
      [nexus]
      repository:pypi私服地址
      username:username
      password:passwaord
      
    • 执行命令 python -m twine upload -r nexus dist/* 或者 twine upload -r nexus dist/*
  • 方式二

    • 直接执行如下命令
      python -m twine upload --repository-url http://pypi私服地址/  -u username -p password dist/*
      

四、验证拉取包

执行如下命令,成功拉取包;也可去私服仓库查看是否存在上传的包。

pip install demo-py==0.0.1 -i http://pypi私服地址/simple --trusted-host pypi私服.com

五、问题记录

打包命令执行错误

执行后报错为: error: invalid command bdist_wheel
解决方式:执行 pip install wheel 按照 wheel 模块

可通过执行 python setup.py --help-commands 查询支持的包格式,默认有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lytao123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值