1. 程式人生 > >Python打包、安裝與釋出工具--setuptools

Python打包、安裝與釋出工具--setuptools

Python中的setuptools工具不僅僅是簡化了distutils 的setup.py檔案的編寫,更重要的是它的包管理能力方面的增強。它可以使用一種更加透明的方法來查詢、下載並安裝依賴包;並可以在一個包的多個版本中自由進行切換,這些版本都安裝在同一個系統上;也可以宣告對某個包的特定版本的需求;還可以只使用一個簡單的命令就能更新到某個包的最新版本。說白了,這個和java中的Maven,以及在centos中使用的Yum有相似的地方;下面,先對以下幾個名詞作一個解釋:

1.distutils

distutils
(1)Part of the Python standard library since version 1.6(自1.6版本,加入python標準庫)
(2)The standard way of building and installing packages(構建、安裝包的標準方法)
(3)Primary functionality in distutils.core
(4) Developer or packager creates setup.py


#!/usr/bin/env python
from distutils.core import setup
setup (name = “foo”,
version = “1.0”,
py_modules = [“foo”])
$ python setup.py sdist (create a source distribution)
$ python setup.py bdist (create a build distribution)
$ python setup.py install (install using defaults)
* Other --install-* options (remember to update $PYTHONPATH)

setup中的引數說明:

setup( arguments)
The basic do-everything function that does most everything you could ever ask for from a Distutils method. See XXXXX

The setup function takes a large number of arguments. These are laid out in the following table.

argument namevaluetype
name The name of the package a string
version The version number of the package
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 a string
maintainer_email The email address of the current maintainer, if different from the author
url A URL for the package (homepage) a URL
download_url A URL to download the package a URL
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
distclass the Distribution class to use A subclass of distutils.core.Distribution
script_name The name of the setup.py script - defaults to sys.argv[0] a string
script_args Arguments to supply to the setup script a list of strings
options default options for the setup script a string
license The license for the package
keywords Descriptive meta-data. See PEP 314
platforms
cmdclass A mapping of command names to Command subclasses a dictionary

2.setuptools
1) A collection of enhancements to the Python distutils package that allow one to more easily build and
distribute python packages(對distutils包的增強,使用setuptools可以更加方便的構建、釋出python packages)
   2)Additional set of keyword arguments to setup()(在distutils.core.setup的基礎上,添加了一些新的引數)
3)Includes easy_install.py (包括一個easy_install.py工具,提供類似centos中的yum安裝工具)
4) Creates eggs (.egg)(關於egg,最好的解釋還是:‘Eggs are to Pythons as Jars are to Java…‘)
5)Features for developers (e.g. support for data files,MANIFEST, Pyrex, PyPI upload,…)
6)Ability to deploy project in “development mode” via setup.py develop command.

3. egg檔案

Eggs

 “Eggs are to Pythons as Jars are to Java…” ------------Phillip J. Eby
(1)Single-file importable distribution format .(這句話的意思是說,python中匯入掛鉤的更改,只要把egg加入到PYTHONPATH或者sys.path中,就可以像往常一樣匯入)
(2)Eggs are Zipfiles using the .egg extension, that support including data and C extensions as well as Python code

(egg也不一定是zipfile,setup的引數中有一個zip_safe引數,在程式打包時決定是否壓縮,如果不指定,bdist_egg會對工程中的每一個檔案作檢查,具體的解釋如下:)

zip_safe A boolean (True or False) flag specifying whether the project can be safely installed and run from a zip file. If this argument is not supplied, the bdist_egg command will have to analyze all of your project's contents for possible problems each time it buids an egg.

(關於egg中是否會打包.py以外的資料、檔案,在setup中有3個引數控制,具體如下:)

