【Python】安装与分发模块

 

目录

一. 问题

二. 知识储备

2.1 Python 相关

2.1.1 Python Packaging Index

2.1.2 setuptools

2.1.3 wheel

2.1.4 twine

2.1.5 tox

2.2 辅助相关

2.2.1 Tracis CI

2.3 扩展相关

2.3.1 YAML

三. 工程结构

四. 安装

五. 配置

5.1 pyproject.toml

5.2 MANIFEST.in

5.3 tox.ini

5.4 setup.cfg

5.5 README.md

六. 测试

七. 构建

7.1 build tar.gz 方式

7.2 build whl 方式

八. 分发(发布)

8.1 注册账号

8.2 检查软件包

8.3 上传软件包

五. 参考


 

一. 问题

最近在整理之前的代码,发现好多都杂乱无章的;

软件模块代码重用(复用)是个好东西;

为何 python 不能像 Java 那种写一个 组件/包,然后发布到 Maven 仓库当中去呢?

后来去官网上,折腾了折腾,找了找,还真有,顺便摘抄记录一下;

 

作为一个流行的开源开发项目,Python拥有一个活跃的贡献者和用户支持社区,这些社区也可以让他们的软件可供其他Python开发人员在开源许可条款下使用。

这允许Python用户有效地共享和协作,从其他人已经创建的解决方案中受益于常见(有时甚至是罕见的)问题,以及可以提供他们自己的解决方案。

本指南涵盖了分发部分的流程。有关安装其他Python项目的指南,请参阅 安装指南和分发(发布)指南。

 

二. 知识储备

 

在世界上大多数地方,软件自动受版权保护。这意味着其他开发人员需要明确的权限来复制,使用,修改和重新分发软件。

开源许可是一种以相对一致的方式明确授予此类权限的方式,允许开发人员通过为各种问题免费提供通用解决方案来有效地共享和协作。这使得许多开发人员可以将更多时间用于关注他们特定情况相对独特的问题。

Python提供的分发工具旨在使开发人员选择开源时,可以合理地直接将其自己的贡献回馈到该公共软件池。

无论该软件是否作为开源软件发布,相同的分发工具也可用于在组织内分发软件。

 

2.1 Python 相关

2.1.1 Python Packaging Index

Python Packaging Index 是一个开源许可的软件包公共存储库,可供所有 Python 用户使用。

 

2.1.2 setuptools

setuptools (在很大程序上)是作为 distutils 的取代者,于 2004 年首次发布。它对未经修改的 distutils 工具最重要的补充是能够声明对其他包的依赖。目前它被推荐用来替代 distutils,其更新更为频繁,在更为多样的 Python 版本之上为最新的包标准提供持续支持。

 

2.1.3 wheel

wheel (在此上下文中)是一个将 bdist_wheel 命令添加到 distutils/setuptools 的项目。这产生了一个跨平台的二进制打包格式(称为“轮子”或“轮子文件”,并在 PEP 427 中定义),它允许在系统上安装Python库,甚至包括二进制扩展的库,而不需在本地进行构建。

 

2.1.4 twine

使用它来将项目发行版上传到PyPI。

 

2.1.5 tox

tox 旨在自动化和标准化Python中的测试。它是简化Python软件的打包,测试和发布过程的更大愿景的一部分。

tox 是通用的virtualenv 管理和测试命令行工具,可用于:

  • 检查您的软件包是否使用不同的Python版本和解释器正确安装;

  • 在每个环境中运行测试,配置您选择的测试工具;

  • 作为持续集成服务器的前端,大大减少了样板文件,并将基于CI 和shell 的测试合并在一起;

需要使用如下命令安装:

# python2
$ python2 -m pip install tox

# python3
$ python3 -m pip install tox

2.2 辅助相关

2.2.1 Tracis CI

最近看到的好多 Github 项目都包含一个.travis.yml 文件,这是用来干啥的呢?

编写代码只是软件开发的一小部分,更多的时间往往花在构建(build)和测试(test)。为了提高软件开发的效率,构建和测试的自动化工具层出不穷。Travis CI 就是这类工具之中,市场份额最大的一个。

官方文档,请参考:

  1. https://www.travis-ci.com/
  2. https://docs.travis-ci.com/

 

2.3 扩展相关

2.3.1 YAML

Travis CI 脚本编写语言采用 YAML 格式,需要了解各个语法写法和习惯;

