Python WebPython

PyPI - 如何打包Python代码为PIP包

2019-06-07  本文已影响0人  红薯爱帅

1. 前言

Python Package,在实际项目中应用广泛,包含Public Package和Private Package。它有助于我们对代码进行功能封装,使得软件更加结构化、有序化、层次化。主要有几个应用场景:

2. 如何打包Python Package

2.1. Creating the package files

/packaging_tutorial
  /example_pkg
    __init__.py
  setup.py
  LICENSE
  README.md

2.2. Generating distribution archives

$ python3 -m pip install --user --upgrade setuptools wheel
$ python3 setup.py sdist bdist_wheel
$ tree dist/
dist/
├── example_pkg_shuzhang-0.0.1-py3-none-any.whl
├── example-pkg-shuzhang-0.0.1.tar.gz
├── example_pkg_shuzhang-0.0.2-py3-none-any.whl
└── example-pkg-shuzhang-0.0.2.tar.gz

0 directories, 4 files

2.3. Uploading the distribution archives

$ python3 -m pip install --user --upgrade twine
$ python3 -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
$ python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
$ twine upload dist/*

2.4. Just do some tests

$ python3 -m pip install --index-url https://test.pypi.org/simple/
$ pip install package-name==0.0.2
>>> import example_pkg
>>> example_pkg.name
'example_pkg'

3. Reference

上一篇 下一篇

猜你喜欢

热点阅读