Python零基础入门--基础(十)-- Setuptools

Setuptools是Python distutils的增强版,便于开发人员构建和分发Python包。本文提供开发指南,介绍了安装方法,即使用pip install -U setuptools;还说明了基本使用方式,如从setuptools导入东西,运行脚本生成分发等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Setuptools是Python distutils的增强功能集合版 ,允许开发人员更轻松地构建和分发Python包,尤其是那些依赖于其他包的包。

对用户使用Setuptools进行构建和分发包就像基于distutils的普通Python包。您的用户无需安装甚至不需要了解setuptools即可使用它们,并且您不必在发行版中包含整个setuptools包。如果用户从源代码构建程序包并且尚未安装合适的版本,通过仅包含一个引导程序模块(12K .py文件),程序包将自动下载并安装setuptools。

一、开发指南

1.1 安装setuptools

要安装最新版本的setuptools,请使用:pip install -U setuptools,更多信息请参考安装指南

1.2 基本使用

对于setuptools的基本使用,只需从setuptools而不是distutils导入东西。这是使用setuptools的最基础设置脚本:

setuptoolsCarl 
|-- myapp
|   |-- __init__.py
|   |-- helloCarl.py  
|-- setup.py

setup.py

#_*_coding:utf-8_*_
from setuptools import setup

setup(
    name='myapp', # 应用名
    version='0.0.1', # 版本号
    packages=['myapp'], # 包括在安装包内的 Python 包
)

helloCarl.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-


def test():
    print "hello world Carl!"  

if __name__ == '__main__':
    test()

__init__.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from .helloCarl import test

如您所见,在项目中使用setuptools并不需要太多。在项目文件夹中运行该脚本,以及您开发的Python包。调用该脚本以生成分发,并自动包含setup.py所在目录中的所有包。请参阅下面的“ 命令参考”部分,了解可以为此安装脚本提供的命令。例如,要生成源代码分发,只需调用:

python setup.py sdist

python setup.py install 

生成目录:

├── dist
│   ├── firstApp-0.0.1-py2.7.egg
│   ├── firstApp-0.0.1.tar.gz
│   └── myapp-0.0.1-py2.7.egg
├── myapp
│   ├── helloCarl.py
│   └── __init__.py
├── myapp.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
└── setup.py

在dist中生成的是egg包,我们创建的egg安装到python的dist-packages目录下,我这里的位置在:

/usr/lib/python2.7/site-packages/myapp-0.0.1-py2.7.egg
打开Python环境,直接导入我们的包

Python 2.7.5 (default, Apr  9 2019, 14:30:50) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myapp
>>> myapp.test()
hello world Carl!
>>> 

当然,在将项目发布到PyPI之前,您需要向设置脚本添加更多信息,以帮助人们查找或了解您的项目。也许到时你的项目变大,包括一些依赖项,也许还有一些数据文件和脚本:

#_*_coding:utf-8_*_ 

from setuptools import setup, find_packages
setup(
    name="myapp",
    version="0.0.1",
    packages = find_packages('myapp'),
    # scripts=['say_hello.py'],

    # Project uses reStructuredText, so ensure that the docutils get
    # installed or upgraded on the target machine
    package_dir = {'':'myapp'},   # 告诉distutils包都在src下
    

    package_data={
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.rst'],
        # And include any *.dat files found in the 'myapp' package, too:
        'myapp': ['*.dat'],
    },

    # metadata to display on PyPI
    install_requires=['docutils>=0.3'],
    author="Me",
    author_email="me@example.com",
    description="This is an Example Package",
    keywords="hello world example examples",
    url="http://example.com/HelloWorld/",   # project home page, if any
    project_urls={
        "Bug Tracker": "https://bugs.example.com/HelloWorld/",
        "Documentation": "https://docs.example.com/HelloWorld/",
        "Source Code": "https://code.example.com/HelloWorld/",
    },
    classifiers=[
        'License :: OSI Approved :: Python Software Foundation License'
    ]

    # could also include long_description, download_url, etc.
)

包含了数据文件:

├── dist
│   └── myapp-0.0.1.tar.gz
├── myapp
│   ├── helloCarl.py
│   ├── __init__.py
│   ├── myapp.egg-info
│   │   ├── dependency_links.txt
│   │   ├── PKG-INFO
│   │   ├── requires.txt
│   │   ├── SOURCES.txt
│   │   └── top_level.txt
│   ├── mytest.dat
│   └── mytest.txt
├── myapp.txt
└── setup.py

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值