Twine 是用于在 PyPI 上发布 Python 软件包的实用程序。
Twine 库可以帮助我们,在新建的项目或者已有的项目中,打包、上传二进制程序包或者源码包,以便于我们分发我们的应用程序。
1. 安装方式
Twine 库的安装方式和其他 pip 库一样。
- [1] 安装方式
$ pip install twine
-
[2] 简单使用
# 1. create distributions$ python setup.py sdist bdist_wheel # 2. upload to testpypi$ twine upload -r testpypi dist/*username: ...password:... # 3. upload to pypi$ twine upload dist/*username: ...password:... # 4. use this lib$ python3 -m pip install pypi-demo
2. 功能特性
- [1] 支持 HTTPS 传输
使用 Twine 库最大的原因就是,无论我们使用的是什么 Python 版本,其都可以在我们构建打包完成之后,通过 HTTPS 协议安全地将对应包上传到 PyPI 服务器上面。同时,上传包的前提是需要,我们正确的配置 Python 版本和底层操作系统等。
- [2] 支持上传任何包格式
我们在使用 Twine 库的时候,在需要手动打包我们的项目了,可以在 setup.py 文件中添加子命令,在其中执行。完成打包构建之后,将自动上传到服务器。
$ python setup.py upload
- [3] 支持
Twine 支持,我们通过 .asc 文件传递到命令行的方式对文件进行签名。这样一来,我们就可以通过 gpg 工具对该库进行验证,确保库没有问题。
# 签名$ twine upload myproject-1.0.1.tar.gz myproject-1.0.1.tar.gz.asc
# 验证$ gpg --detach-sign -a <filename>
3. 使用方式
Twine 库的使用方式和方法也很简单且易用!
- setpy.py
from setuptools import find_packages, setup, Command
class UploadCommand(Command): description = 'Build and publish the package.' user_options = []
@staticmethod def status(s): print('\033[1m{0}\033[0m'.format(s))
def initialize_options(self): pass
def finalize_options(self): pass
def run(self): self.status('Building Source and Wheel distribution…') os.system('{0} setup.py sdist bdist_wheel'.format(sys.executable))
self.status('Uploading the package to PyPI via Twine…') os.system('twine upload dist/*')
self.status('Pushing git tags…') os.system('git tag v{0}'.format(about['__version__'])) os.system('git push --tags') sys.exit()
setup( name=NAME, version=about['__version__'], description=DESCRIPTION, cmdclass={ 'upload': UploadCommand, })
- run
$ python setup.py upload
4. 命令介绍
下面主要罗列了 twine 工具的相关子命令!
[1] twine upload
上传一个或多个发行版到存储库
$ twine upload -husage: twine upload [-h] [-r REPOSITORY] [--repository-url REPOSITORY_URL] [-s] [--sign-with SIGN_WITH] [-i IDENTITY] [-u USERNAME] [-p PASSWORD] [-c COMMENT] [--config-file CONFIG_FILE] [--skip-existing] [--cert path] [--client-cert path] [--verbose] [--disable-progress-bar] dist [dist ...]
positional arguments: dist The distribution files to upload to the repository (package index). Usually dist/* . May additionally contain a .asc file to include an existing signature with the file upload.
optional arguments: -h, --help show this help message and exit -r REPOSITORY, --repository REPOSITORY The repository (package index) to upload the package to. Should be a section in the config file (default: pypi). (Can also be set via TWINE_REPOSITORY environment variable.) --repository-url REPOSITORY_URL The repository (package index) URL to upload the package to. This overrides --repository. (Can also be set via TWINE_REPOSITORY_URL environment variable.) -s, --sign Sign files to upload using GPG. --sign-with SIGN_WITH GPG program used to sign uploads (default: gpg). -i IDENTITY, --identity IDENTITY GPG identity used to sign files. -u USERNAME, --username USERNAME The username to authenticate to the repository (package index) as. (Can also be set via TWINE_USERNAME environment variable.) -p PASSWORD, --password PASSWORD The password to authenticate to the repository (package index) with. (Can also be set via TWINE_PASSWORD environment variable.) --non-interactive Do not interactively prompt for username/password if the required credentials are missing. (Can also be set via TWINE_NON_INTERACTIVE environment variable.) -c COMMENT, --comment COMMENT The comment to include with the distribution file. --config-file CONFIG_FILE The .pypirc config file to use. --skip-existing Continue uploading files if one already exists. (Only valid when uploading to PyPI. Other implementations may not support this.) --cert path Path to alternate CA bundle (can also be set via TWINE_CERT environment variable). --client-cert path Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. --verbose Show verbose output. --disable-progress-bar Disable the progress bar.
[2] twine check
检查发布的详细说明是否可以在 PyPI 上正确呈现
$ twine check -husage: twine check [-h] [--strict] dist [dist ...]
positional arguments: dist The distribution files to check, usually dist/*
optional arguments: -h, --help show this help message and exit --strict Fail on warnings
[3] twine register
现在已经基本不再使用了
$ twine register -h
usage: twine register [-h] -r REPOSITORY [--repository-url REPOSITORY_URL] [-u USERNAME] [-p PASSWORD] [-c COMMENT] [--config-file CONFIG_FILE] [--cert path] [--client-cert path] package
positional arguments: package File from which we read the package metadata.
optional arguments: -h, --help show this help message and exit -r REPOSITORY, --repository REPOSITORY The repository (package index) to register the package to. Should be a section in the config file. (Can also be set via TWINE_REPOSITORY environment variable.) Initial package registration no longer necessary on pypi.org: https://packaging.python.org/guides/migrating-to-pypi- org/ --repository-url REPOSITORY_URL The repository (package index) URL to register the package to. This overrides --repository. (Can also be set via TWINE_REPOSITORY_URL environment variable.) -u USERNAME, --username USERNAME The username to authenticate to the repository (package index) as. (Can also be set via TWINE_USERNAME environment variable.) -p PASSWORD, --password PASSWORD The password to authenticate to the repository (package index) with. (Can also be set via TWINE_PASSWORD environment variable.) --non-interactive Do not interactively prompt for username/password if the required credentials are missing. (Can also be set via TWINE_NON_INTERACTIVE environment variable.) -c COMMENT, --comment COMMENT The comment to include with the distribution file. --config-file CONFIG_FILE The .pypirc config file to use. --cert path Path to alternate CA bundle (can also be set via TWINE_CERT environment variable). --client-cert path Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
5. 配置文件
Twine 库还可以从配置文件中读取配置。
Twine 可以从 .pypirc 文件中读取项目配置,该文件位于主目录中或通过 --config-file 选项来制定来源。有关编写和使用 .pypirc 的详细信息,请参见《Python 打包用户指南》中的规范。
TWINE_USERNAME-
- 用于存储库进行身份验证的用户名
TWINE_PASSWORD-
- 用于存储库进行身份验证的密码
TWINE_REPOSITORY-
- 存储库配置
TWINE_REPOSITORY_URL-
- 要使用的存储库
URL地址
- 要使用的存储库
TWINE_CERT-
- 自定义
CA证书 - 用于自签名或不受信任证书的存储库
- 自定义
TWINE_NON_INTERACTIVE-
- 不要以交互方式提示输入用户名和密码
以上就是本次分享的所有内容,想要了解更多 python 知识欢迎前往公众号:Python 编程学习圈 ,发送 “J” 即可免费获取,每日干货分享