include_package_data If set to True, this tells setuptools to automatically include any data files it finds inside your package directories, that are either under CVS or Subversion control, or which are specified by your MANIFEST.in file. For more information, see the section below on Including Data Files. exclude_package_data A dictionary mapping package names to lists of glob patterns that should be excluded from your package directories. You can use this to trim back any excess files included by include_package_data. For a complete description and examples, see the section below on Including Data Files. package_data A dictionary mapping package names to lists of glob patterns. For a complete description and examples, see the section below on Including Data Files. You do not need to use this option if you are using include_package_data, unless you need to add e.g. files that are generated by your setup script and build process. (And are therefore not in source control or are files that you don't want to include in your source distribution.)

這裡有一個例子:

The distutils have traditionally allowed installation of "data files", which are placed in a platform-specific location. However, the most common use case for data files distributed with a package is for use by the package, usually by including the data files in the package directory.

Setuptools offers three ways to specify data files to be included in your packages. First, you can simply use the include_package_data keyword, e.g.:

from setuptools import setup, find_packages
setup(
    ...
    include_package_data = True
)

This tells setuptools to install any data files it finds in your packages. The data files must be under CVS or Subversion control, or else they must be specified via the distutils' MANIFEST.in file. (They can also be tracked by another revision control system, using an appropriate plugin. See the section below on Adding Support for Other Revision Control Systems for information on how to write such plugins.)

If you want finer-grained control over what files are included (for example, if you have documentation files in your package directories and want to exclude them from installation), then you can also use the package_data keyword, e.g.:

from setuptools import setup, find_packages
setup(
    ...
    package_data = {
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.rst'],
        # And include any *.msg files found in the 'hello' package, too:
        'hello': ['*.msg'],
    }
)

The package_data argument is a dictionary that maps from package names to lists of glob patterns. The globs may include subdirectory names, if the data files are contained in a subdirectory of the package. For example, if the package tree looks like this:

setup.py
src/
    mypkg/
        __init__.py
        mypkg.txt
        data/
            somefile.dat
            otherdata.dat

The setuptools setup file might look like this:

from setuptools import setup, find_packages
setup(
    ...
    packages = find_packages('src'),  # include all packages under src
    package_dir = {'':'src'},   # tell distutils packages are under src

    package_data = {
        # If any package contains *.txt files, include them:
        '': ['*.txt'],
        # And include any *.dat files found in the 'data' subdirectory
        # of the 'mypkg' package, also:
        'mypkg': ['data/*.dat'],
    }
)

Notice that if you list patterns in package_data under the empty string, these patterns are used to find files in every package, even ones that also have their own patterns listed. Thus, in the above example, the mypkg.txt file gets included even though it's not listed in the patterns for mypkg.

Also notice that if you use paths, you must use a forward slash (/) as the path separator, even if you are on Windows. Setuptools automatically converts slashes to appropriate platform-specific separators at build time.

(Note: although the package_data argument was previously only available in setuptools, it was also added to the Python distutils package as of Python 2.4; there is some documentation for the feature available on the python.org website.)

Sometimes, the include_package_data or package_data options alone aren't sufficient to precisely define what files you want included. For example, you may want to include package README files in your revision control system and source distributions, but exclude them from being installed. So, setuptools offers an exclude_package_data option as well, that allows you to do things like this:

from setuptools import setup, find_packages
setup(
    ...
    packages = find_packages('src'),  # include all packages under src
    package_dir = {'':'src'},   # tell distutils packages are under src

    include_package_data = True,    # include everything in source control

    # ...but exclude README.txt from all packages
    exclude_package_data = { '': ['README.txt'] },
)

The exclude_package_data option is a dictionary mapping package names to lists of wildcard patterns, just like the package_data option. And, just as with that option, a key of '' will apply the given pattern(s) to all packages. However, any files that match these patterns will be excluded from installation, even if they were listed in package_data or were included as a result of using include_package_data.

In summary, the three options allow you to:

include_package_data
Accept all data files and directories matched by MANIFEST.in or found in source control.
package_data
Specify additional patterns to match files and directories that may or may not be matched by MANIFEST.in or found in source control.
exclude_package_data
Specify patterns for data files and directories that should not be included when a package is installed, even if they would otherwise have been included due to the use of the preceding options.

NOTE: Due to the way the distutils build process works, a data file that you include in your project and then stop including may be "orphaned" in your project's build directories, requiring you to run setup.py clean --all to fully remove them. This may also be important for your users and contributors if they track intermediate revisions of your project using Subversion; be sure to let them know when you make changes that remove files from inclusion so they can run setup.py clean --all.


(3)Requires Python 2.3 or above

(4)Eggs are built using the setuptools package;eg:

python2.4 setup.py bdist_egg
(5)The published plan is to propose inclusion in the Python 2.5 standard library.(這個計劃沒有實現)
這個是我在http://www.ibm.com/developerworks/cn/linux/l-cppeak3.html上節選過來的,說的很好:

egg 是一個包含所有包資料的檔案包。在理想情況中,egg 是一個使用 zip 壓縮的檔案,其中包括了所有需要的包檔案。但是在某些情況下,setuptools 會決定(或被開關告知)包不應該是 zip 壓縮的。在這些情況下,egg 只是一個簡單的未曾壓縮的子目錄,但是裡面的內容是相同的。使用單一的版本可以方便地進行轉換,並可以節省一點磁碟空間,但是 egg 目錄從功能和組織結構上來說都是相同的。一直使用 JAR 檔案的 Java™ 技術的使用者會發現 egg 非常熟悉。

由於最新的 Python 版本中(需要 2.3.5+ 或 2.4)匯入掛鉤的更改,可以簡單地通過設定 PYTHONPATHsys.path 並像往常一樣匯入相應的包來使用 egg。如果希望採用這種方法,就不需要使用 setuptoolsez_setup.py 了。例如,在本文使用的工作目錄中,我就為 PyYAML 包放入了一個 egg。現在我就可以使用這個包了,方法如下:

% export PYTHONPATH=~/work/dW/PyYAML-3.01-py2.4.egg
% python -c 'import yaml; print yaml.dump({"foo":"bar",1:[2,3]})'
1: [2, 3]
foo: bar

(在這個地方,我想說一下,你必須使用過setuptools才會有eazy-install.pth)

不過,PYTHONPATH 的(或者指令碼或 Python shell 會話內的 sys.path的)這種操作有些脆弱。egg 的發現最好是在新一點的 .pth 檔案中進行。在 site-packages/ 或 PYTHONPATH 中的任何 .pth 檔案都會進行解析來執行其他匯入操作,其方法類似於檢查可能包含包的那些目錄位置一樣。如果使用 setuptools 來處理包的管理功能,那麼在安裝、更新、刪除包時,就需要修改一個名為 easy-install.pth 的檔案。而且可以按照自己喜歡的方式對這個 .pth 進行命名(只要其副檔名是 .pth 即可)。例如,下面是我的 easy-install.pth 檔案的內容:

% cat /sw/lib/python2.4/site-packages/easy-install.pth
import sys; sys.__plen = len(sys.path)
setuptools-0.6b1-py2.4.egg
SQLObject-0.7.0-py2.4.egg
FormEncode-0.5.1-py2.4.egg
Gnosis_Utils-1.2.1-py2.4.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:];
  p=getattr(sys,'__egginsert',0); sys.path[p:p]=new;
  sys.__egginsert = p+len(new)

