官方文档地址:https://setuptools.pypa.io/en/latest/userguide/quickstart.html
pip install --upgrade setuptools wheel pip install --upgrade build
项目结构
demo -- setup.py
-- project1 -- utils/
-- main.py
-- readme.md
-- requirements.txt
1、setup.py方式打包
from setuptools import setup, find_packages, find_namespace_packages
#如果每个包中都有__init__.py则可以使用find_packages
setup(
name="project1", #name最好和项目名保持一致,即project1
version="0.1",
author='wu',
description='test',
packages=find_namespace_packages(exclude=['venv*'],where='project1'),
package_data={
'': ['log/*.txt']
},
entry_points={
'console_scripts': [
'cli-name = src.utils.format:test',
]
}
)
打包
python -m build #打包成wheel和tar.gz格式
pip install /dist/demo-0.1-py3-none-any.whl
安装后,即可使用cli-name命令行,以及在项目中引入包
from project1.utils.format import test
其他打包方式
python setup.py sdist --formats=zip
python setup.py bdist
安装
python setup.py install
argument name |
value |
type |
---|---|---|
name |
The name of the package |
a string |
version |
The version number of the package; see distutils.version |
a string |
description |
A single line describing the package |
a string |
long_description |
Longer description of the package |
a string |
author |
The name of the package author |
a string |
author_email |
The email address of the package author |
a string |
maintainer |
The name of the current maintainer, if different from the author. Note that if the maintainer is provided, distutils will use it as the author in |
a string |
maintainer_email |
The email address of the current maintainer, if different from the author |
a string |
url |
A URL for the package (homepage) |
a string |
download_url |
A URL to download the package |
a string |
packages |
A list of Python packages that distutils will manipulate |
a list of strings |
py_modules |
A list of Python modules that distutils will manipulate |
a list of strings |
scripts |
A list of standalone script files to be built and installed |
a list of strings |
ext_modules |
A list of Python extensions to be built |
a list of instances of distutils.core.Extension |
classifiers |
A list of categories for the package |
a list of strings; valid classifiers are listed on PyPI. |
distclass |
the Distribution class to use |
a subclass of distutils.core.Distribution |
script_name |
The name of the setup.py script - defaults to |
a string |
script_args |
Arguments to supply to the setup script |
a list of strings |
options |
default options for the setup script |
a dictionary |
license |
The license for the package |
a string |
keywords |
Descriptive meta-data, see PEP 314 |
a list of strings or a comma-separated string |
platforms |
a list of strings or a comma-separated string |
|
cmdclass |
A mapping of command names to Command subclasses |
a dictionary |
data_files |
A list of data files to install |
a list |
package_dir |
A mapping of package to directory names |
a dictionary |
2、pyproject.toml方式
文档地址:https://packaging.python.org/en/latest/specifications/declaring-project-metadata/
上述目录根目录中新建pyproject.toml,setup.py中注释掉setup()方法中的参数,仅保留如下内容
from setuptools import setup
setup()
#pyproject.toml
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
[project]
name = "project1"
authors = [
{name = "wu", email = "[email protected]"},
]
description = "My package description"
readme = "README.rst"
requires-python = ">=3.7"
keywords = ["one", "two"]
license = {text = "BSD-3-Clause"}
classifiers = [
"Framework :: Django",
"Programming Language :: Python :: 3",
]
dynamic = ["version"]
[project.scripts]
cli-name = "project1.utils.format:test"
#其他可用的table如下
#[project.optional-dependencies]
#[project.urls]
#[project.gui-scripts]
#[project.entry-points."spam.magical"]
python -m build #打包
3、 setup.cfg打包
文档地址:https://setuptools.pypa.io/en/latest/userguide/declarative_config.html
新建setup.cfg文件
[metadata]
name = porject1
author = wu
author_email = [email protected]
description = My package description
long_description = test
keywords = one, two
license = BSD-3-Clause
classifiers =
Framework :: Django
Programming Language :: Python :: 3
[options]
zip_safe = True
include_package_data = True
packages = find:
python_requires = >=3.7
[options.package_data]
* = *.txt, *.rst
[options.entry_points]
console_scripts =
cli-name = project1.utils.format:test
[options.extras_require]
pdf = ReportLab>=1.2; RXP
rest = docutils>=0.3; pack ==1.1, ==1.3
[options.packages.find]
exclude =
venv*
where=project1
执行
python -m build
三种方式均能起到打包的作用