Hướng dẫn develop your own python package - phát triển gói python của riêng bạn

Tại sao Pypi?

Chỉ số gói Python (PYPI) là một kho phần mềm cho ngôn ngữ lập trình Python. PYPI giúp bạn tìm và cài đặt phần mềm được phát triển và chia sẻ bởi cộng đồng Python. Nếu bạn sử dụng lệnh

if __name__ == "__main__":
code outside of a class or function goes here
4, bạn đã sử dụng PYPI.

Khi gói của bạn được xuất bản lên PYPI, mọi người đều có thể cài đặt và sử dụng nó với lệnh đơn giản quen thuộc:

pip install {your_package_name}

Tuyệt, hả?

Vì vậy, bạn cần làm gì để xuất bản gói của riêng bạn lên PYPI? Dưới đây là một danh sách ngắn các bước:

  • Làm cho mã của bạn sẵn sàng xuất bản: Tạo gói Python, thêm các tệp cần thiết cho PYPI
  • Tạo một tài khoản pypi nếu bạn đã có một tài khoản
  • Tạo tài liệu lưu trữ phân phối và tải lên PYPI
  • Cài đặt gói của riêng bạn bằng cách sử dụng
    if __name__ == "__main__":
    code outside of a class or function goes here
    4

Bước 1. Làm cho mã của bạn xuất bản sẵn sàng

Hướng dẫn này sẽ lấy gói của tôi được gọi là

if __name__ == "__main__":
code outside of a class or function goes here
6 làm ví dụ thực tế. Bạn có thể tìm thấy nó ở đây trên PYPI, mã nguồn ở đây trên GitHub.

Một số quy tắc của ngón tay cái:

  • Xóa tất cả các câu lệnh
    if __name__ == "__main__":
    code outside of a class or function goes here
    7 khỏi mã của bạn. Nếu bạn muốn thông báo hoặc đăng nhập một cái gì đó, hãy sử dụng ghi nhật ký.
  • Xóa tất cả các mã nằm ngoài một lớp hoặc chức năng. Mã đó (nếu thực sự cần thiết), hãy đặt nó theo hàm
    if __name__ == "__main__":
    code outside of a class or function goes here
    8:
if __name__ == "__main__":
code outside of a class or function goes here

Tạo gói Python

Gói trong Python chỉ đơn giản là một thư mục có tên của gói của bạn. Thư mục này chứa các tệp (mô-đun) và các bộ phụ khác (gói phụ).

Và bạn cần đặt một tệp

if __name__ == "__main__":
code outside of a class or function goes here
9 (hai dấu gạch dưới và sau
from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

0) để đánh dấu thư mục này thành gói Python. Bên trong tệp
if __name__ == "__main__":
code outside of a class or function goes here
9 này, bạn có thể chỉ định lớp bạn muốn người dùng truy cập thông qua giao diện gói.

Tệp mẫu

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

2:

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

Thêm các tệp cần thiết cho pypi

Nhu cầu PYPI theo tệp để làm việc:

  • from . import indextools
    from . import doctools

    __all__ = [
    'indextools',
    'doctools'
    ]

    3 (chi tiết bên dưới)
  • from . import indextools
    from . import doctools

    __all__ = [
    'indextools',
    'doctools'
    ]

    4 (Tệp giấy phép, nếu bạn chọn MIT, hãy lấy nội dung từ đây)
  • from . import indextools
    from . import doctools

    __all__ = [
    'indextools',
    'doctools'
    ]

    5 (tùy chọn nhưng rất được khuyến khích)
  • from . import indextools
    from . import doctools

    __all__ = [
    'indextools',
    'doctools'
    ]

    6 (tùy chọn nhưng rất được khuyến khích)

Cấu trúc dự án mẫu:

.
|-- HISTORY.md
|-- LICENSE.txt
|-- README.md
|-- elastictools
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | |-- doctools.cpython-36.pyc
| | `-- indextools.cpython-36.pyc
| |-- doctools.py
| `-- indextools.py
|-- setup.py
`-- tests
|-- test_doc_tools.py
`-- test_index_tools.py

Tệp from . import indextoolsfrom . import doctools__all__ = [ 'indextools', 'doctools']3

Tệp setup.py chứa thông tin về gói của bạn mà pypi cần, như tên của nó, một mô tả, phiên bản hiện tại, v.v.

from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

Lưu ý: Không nhầm lẫn

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

9 với
.
|-- HISTORY.md
|-- LICENSE.txt
|-- README.md
|-- elastictools
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | |-- doctools.cpython-36.pyc
| | `-- indextools.cpython-36.pyc
| |-- doctools.py
| `-- indextools.py
|-- setup.py
`-- tests
|-- test_doc_tools.py
`-- test_index_tools.py
0. Chúng tôi sử dụng ____29 ở đây.
Do NOT confuse
from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

9 with
.
|-- HISTORY.md
|-- LICENSE.txt
|-- README.md
|-- elastictools
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | |-- doctools.cpython-36.pyc
| | `-- indextools.cpython-36.pyc
| |-- doctools.py
| `-- indextools.py
|-- setup.py
`-- tests
|-- test_doc_tools.py
`-- test_index_tools.py
0. We use
from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

9 here.

Hầu hết các tùy chọn đều có thể tự giải thích, bạn chỉ có thể sao chép nội dung của

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