這種格式有點特殊:它近似於一個 Python 指令碼,但卻不完全是。需要說明的是,可以在那裡新增額外列出的 egg;更好的情況是,easy_install 會在執行時實現這種功能。也可以在 site-packages/ 下建立任意多個 .pth 檔案;每個都可以列出有哪些 egg 是可用的。

Why bother with Eggs?
    1)Enable tools like “Easy Install” Python package manager(這個很顯然)
 2)They are a “zero installation” format for pure Python packages (put them on PYTHONPATH or sys.path)(上面說的那個.pth)
 3)They can include package metadata (e.g. dependencies)
 4)They allow namespace packages (packages that contain other packages) to be split into separate distributions
 5)They allow applications or libraries to specify the needed version of a library before doing an import (e.g. require(“Twisted-Internet>=2.0”) )(這個涉及到的是setup裡install_requires引數)
 6)They provide a framework for plug-ins (similar to Eclipse’s extension point)
 7)Enables one to distribute a project that depends on other software available via PyPI a.k.a. Cheese Shop.(eazy_install或者執行python setup.py install時,會依據install_requires引數自動從http://www.python.org/pypi/上尋找規定的版本,並下載安裝好這些依賴的包)

4. eazy_install

Easy Install / easy_install.py
1)A Python module bundled with setuptools that lets one automatically build, install, and manage python
packages
2) Part of the setuptools package
3) Installs any distutils-based package
4) Can find packages on PyPI
5) Handles dependencies via arguments to setup() (e.g. install_requires = [‘foo>=1.4’,’Bar’] )

[1]http://docs.python.org/distutils/index.html

[2]http://peak.telecommunity.com/DevCenter/setuptools

[3]http://peak.telecommunity.com/DevCenter/PythonEggs

[4]http://peak.telecommunity.com/DevCenter/EasyInstall

[5]http://www.ibm.com/developerworks/cn/linux/l-cppeak3.html

相關推薦

Python打包安裝釋出工具--setuptools

Python中的setuptools工具不僅僅是簡化了distutils 的setup.py檔案的編寫,更重要的是它的包管理能力方面的增強。它可以使用一種更加透明的方法來查詢、下載並安裝依賴包;並可以在一個包的多個版本中自由進行切換,這些版本都安裝在同一個系統上;也可以宣告對

python系列(一)python簡介安裝基本應用

注:本文為轉載,如有侵權請告知,將予以刪除 原連結:http://blog.51cto.com/zpf666/1920313     一、python簡介 1、python介於C語言與shell之間,於1989年由guido van Rossum(龜叔)開發,

python Flask離線安裝測試

python web開發 flask Flask是用python進行web開發時,常見的python web框架。如果服務器可以連接到外網,可以簡單的用 pip install Flask 直接將Flask的依賴包及Flask直接安裝好。但公司的測試環境,一般是內網,不能隨表上連到外網,這就需要采

PYTHON REQUESTS的安裝簡單運用

Coding import out compile 鏈接 客戶端 detail 但我 編輯 PYTHON REQUESTS的安裝與簡單運用 2013.07.09 強烈推薦!requests官方文檔已有了中文版,請見http://cn.python-requests.o

3dsmax2014的下載安裝註冊激活教程詳解