官方文档,请参考:

  1. https://yaml.org/
  2. https://yaml.org/spec/1.2/spec.html
  3. https://pyyaml.org/
  4. https://yaml.readthedocs.io/en/latest/

 

本文档假设都是在 virtualenv 环境下,进行安装与分发操作;

 

三. 工程结构

项目工程结构,举例如下:

# soft package tree

--- project root
    |
    | --- .tox
    | --- .vscode
    | --- build
    | --- dist
    | --- doc
        --- ...
            --- README.md
    | --- src
        --- com
            --- dvsnier
                --- ...
    | --- test
        --- com
            --- dvsnier
                --- ...
    | --- venv
    | --- venv2
    | --- .editorconfig
    | --- .gitignore
    | --- LICENSE.txt
    | --- MANIFEST.in
    | --- pyproject.toml
    | --- README.md
    | --- setup.cfg
    | --- tox.ini

具体细节可参考,  Python-Demo 项目;

 

四. 安装

默认情况下可能未安装 pip,一种可选解决方案是:

# POSIX python2
python2 -m ensurepip --default-pip

# POSIX python3
python3 -m ensurepip --default-pip



# windows python2
python2 -m ensurepip --default-pip --user

# windows python3
python3 -m ensurepip --default-pip --user

还有其他资源可用来 安装 pip; 

pip 是首选的安装程序。从Python2.7.9开始,默认情况下Python二进制安装程序中包含了它。

 

virtualenv 是一个半隔离的Python环境,它允许安装包供特定应用程序使用,而不是安装在系统范围内;

virtualenv 是用于创建虚拟环境的第三方工具,它默认将pip安装到所有创建的虚拟环境中。

 

当前,有两种用于创建Python 虚拟环境的常用工具:

  • venv 在Python 3.3和更高版本中默认可用,并在Python 3.4和更高版本中将pip 和setuptools 安装 到创建的虚拟环境中。

  • virtualenv 需要单独安装,但支持Python 2.7+ 和Python 3.3+ ,并且pip, setuptools 和wheel 默认情况下始终安装到创建的虚拟环境中(无论Python版本如何)。

 

有关 virtualenv  安装请参考如下链接:

1.【Mac】【Python】VSCode + Python 3.x + virtualenv

2.【Windows】【Python】VSCode + Python 2.x + virtualenv

 

标准打包工具完全是针对命令行使用方式来设计的。

在命令行中指定一个准确或最小版本也是可以的。 当使用比较运算符例如 >< 或其他某些可以被终端所解析的特殊字符时,包名称与版本号应当用双引号括起来:

# python install specific version
python -m pip install SomePackage==1.0.4    # specific version
python -m pip install "SomePackage>=1.0.4"  # minimum version


# POSIX python2
python2 -m pip install -e 
python2 -m pip install 

# POSIX python3
python3 -m pip install -e 
python3 -m pip install 


# Windows python2
py -2 -m pip install -e  --user
py -2 -m pip install  --user
python2 -m pip install -e  --user
python2 -m pip install  --user

# Windows python3
py -3 -m pip install -e  --user
py -3 -m pip install  --user
python3 -m pip install -e  --user
python3 -m pip install  --user

通常,如果一个匹配的模块已安装,尝试再次安装将不会有任何效果。 要升级现有模块必须显式地发出请求:

# python upgrade package
python -m pip install --upgrade SomePackage


# POSIX python2
python2 -m pip install --upgrade SomePackage
python2 -m pip install --upgrade SomePackage

# POSIX python3
python3 -m pip install --upgrade SomePackage
python3 -m pip install --upgrade SomePackage


# Windows python2
py -2 -m pip install --upgrade SomePackage --user
py -2 -m pip install --upgrade SomePackage --user
python2 -m pip install --upgrade SomePackage --user
python2 -m pip install --upgrade SomePackage --user

# Windows python3
py -3 -m pip install --upgrade SomePackage --user
py -3 -m pip install --upgrade SomePackage --user
python3 -m pip install --upgrade SomePackage --user
python3 -m pip install --upgrade SomePackage --user

更多有关 pip 及其功能的信息和资源可以在 Python 软件包用户指南 中找到。

参考 Python Packaging User Guide: Installing Python Distribution Packages

 

首先检查是否安装如下依赖:

  1. pip
  2. flake8 (可选)
  3. virtualenv
  4. setuptools
  5. wheel
  6. discover
  7. tox
  8. toml (可选)
  9. tox-travis (可选)
  10. build
  11. twine

