本文介紹了python的構建工具setup.py,分享個大家,具體如下:
一、構建工具setup.py的應用場景
在安裝python的相關模塊和庫時,我們一般使用“pip install 模塊名”或者“python setup.py install”,前者是在線安裝,會安裝該包的相關依賴包;后者是下載源碼包然后在本地安裝,不會安裝該包的相關依賴包。所以在安裝普通的python包時,利用pip工具相當簡單。但是在如下場景下,使用python setup.py install會更適合需求:
在編寫相關系統時,python 如何實現連同依賴包一起打包發布?
假如我在本機開發一個程序,需要用到python的redis、mysql模塊以及自己編寫的redis_run.py模塊。我怎么實現在服務器上去發布該系統,如何實現依賴模塊和自己編寫的模塊redis_run.py一起打包,實現一鍵安裝呢?同時將自己編寫的redis_run.py模塊以exe文件格式安裝到python的全局執行路徑C:\Python27\Scripts下呢?
在這種應用場景下,pip工具似乎派不上了用場,只能使用python的構建工具setup.py了,使用此構建工具可以實現上述應用場景需求,只需在 setup.py 文件中寫明依賴的庫和版本,然后到目標機器上使用python setup.py install安裝。
二、setup.py介紹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
from setuptools import setup, find_packages setup( name = "test" , version = "1.0" , keywords = ( "test" , "xxx" ), description = "eds sdk" , long_description = "eds sdk for python" , license = "MIT Licence" , url = "http://test.com" , author = "test" , packages = find_packages(), include_package_data = True , platforms = "any" , install_requires = [], scripts = [], entry_points = { 'console_scripts' : [ 'test = test.help:main' ] } ) |
setup.py各參數介紹:
--name 包名稱
--version (-V) 包版本
--author 程序的作者
--author_email 程序的作者的郵箱地址
--maintainer 維護者
--maintainer_email 維護者的郵箱地址
--url 程序的官網地址
--license 程序的授權信息
--description 程序的簡單描述
--long_description 程序的詳細描述
--platforms 程序適用的軟件平臺列表
--classifiers 程序的所屬分類列表
--keywords 程序的關鍵字列表
--packages 需要處理的包目錄(包含__init__.py的文件夾)
--py_modules 需要打包的python文件列表
--download_url 程序的下載地址
--cmdclass
--data_files 打包時需要打包的數據文件,如圖片,配置文件等
--scripts 安裝時需要執行的腳步列表
--package_dir 告訴setuptools哪些目錄下的文件被映射到哪個源碼包。一個例子:package_dir = {'': 'lib'},表示“root package”中的模塊都在lib 目錄中。
--requires 定義依賴哪些模塊
--provides定義可以為哪些模塊提供依賴
--find_packages() 對于簡單工程來說,手動增加packages參數很容易,剛剛我們用到了這個函數,它默認在和setup.py同一目錄下搜索各個含有 __init__.py的包。
其實我們可以將包統一放在一個src目錄中,另外,這個包內可能還有aaa.txt文件和data數據文件夾。另外,也可以排除一些特定的包
1
|
find_packages(exclude = [ "*.tests" , "*.tests.*" , "tests.*" , "tests" ]) |
--install_requires = ["requests"] 需要安裝的依賴包
--entry_points 動態發現服務和插件,下面詳細講
下列entry_points中: console_scripts 指明了命令行工具的名稱;在“redis_run = RedisRun.redis_run:main”中,等號前面指明了工具包的名稱,等號后面的內容指明了程序的入口地址。
1
2
3
|
entry_points = { 'console_scripts' : [ 'redis_run = RedisRun.redis_run:main' , ]} |
這里可以有多條記錄,這樣一個項目就可以制作多個命令行工具了,比如:
1
2
3
4
5
6
7
|
setup( entry_points = { 'console_scripts' : [ 'foo = demo:test' , 'bar = demo:test' , ]} ) |
三、setup.py的項目示例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#!/usr/bin/env python # coding=utf-8 from setuptools import setup ''' 把redis服務打包成C:\Python27\Scripts下的exe文件 ''' setup( name = "RedisRun" , #pypi中的名稱,pip或者easy_install安裝時使用的名稱,或生成egg文件的名稱 version = "1.0" , author = "Andreas Schroeder" , description = ( "This is a service of redis subscripe" ), license = "GPLv3" , keywords = "redis subscripe" , url = "https://ssl.xxx.org/redmine/projects/RedisRun" , packages = [ 'RedisRun' ], # 需要打包的目錄列表 # 需要安裝的依賴 install_requires = [ 'redis>=2.10.5' , 'setuptools>=16.0' , ], # 添加這個選項,在windows下Python目錄的scripts下生成exe文件 # 注意:模塊與函數之間是冒號: entry_points = { 'console_scripts' : [ 'redis_run = RedisRun.redis_run:main' , ]}, # long_description=read('README.md'), classifiers = [ # 程序的所屬分類列表 "Development Status :: 3 - Alpha" , "Topic :: Utilities" , "License :: OSI Approved :: GNU General Public License (GPL)" , ], # 此項需要,否則卸載時報windows error zip_safe = False ) |
四、修改后的項目代碼(此時RedisRun模塊是DrQueue模塊的子模塊,這是因為要導入某些公用的模塊)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/usr/bin/env python # coding=utf-8 from setuptools import setup ''' 把redis服務打包成C:\Python27\Scripts下的exe文件 ''' setup( name = "RedisRun" , #pypi中的名稱,pip或者easy_install安裝時使用的名稱 version = "1.0" , author = "Andreas Schroeder" , description = ( "This is a service of redis subscripe" ), license = "GPLv3" , keywords = "redis subscripe" , url = "https://ssl.xxx.org/redmine/projects/RedisRun" , packages = [ 'DrQueue' ], # 需要打包的目錄列表 # 需要安裝的依賴 install_requires = [ 'redis>=2.10.5' , ], # 添加這個選項,在windows下Python目錄的scripts下生成exe文件 # 注意:模塊與函數之間是冒號: entry_points = { 'console_scripts' : [ 'redis_run = DrQueue.RedisRun.redis_run:main' , ]}, # long_description=read('README.md'), classifiers = [ # 程序的所屬分類列表 "Development Status :: 3 - Alpha" , "Topic :: Utilities" , "License :: OSI Approved :: GNU General Public License (GPL)" , ], # 此項需要,否則卸載時報windows error zip_safe = False ) |
此時項目的目錄結構為:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/maociping/p/6633948.html