ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

packaging python project by setuptools - basic usage

2022-06-22 21:31:36  阅读:206  来源: 互联网

标签:packaging install package python setup project setuptools packages


packaging python project by setuptools - basic usage


setuptools

fully-featured, actively-maintained, python-package-manager
commonly used tool

installation

python -m pip install setuptools

can also install build using pip

python -m pip install build

use setuptools

if have a Python-proj package , must provide a pyproject.toml file

and containing a build-system section

example:

[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'

in a addition to specifying a build system contain dependencies you also will need to add some package imformation as metadata, content, dependencies, etc.

can be done in same setuptools.toml , or in a separated one : setup.cfg or setup.py

the following example a demonstrates minimun configuration:

# pyproject.tmol
[build-system]
name='example-package'
dependencies = [
    "requests",
    'importlib-metadata; python_version == "3.8"',
]
# setup.py
from setuptools import setup

setup(
    name="example-package",
    version="0.1",
    install_requires = [
        "requests",
        "importlib-metadata; python_version == "3.8",
    ]
)
# setup.cfg
[metadata]
name = 'example-package',
version = 0.1

[options]
install_requires = 
    requests
    importlib-metadata; python_version == "3.8"

finally, distributing you python code into something that looks like the following:

example-package
|
|---> pyproject.toml
|     or `setup.py` or `setup.cfg`
|---> package
|     |
|     |---> __init__.py
|     |---> other code file...


package discovery

specify all proj-package and all namespace is vital , if you proj that follow a simple directory structure , setuptools can automatically detect all packages and namespaces

as shown in the example below:

# setup.py
from setuptools import setup, find_packages

setup(
    packages=find_packages(),
    ...
)

# if proj is complex structure

setup(
    package=find_packages(
        where=".",
        include=['mypackage folder'],
        exclude=['some folder or file like test code']
    ),
    ....
)
# pyproject.toml, addtional section `[tool.setuptools.packages.find]`
[tool.setuptools.package.find]
where = ["."]
include = ["..."]
exclude = ["..."]

[options]
packages = find:

[options.packages.find]
include = mypackage*
exclude = mypackage.tests*

entry points and automatic script creation

entry points means cli command name , like pip install instead of having to type python -m pip install

# setup.py

setup(
    ...
    entry_points = {
        'console_scripts': [
            'mycliname=mypackage.somemodule:some_func'
        ]
    }
)
[project.script]
mycliname = 'mypackage.somemodule:some_func'
[options.entry_potins]
console_scripts = 
    mycliname = mypackage.somemodule:some_func

Dependency management

setuptools must specify all dependencies in configuration
its to be automatically installed when the package itself is installed

that looks like the following below:

# setup.py
setup(
    install_requires=[
        "requests",
        "flask == 0.1"
    ],
)
# pyproject.toml
[project]
dependencies = [
    "requests",
    "flask == 0.1"
]
# setup.cfg
[options]
install_requires=
    requests
    flask == 0.1

标签:packaging,install,package,python,setup,project,setuptools,packages
来源: https://www.cnblogs.com/xiuneng/p/16403114.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有