如若没有,请使用pip 命令安装如下软件包:

# python2 pip version
python2 -m pip --version

python2 -m pip install flake8
python2 -m pip install virtualenv
python2 -m pip install setuptools
python2 -m pip install wheel
python2 -m pip install discover
python2 -m pip install tox
python2 -m pip install toml
python2 -m pip install tox-travis
python2 -m pip install build
python2 -m pip install twine

# python3 pip version
python3 -m pip --version

python3 -m pip install flake8
python3 -m pip install virtualenv
python3 -m pip install setuptools
python3 -m pip install wheel
python3 -m pip install discover
python3 -m pip install tox
python3 -m pip install toml
python3 -m pip install tox-travis
python3 -m pip install build
python3 -m pip install twine

其实,一般安装有 pip 软件包的,一般都会有setuptools 和wheel 软件包附带安装;

 

五. 配置

5.1 pyproject.toml

首先配置 pyproject.toml ,模板固定, 一般不需大范围改动;

[build-system]
# https://setuptools.readthedocs.io/en/latest/build_meta.html
# These are the assumed default build requirements from pip:
# https://pip.pypa.io/en/stable/cli/pip/#pep-517-and-518-support
#
# As of version 10.0, pip supports projects declaring dependencies that are
# required at install time using a pyproject.toml file, in the form described
# in PEP 518. When building a project, pip will install the required
# dependencies locally, and make them available to the build process.
# Furthermore, from version 19.0 onwards, pip supports projects specifying the
# build backend they use in pyproject.toml, in the form described in PEP 517.
#
requires = [
    "setuptools>=40.8.0",
    "wheel"
]
build-backend = "setuptools.build_meta"

 

5.2 MANIFEST.in

然后配置 MANIFEST.in 软件包应当包含哪些信息,哪些排除配置信息, 如下所示:

# https://packaging.python.org/guides/using-manifest-in/

include pyproject.toml

# Include the README
include *.md

# Include the license file
include LICENSE.txt

同上,模板一般都是固定结构,无需太范围改动;

 

5.3 tox.ini

再然后配置 tox 脚本自动化测试, 指定Python 虚拟环境版本, 配置信息如下:

# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
# For information see https://tox.readthedocs.io/en/latest/examples.html

[tox]
envlist = py27, py39

minversion = 3.23.1

# Activate isolated build environment. tox will use a virtual environment
# to build a source distribution from the source tree. For build tools and
# arguments use the pyproject.toml file as specified in PEP-517 and PEP-518.
isolated_build = true

# install testing framework
# ... or install anything else you might need here
[testenv]
platform = linux: linux
           macos: darwin
           windows: win32
; alwayscopy = True
allowlist_externals =
    /bin/bash
changedir =
    tests
deps =
    build
    discover
    twine
commands =
    discover
    ; python -m unittest discover

 

5.4 setup.cfg

最后配置 setup.cfg 构建脚本, 指定软件包所要构建的软件细节部分, 举例配置信息如下:

[metadata]
# 1. https://setuptools.readthedocs.io/en/latest/
# 2. https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html
# 3. https://setuptools.readthedocs.io/en/latest/references/keywords.html
name = com.dvsnier.directory
version = 0.0.1.dev1
author = dvsnier
author_email = [email protected]
description = this is dvsnier directory.
long_description = file: ./doc/description/directory/README.md
long_description_content_type = text/markdown
keywords = dir, development
url = https://github.com/Alinvor/Python-DeMo
project_urls =
    Documentation = https://packaging.python.org/tutorials/distributing-packages/
    Funding = https://donate.pypi.org
    Wiki = https://github.com/Alinvor/Python-DeMo/wiki
    Bug_Tracker = https://github.com/Alinvor/Python-DeMo/issues
    Source = https://github.com/Alinvor/Python-DeM
platforms = any
# classifiers
# Development Status :: 3 - Alpha
# Development Status :: 4 - Beta
# Development Status :: 5 - Production/Stable
classifiers =
    Development Status :: 3 - Alpha
    Programming Language :: Python :: 2.7
    Programming Language :: Python :: 3.8
    Programming Language :: Python :: 3.9
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent

python_requires =
    >=2.7
    <4

# This includes the license file(s) in the wheel.
# https://wheel.readthedocs.io/en/stable/user_guide.html#including-license-files-in-the-generated-wheel-file
# https://choosealicense.com/
license = MIT License
license_files =
    LICENSE.txt

