开源python-打包发布

python开源系列文章

打包发布

打包发布之后,别的开发者就可以通过pip等方式进行安装使用

打包方式一:setup.py

from setuptools import setup, find_packages

setup(
    name='volumeestimator',
    packages=find_packages(),
    package_data={
    
    '': ['config.yaml', 'yolov5s.onnx']},  # include besides python file
    author='friday',
    author_email='[email protected]',
    version='0.0.7',
    description='volume estimator basic version',
    license='',
    install_requires=[
        'numpy==1.19.5',
        'pandas==1.3.2',
        'torch==1.7.1',
        'onnxruntime==1.8.0',
        'onnx==1.8.0',
        'opencv-python==4.5.3.56',
        'pyyaml==5.4.1'
    ]
)

打包方式二: poetry-pyproject.toml

这篇文章描述的好处是

  • your pyproject.toml can be very close to boilerplate.
    It’s basically the three lines that Brett posted…
    the additional stuff for screed has to do with setuptools_scm, which we’re using to automatically convert git tags like v1.0.4 into actual version numbers.
  • your setup.cfg basically contains almost everything your setup.py contained, just a bit reformatted to fit into the setup.cfg format.
  • your new setup.py can now be a really short stub to permit python setup.py … to continue to work.

build打包

python3 -m pip install --upgrade build
python3 -m build

pyproject也可以调用外面的setup.py,把二者的好处都占了

build-backend = "setuptools.build_meta"

当然也可以使用poetry直接进行包的管理与发布

# 安装环境,生成poetry.lock
poetry install
# 打包
poetry build
# 发布
poetry publish --username yuetan --password 123456

如果poetry install太慢,清楚缓存

sudo poetry cache clear --all pypi

设置自己的pypi用户名和密码

上传方式一

twine upload dist/*

上传方式二

命令上传
在根目录建立.pypirc, 注意其目录~/.pypirc

[distutils]
index-servers =
    pypi

[pypi]
repository: <repository-url>
username: <username>
password: <password>

打包并上传

python setup.py sdist upload

避免错误

注意上传的网址pypi和test.pypi,如果都上传要注册两次账号,否则上传时就有报错

  • https://pypi.org/
  • https://test.pypi.org/
ERROR    HTTPError: 403 Forbidden from https://test.pypi.org/legacy/
         Invalid or non-existent authentication information. See https://test.pypi.org/help/#invalid-auth
         for more information.

如果之后pip还是报错,我自己是因为设置了python3.8以上,在3.7的环境里就没有了

版本

poetry也可以管理版本

参考与继续阅读

  • https://packaging.python.org/distributing/#uploading-your-project-to-pypi
  • https://zhuanlan.zhihu.com/p/557498429
  • https://zhuanlan.zhihu.com/p/342682533
  • https://zhuanlan.zhihu.com/p/296333007
  • https://copyprogramming.com/howto/pypiserver-debugging-could-not-find-a-version-that-satisfies-the-requirement
  • https://stackoverflow.com/questions/46606692/cant-upload-to-pypi-with-twine-

猜你喜欢

转载自blog.csdn.net/weixin_38812492/article/details/124102472