3 ở trên và sửa đổi nó là nhu cầu của bạn. Xin nhớ liệt kê tất cả các phụ thuộc của gói của bạn trong danh sách
.
|-- HISTORY.md
|-- LICENSE.txt
|-- README.md
|-- elastictools
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | |-- doctools.cpython-36.pyc
| | `-- indextools.cpython-36.pyc
| |-- doctools.py
| `-- indextools.py
|-- setup.py
`-- tests
|-- test_doc_tools.py
`-- test_index_tools.py
3, để các yêu cầu này có thể được cài đặt tự động trong khi gói của bạn đang được cài đặt.

Bao gồm các tệp dữ liệu

Để bao gồm các tệp dữ liệu (cấu hình, env, mẫu.) Đến gói, lúc đầu, chúng ta cần thêm dòng này vào

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

3:

setup(
...
include_package_data=True
)

Và sau đó tạo một tệp

.
|-- HISTORY.md
|-- LICENSE.txt
|-- README.md
|-- elastictools
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | |-- doctools.cpython-36.pyc
| | `-- indextools.cpython-36.pyc
| |-- doctools.py
| `-- indextools.py
|-- setup.py
`-- tests
|-- test_doc_tools.py
`-- test_index_tools.py
5 bên cạnh tệp
from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

3 và liệt kê tất cả các tệp cần thiết để bao gồm như sau:

include src/templates/*

Bước 2. Tạo tài khoản Pypi

Nếu bạn đã có tài khoản PYPI (và vẫn còn nhớ tên người dùng/mật khẩu của bạn), bạn có thể bỏ qua bước này. Nếu không, vui lòng truy cập trang chủ PYPI và đăng ký tài khoản mới ngay lập tức (tất nhiên là miễn phí).

Bước 3. Tạo lưu trữ phân phối và tải lên PYPI

Tạo tài liệu lưu trữ phân phối

Đây là những tài liệu lưu trữ được tải lên chỉ mục gói và có thể được cài đặt bởi PIP.

Đảm bảo bạn đã cài đặt các phiên bản mới nhất của

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

9 và
.
|-- HISTORY.md
|-- LICENSE.txt
|-- README.md
|-- elastictools
| |-- __init__.py
| |-- __pycache__
| | |-- __init__.cpython-36.pyc
| | |-- doctools.cpython-36.pyc
| | `-- indextools.cpython-36.pyc
| |-- doctools.py
| `-- indextools.py
|-- setup.py
`-- tests
|-- test_doc_tools.py
`-- test_index_tools.py
8:

pip install --user --upgrade setuptools wheel

Bây giờ hãy chạy lệnh này từ cùng một thư mục nơi

from . import indextools
from . import doctools

__all__ = [
'indextools',
'doctools'
]

3 được đặt:

python3 setup.py sdist bdist_wheel

Lệnh này sẽ xuất ra rất nhiều văn bản và sau khi hoàn thành sẽ tạo hai tệp trong thư mục

from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

0, trong số các tệp khác trong thư mục
from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

1 và
from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

2:

.
|-- build
| |-- bdist.linux-x86_64
| `-- lib
| `-- elastictools
| |-- __init__.py
| |-- doctools.py
| `-- indextools.py
|-- dist
| |-- elastictools-0.1.2-py3-none-any.whl
| `-- elastictools-0.1.2.tar.gz
`-- elastictools.egg-info
|-- PKG-INFO
|-- SOURCES.txt
|-- dependency_links.txt
|-- requires.txt
`-- top_level.txt

Bạn nên thêm tất cả ba thư mục này vào tệp

from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

3 của bạn.

Tải lên kho lưu trữ phân phối

Để làm điều này, bạn có thể sử dụng

from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

4. Đầu tiên, cài đặt nó bằng
if __name__ == "__main__":
code outside of a class or function goes here
4:

if __name__ == "__main__":
code outside of a class or function goes here
0

Sau đó tải lên tất cả các tài liệu lưu trữ lên PYPI:

if __name__ == "__main__":
code outside of a class or function goes here
1

Sau khi tải lên thành công, hãy truy cập trang web PYPI, theo dự án của bạn, bạn có thể tìm thấy gói được xuất bản của mình.

Gói được xuất bản xuất hiện theo `Dự án của bạn trên PYPI.`Your projects` on PyPi.

Danh sách công khai ở đây: https://pypi.org/project/elastictools/

Trang gói công khai của bạn trên PYPI.

Bước 4. Cài đặt gói của riêng bạn bằng cách sử dụng if __name__ == "__main__": code outside of a class or function goes here4

Bây giờ mọi người đều có thể cài đặt gói của bạn bằng lệnh

from setuptools import setup, find_packages

with open('README.md') as readme_file:
README = readme_file.read()

with open('HISTORY.md') as history_file:
HISTORY = history_file.read()

setup_args = dict(
name='elastictools',
version='0.1.2',
description='Useful tools to work with Elastic stack in Python',
long_description_content_type="text/markdown",
long_description=README + '\n\n' + HISTORY,
license='MIT',
packages=find_packages(),
author='Thuc Nguyen',
author_email='',
keywords=['Elastic', 'ElasticSearch', 'ElasticStack'],
url='https://github.com/ncthuc/elastictools',
download_url='https://pypi.org/project/elastictools/'
)

install_requires = [
'elasticsearch>=6.0.0,<7.0.0',
'jinja2'
]

if __name__ == '__main__':
setup(**setup_args, install_requires=install_requires)

7 quen thuộc:

pip install {your_package_name}

Và cập nhật nó sau:

if __name__ == "__main__":
code outside of a class or function goes here
3