一、構建工具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 from setuptools import setup, find_packages 2 3 setup( 4 name = "test", 5 version = "1.0", 6 keywords = ("test", "xxx"), 7 description = "eds sdk", 8 long_description = "eds sdk for python", 9 license = "MIT Licence", 10 11 url = "http://test.com", 12 author = "test", 13 author_email = "[email protected]", 14 15 packages = find_packages(), 16 include_package_data = True, 17 platforms = "any", 18 install_requires = [], 19 20 scripts = [], 21 entry_points = { 22 'console_scripts': [ 23 'test = test.help:main' 24 ] 25 } 26 )
setup.py各參數介紹:
--name 包名稱 其實我們可以將包統一放在一個src目錄中,另外,這個包內可能還有aaa.txt文件和data數據文件夾。另外,也可以排除一些特定的包 find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]) --install_requires = ["requests"] 需要安裝的依賴包 |
下列entry_points中: console_scripts 指明瞭命令行工具的名稱;在“redis_run = RedisRun.redis_run:main”中,等號前面指明瞭工具包的名稱,等號後面的內容指明瞭程序的入口地址。
1 entry_points={'console_scripts': [ 2 'redis_run = RedisRun.redis_run:main', 3 ]}
這裏可以有多條記錄,這樣一個項目就可以製作多個命令行工具了,比如:
1 setup( 2 entry_points = { 3 'console_scripts': [ 4 'foo = demo:test', 5 'bar = demo:test', 6 ]} 7 )
三、setup.py的項目示例代碼
1 #!/usr/bin/env python 2 # coding=utf-8 3 4 from setuptools import setup 5 6 ''' 7 把redis服務打包成C:\Python27\Scripts下的exe文件 8 ''' 9 10 setup( 11 name="RedisRun", #pypi中的名稱,pip或者easy_install安裝時使用的名稱,或生成egg文件的名稱 12 version="1.0", 13 author="Andreas Schroeder", 14 author_email="[email protected]", 15 description=("This is a service of redis subscripe"), 16 license="GPLv3", 17 keywords="redis subscripe", 18 url="https://ssl.xxx.org/redmine/projects/RedisRun", 19 packages=['RedisRun'], # 需要打包的目錄列表 20 21 # 需要安裝的依賴 22 install_requires=[ 23 'redis>=2.10.5', 24 'setuptools>=16.0', 25 ], 26 27 # 添加這個選項,在windows下Python目錄的scripts下生成exe文件 28 # 注意:模塊與函數之間是冒號: 29 entry_points={'console_scripts': [ 30 'redis_run = RedisRun.redis_run:main', 31 ]}, 32 33 # long_description=read('README.md'), 34 classifiers=[ # 程序的所屬分類列表 35 "Development Status :: 3 - Alpha", 36 "Topic :: Utilities", 37 "License :: OSI Approved :: GNU General Public License (GPL)", 38 ], 39 # 此項需要,否則卸載時報windows error 40 zip_safe=False 41 )
四、修改後的項目代碼(此時RedisRun模塊是DrQueue模塊的子模塊,這是因爲要導入某些公用的模塊)
1 #!/usr/bin/env python 2 # coding=utf-8 3 4 from setuptools import setup 5 6 ''' 7 把redis服務打包成C:\Python27\Scripts下的exe文件 8 ''' 9 10 setup( 11 name="RedisRun", #pypi中的名稱,pip或者easy_install安裝時使用的名稱 12 version="1.0", 13 author="Andreas Schroeder", 14 author_email="[email protected]", 15 description=("This is a service of redis subscripe"), 16 license="GPLv3", 17 keywords="redis subscripe", 18 url="https://ssl.xxx.org/redmine/projects/RedisRun", 19 packages=['DrQueue'], # 需要打包的目錄列表 20 21 # 需要安裝的依賴 22 install_requires=[ 23 'redis>=2.10.5', 24 ], 25 26 # 添加這個選項,在windows下Python目錄的scripts下生成exe文件 27 # 注意:模塊與函數之間是冒號: 28 entry_points={'console_scripts': [ 29 'redis_run = DrQueue.RedisRun.redis_run:main', 30 ]}, 31 32 # long_description=read('README.md'), 33 classifiers=[ # 程序的所屬分類列表 34 "Development Status :: 3 - Alpha", 35 "Topic :: Utilities", 36 "License :: OSI Approved :: GNU General Public License (GPL)", 37 ], 38 # 此項需要,否則卸載時報windows error 39 zip_safe=False 40 )
此時項目的目錄結構爲:
參考博客:
http://blog.csdn.net/lynn_kong/article/details/17540207
http://blog.csdn.net/pfm685757/article/details/48651389
http://blog.csdn.net/langb2014/article/details/53114341