[options]
package_dir =
    = src

packages = find:

[options.packages.find]
where = src
#
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#wheels
#
[bdist_wheel]
universal = 1

软件包名称和软件包版本信息, 一定要明确具体发布规则;

实际项目中, 可依据如上配置信息稍加修改和增添;

 

5.5 README.md

更新README.md 版本和摘要信息;

 

六. 测试

首次运行tox 命令,可使用tox-quickstart 命令, 依据简短提示信息进行配置:

tox-quickstart

后续运行tox 命令,可执行自动化脚本测试:

(venv2) $ tox

.package create: /Users/xxx/Python-DeMo/.tox/.package
.package installdeps: setuptools>=40.8.0, wheel
py27 create: /Users/xxx/Python-DeMo/.tox/py27
py27 installdeps: build, discover, twine
py27 inst: /Users/xxx/Python-DeMo/.tox/.tmp/package/1/com.dvsnier.directory-0.0.1.dev1.tar.gz
py27 installed: DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.,appdirs==1.4.4,bleach==3.3.0,build==0.4.0,certifi==2020.12.5,chardet==4.0.0,com.dvsnier.directory @ file:///Users/xxx/Python-DeMo/.tox/.tmp/package/1/com.dvsnier.directory-0.0.1.dev1.tar.gz,configparser==4.0.2,contextlib2==0.6.0.post1,discover==0.4.0,distlib==0.3.1,docutils==0.17.1,filelock==3.0.12,idna==2.10,importlib-metadata==2.1.1,importlib-resources==3.3.1,packaging==20.9,pathlib2==2.3.5,pep517==0.10.0,pkginfo==1.7.0,Pygments==2.5.2,pyparsing==2.4.7,readme-renderer==29.0,requests==2.25.1,requests-toolbelt==0.9.1,scandir==1.10.0,singledispatch==3.6.2,six==1.16.0,toml==0.10.2,tqdm==4.61.0,twine==1.15.0,typing==3.10.0.0,urllib3==1.26.5,virtualenv==20.4.7,webencodings==0.5.1,zipp==1.2.0
py27 run-test-pre: PYTHONHASHSEED='1963849366'
py27 run-test: commands[0] | discover

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
py39 create: /Users/xxx/Python-DeMo/.tox/py39
py39 installdeps: build, discover, twine
py39 inst: /Users/xxx/Python-DeMo/.tox/.tmp/package/1/com.dvsnier.directory-0.0.1.dev1.tar.gz

 

七. 构建

可使用如下命令, 进行xxx.tar.gz 源码包和xxx.whl 二进制包构建, 命令如下:

# python build tar.gz and whl
python2 -m build

# python build tar.gz
python2 -m build --sdist
# python build whl
python2 -m build --wheel


# python build tar.gz and whl
python3 -m build

# python build tar.gz
python3 -m build --sdist
# python build whl
python3 -m build --wheel

 

7.1 build tar.gz 方式

构建tar.gz 方式,运行如下:


(venv2) $ python2 -m build --sdist
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting wheel
  Using cached wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Collecting setuptools>=40.8.0
  Using cached setuptools-44.1.1-py2.py3-none-any.whl (583 kB)
