Python零基礎入門--基礎(十)-- Setuptools

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="[email protected]",
    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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章