com cor 殺毒 系統 3dmax http 後退 tar 輸入 3dsmax2014的下載、安裝與註冊激活教程,雖然網上類似的教程文章不勝枚舉,但大多比較粗枝大葉,沒有詳細的步驟,尤其對於電腦小白來說,更是不易參考,今天我就教大家如何註冊破解3dsmax2014吧!

Rational Rose_2007的下載安裝破解--UML建模軟件

加載 ext img 百度 jpg 相關 -s license -- 一、下載Rational.Rose_2007安裝包與破解文件 對於Rational.Rose_2007,您可以到我的百度網盤計算機相關專業所用軟件---百度雲鏈接下載下載,另外附上安裝需要的通行證(破解

MySQL 5.6 (Win7 64位)下載安裝配置圖文教程

服務 建立 basedir 字長 目錄 之前 配置環境 改名 一次 一. 工具 Win7 64位操作系統 二. 步驟 第一步:下載安裝包 下載 地址:http://www.mysql.com/ 截止到目前(2016/7/24) ,官網的最新版本是5.7.13

Navicat for MySQL下載安裝破解

for 完成後 失效 exe 進入 .html 關閉 百度 logs 參考來源:https://www.cnblogs.com/da19951208/p/6403607.html 一:下載Navicat for MySQL   進入 Navicat for MySQL

正則grepegrep工具

shadow oss 示例 jpg 命令 log proc fff 分享圖片 正則 grep命令 grep與egrep示例 正則、grep與egrep工具

[Web][DreamweaverCS6][高中同學畢業分布去向網站+服務器上掛載]一安裝破解DreamweaverCS6+基本規劃

ima data 計時 感覺 get .net 查詢 並且 背景   DreamweaverCS6安裝與破解 一、背景介紹:同學畢業分布圖項目計劃簡介   哎哎哎,炸麽說呢,對於Web前端設計來說,純手撕html部分代碼實在是難受。   對於想做地圖這類的就&ldqu

(一)5安裝配置MySQL

ubun register oot local sock www sans res state 最後當然還有我們的MySQL啦(遇選擇請選“Y”) root@ubuntu:/# sudo apt-get install mysql-server 漫長的等待之後,我的界面

phpstorm 的下載安裝激活

name see 使用 模式 insert clas 鼠標 窗口 下載地址 1.phpstorm的下載地址   https://www.jetbrains.com/phpstorm/   下載後的安裝包如圖:    2.phpstorm的安裝過程   跟據電腦系統下載安裝對

python 虛擬環境安裝命令簡介

python1. 更新Pip版本 #安裝 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 setup.py install pip install -U pip 2. 使用Pip安裝virtualenv虛擬環境工具,創建

Python簡介安裝更新基本語法及資料型別

Python簡介 python的創始人為吉多·範羅蘇姆(Guido van Rossum)。1989年的聖誕節期間,吉多·範羅蘇姆為了在阿姆斯特丹打發時間,決心開發一個新的指令碼解釋程式,作為ABC語言的一種繼承。   最新的TIOBE排行榜,Python趕超PHP佔據第

大資料(十七):Hive簡介安裝基本操作

一、簡介         Hive由Facebook開源用於解決海量結構化日誌的資料統計。Hive是基於Hadoop的一個數據倉庫工具,可以將結構化的資料檔案對映為一張表,並提供類Sql查詢的功能。 hive本質是將HQL轉化為MapRedu

MongoDB的下載安裝部署

1、什麼是MongoDB?        它是介於關係型資料庫和非關係型資料庫之間的一種NoSQL資料庫,用C++編寫,是一款集敏捷性、可伸縮性、擴充套件性於一身的高效能的面向文件的通用資料庫。 2、為什麼要用MongoDB?   它具有以下幾個特

Python學習】安裝配置方法

Python安裝 作為熱門榜上上竄最快,也是當今一款非常流行的語言python!讓我們一起來學習吧 1.下載 官網下載地址:https://www.python.org/downloads/ 這裡我們選擇下載到E盤,下載完成後雙擊。 2.安裝 (

Office 2019 正式版下載安裝啟用

準備安裝檔案 準備 Office Tool Plus Office Tool Plus(以下簡稱“OTP”)由 Yerong 開發,是一個小巧、安全、簡潔的 Office 管理、下載、安裝器,支援在 Windows 7、8、10 上快速部署 Office。 此文中使用的

Android Studio 下載安裝配置(開發環境搭建)

下載jdk8並安裝. 下載並安裝AndroidStudio。因為anroid.com無法訪問,可以從https://developer.android.google.cn/studio/獲取。 安裝AndroidStudio後,會自動下載Android SDK及其他

Python模組的安裝解除安裝

安裝:        無pip環境變數時:           安裝whl包:python -mpip install wheel    ->    python -m pip install  **.whl           安裝tar.gz包:cd到解壓後路