Installing collected packages: wheel, setuptools
Successfully installed setuptools-44.1.1 wheel-0.36.2
running egg_info
writing src/com.dvsnier.directory.egg-info/PKG-INFO
writing top-level names to src/com.dvsnier.directory.egg-info/top_level.txt
writing dependency_links to src/com.dvsnier.directory.egg-info/dependency_links.txt
reading manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
running sdist
running egg_info
writing src/com.dvsnier.directory.egg-info/PKG-INFO
writing top-level names to src/com.dvsnier.directory.egg-info/top_level.txt
writing dependency_links to src/com.dvsnier.directory.egg-info/dependency_links.txt
reading manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
running check
creating com.dvsnier.directory-0.0.1.dev1
creating com.dvsnier.directory-0.0.1.dev1/src
creating com.dvsnier.directory-0.0.1.dev1/src/com
creating com.dvsnier.directory-0.0.1.dev1/src/com.dvsnier.directory.egg-info
creating com.dvsnier.directory-0.0.1.dev1/src/com/dvsnier
creating com.dvsnier.directory-0.0.1.dev1/src/com/dvsnier/dir
copying files to com.dvsnier.directory-0.0.1.dev1...
copying LICENSE.txt -> com.dvsnier.directory-0.0.1.dev1
copying MANIFEST.in -> com.dvsnier.directory-0.0.1.dev1
copying README.md -> com.dvsnier.directory-0.0.1.dev1
copying pyproject.toml -> com.dvsnier.directory-0.0.1.dev1
copying setup.cfg -> com.dvsnier.directory-0.0.1.dev1
copying src/com/__init__.py -> com.dvsnier.directory-0.0.1.dev1/src/com
copying src/com.dvsnier.directory.egg-info/PKG-INFO -> com.dvsnier.directory-0.0.1.dev1/src/com.dvsnier.directory.egg-info
copying src/com.dvsnier.directory.egg-info/SOURCES.txt -> com.dvsnier.directory-0.0.1.dev1/src/com.dvsnier.directory.egg-info
copying src/com.dvsnier.directory.egg-info/dependency_links.txt -> com.dvsnier.directory-0.0.1.dev1/src/com.dvsnier.directory.egg-info
copying src/com.dvsnier.directory.egg-info/top_level.txt -> com.dvsnier.directory-0.0.1.dev1/src/com.dvsnier.directory.egg-info
copying src/com/dvsnier/__init__.py -> com.dvsnier.directory-0.0.1.dev1/src/com/dvsnier
copying src/com/dvsnier/dir/__init__.py -> com.dvsnier.directory-0.0.1.dev1/src/com/dvsnier/dir
copying src/com/dvsnier/dir/common_dir.py -> com.dvsnier.directory-0.0.1.dev1/src/com/dvsnier/dir
Writing com.dvsnier.directory-0.0.1.dev1/setup.cfg
Creating tar archive
removing 'com.dvsnier.directory-0.0.1.dev1' (and everything under it)

 

7.2 build whl 方式

构建whl 方式,运行如下:

(venv2) $ python2 -m build --wheel
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting wheel
  Using cached wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Collecting setuptools>=40.8.0
  Using cached setuptools-44.1.1-py2.py3-none-any.whl (583 kB)
Installing collected packages: wheel, setuptools
Successfully installed setuptools-44.1.1 wheel-0.36.2
running egg_info
writing src/com.dvsnier.directory.egg-info/PKG-INFO
writing top-level names to src/com.dvsnier.directory.egg-info/top_level.txt
writing dependency_links to src/com.dvsnier.directory.egg-info/dependency_links.txt
reading manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Requirement already satisfied: wheel in /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/build-env-7yTzk5/lib/python2.7/site-packages (from -r /var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/build-reqs-UGtALH.txt (line 1)) (0.36.2)
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/com
copying src/com/__init__.py -> build/lib/com
creating build/lib/com/dvsnier
copying src/com/dvsnier/__init__.py -> build/lib/com/dvsnier
creating build/lib/com/dvsnier/dir
copying src/com/dvsnier/dir/__init__.py -> build/lib/com/dvsnier/dir
copying src/com/dvsnier/dir/common_dir.py -> build/lib/com/dvsnier/dir
installing to build/bdist.macosx-10.15-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-10.15-x86_64
creating build/bdist.macosx-10.15-x86_64/wheel
creating build/bdist.macosx-10.15-x86_64/wheel/com
copying build/lib/com/__init__.py -> build/bdist.macosx-10.15-x86_64/wheel/com
creating build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier
copying build/lib/com/dvsnier/__init__.py -> build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier
creating build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier/dir
copying build/lib/com/dvsnier/dir/__init__.py -> build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier/dir
copying build/lib/com/dvsnier/dir/common_dir.py -> build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier/dir
running install_egg_info
running egg_info
writing src/com.dvsnier.directory.egg-info/PKG-INFO
writing top-level names to src/com.dvsnier.directory.egg-info/top_level.txt
writing dependency_links to src/com.dvsnier.directory.egg-info/dependency_links.txt
reading manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'src/com.dvsnier.directory.egg-info/SOURCES.txt'
Copying src/com.dvsnier.directory.egg-info to build/bdist.macosx-10.15-x86_64/wheel/com.dvsnier.directory-0.0.1.dev1-py2.7.egg-
(venv2) $ python2 -m pip freeze > ./Temp/python_pip_freeze.txt

 

八. 分发(发布)

分发,即发布软件包模块;

标准库不包括支持现代Python打包标准的构建工具,因为核心开发团队已经发现,即使在旧版本的Python上,使用一致工作的标准工具也很重要。

可以通过在命令行调用 pip 模块来安装当前推荐的构建和分发工具:

# POSIX python2
python2 -m pip install setuptools wheel twine

# POSIX python3
python3 -m pip install setuptools wheel twine


# Windwos python2
python2 -m pip install setuptools wheel twine --user

# Windwos python3
python3 -m pip install setuptools wheel twine --user

Python打包指南包含有关 currently recommended tools 的更多详细信息。

参见 Python Packaging User Guide: Binary Extensions。

 

《Python打包用户指南》涵盖了创建项目所涉及的各种关键步骤和元素:

  • 项目的结构

  • 项目的构建与打包

  • 上传项目到 Python Packaging Index

 

8.1 注册账号

  1. 账户注册地址: https://pypi.org/account/register/
  2. 创建一个PyPI API令牌, 以便能够安全地上传您的项目;
  3. 为了避免每次上载时都必须复制和粘贴令牌,可以创建一个$HOME/.pypirc 文件, 参考如下:
[pypi]
username = __token__
password = 

 

8.2 检查软件包

检查软件包, 命令:

(venv2) $ twine check dist/*
Checking dist/com.dvsnier.directory-0.0.1.dev1-py2.py3-none-any.whl: PASSED
Checking dist/com.dvsnier.directory-0.0.1.dev1.tar.gz: PASSED

8.3 上传软件包

上传软件包, 命令:

(venv2) $ twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Uploading com.dvsnier.directory-0.0.1.dev1-py2.py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████████████| 7.80k/7.80k [00:02<00:00, 3.35kB/s]
Uploading com.dvsnier.directory-0.0.1.dev1.tar.gz
100%|█████████████████████████████████████████████████████████████████████████████████████| 9.15k/9.15k [00:01<00:00, 6.15kB/s]

View at:
https://pypi.org/project/com.dvsnier.directory/0.0.1.dev1/

 

五. 参考

  1. https://pypi.org/
  2. https://docs.python.org/zh-cn/2.7/index.html
  3. https://setuptools.readthedocs.io/en/latest/
  4. https://docs.python.org/zh-cn/2.7/library/distutils.html#module-distutils
  5. https://pip.readthedocs.io/en/latest/
  6. https://wheel.readthedocs.io/en/latest/
  7. https://setuptools.readthedocs.io/en/latest/
  8. https://tox.readthedocs.io/en/latest/
  9. https://www.python.org/dev/peps/pep-0427
  10. https://www.pypa.io/en/latest/
  11. https://github.com/pypa
  12. https://github.com/blog/2463-github-welcomes-all-ci-tools
  13. https://github.com/Alinvor/Python-DeMo/tree/feature_com.dvsnier.directory#python-demo
  14. https://packaging.python.org/
  15. https://packaging.python.org/tutorials/installing-packages/
  16. https://packaging.python.org/tutorials/installing-packages/#install-pip-setuptools-and-wheel
  17. https://packaging.python.org/en/latest/current/#packaging-tool-recommendations
  18. https://packaging.python.org/en/latest/distributing/
  19. https://packaging.python.org/en/latest/distributing/#packaging-your-project
  20. https://packaging.python.org/en/latest/distributing/#uploading-your-project-to-pypi
  21. https://packaging.python.org/en/latest/extensions
  22. https://packaging.python.org/key_projects/#pip
  23. https://packaging.python.org/key_projects/#setuptools
  24. https://packaging.python.org/key_projects/#wheel
  25. https://packaging.python.org/key_projects/#virtualenv
  26. https://packaging.python.org/glossary/#term-Distribution-Package
  27. https://packaging.python.org/glossary/#term-Python-Package-Index-PyPI
  28. https://pypi.org/account/register/
  29. https://pypi.org/help/#apitoken
  30. https://docs.python.org/zh-cn/2.7/installing/index.html
  31. https://docs.python.org/zh-cn/2.7/installing/index.html#installing-index
  32. https://docs.python.org/zh-cn/2.7/distributing/index.html
  33. https://docs.python.org/zh-cn/2.7/distributing/index.html#distributing-index
  34. https://docs.python.org/3/library/venv.html
  35. https://www.travis-ci.com/
  36. https://docs.travis-ci.com/
  37. https://yaml.org/
  38. https://yaml.org/spec/1.2/spec.html
  39. https://pyyaml.org/
  40. https://yaml.readthedocs.io/en/latest/

 

 

(完)

 

 

你可能感兴趣的:(Python,python,build,twine)