Python 的构建工具 setup.py
一、構(gòu)建工具setup.py的應(yīng)用場景
? ? ? 在安裝python的相關(guān)模塊和庫時,我們一般使用“pip install ?模塊名”或者“python setup.py install”,前者是在線安裝,會安裝該包的相關(guān)依賴包;后者是下載源碼包然后在本地安裝,不會安裝該包的相關(guān)依賴包。所以在安裝普通的python包時,利用pip工具相當簡單。但是在如下場景下,使用python setup.py install會更適合需求:
| 在編寫相關(guān)系統(tǒng)時,python 如何實現(xiàn)連同依賴包一起打包發(fā)布? ? ? ? 假如我在本機開發(fā)一個程序,需要用到python的redis、mysql模塊以及自己編寫的redis_run.py模塊。我怎么實現(xiàn)在服務(wù)器上去發(fā)布該系統(tǒng),如何實現(xiàn)依賴模塊和自己編寫的模塊redis_run.py一起打包,實現(xiàn)一鍵安裝呢?同時將自己編寫的redis_run.py模塊以exe文件格式安裝到python的全局執(zhí)行路徑C:\Python27\Scripts下呢? |
? ? ? ?在這種應(yīng)用場景下,pip工具似乎派不上了用場,只能使用python的構(gòu)建工具setup.py了,使用此構(gòu)建工具可以實現(xiàn)上述應(yīng)用場景需求,只需在 setup.py 文件中寫明依賴的庫和版本,然后到目標機器上使用python setup.py install安裝。
二、setup.py介紹
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", author_email = "test@gmail.com", packages = find_packages(), include_package_data = True, platforms = "any", install_requires = [], scripts = [], entry_points = { 'console_scripts': [ 'test = test.help:main' ] } )?setup.py各參數(shù)介紹:
| --name 包名稱 ? ? ? ? ? ? ? ? ? ? ? ? ? 其實我們可以將包統(tǒng)一放在一個src目錄中,另外,這個包內(nèi)可能還有aaa.txt文件和data數(shù)據(jù)文件夾。另外,也可以排除一些特定的包 ? ? ? ? ? ? ? ? ? ? ? ? ? find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]) --install_requires = ["requests"] 需要安裝的依賴包 |
? ? ?下列entry_points中:?console_scripts 指明了命令行工具的名稱;在“redis_run = RedisRun.redis_run:main”中,等號前面指明了工具包的名稱,等號后面的內(nèi)容指明了程序的入口地址。
entry_points={'console_scripts': ['redis_run = RedisRun.redis_run:main', ]}?這里可以有多條記錄,這樣一個項目就可以制作多個命令行工具了,比如:
setup(entry_points = {'console_scripts': ['foo = demo:test','bar = demo:test',]})三、setup.py的項目示例代碼
#!/usr/bin/env python# coding=utf-8from setuptools import setup'''把redis服務(wù)打包成C:\Python27\Scripts下的exe文件'''setup(name="RedisRun", #pypi中的名稱,pip或者easy_install安裝時使用的名稱,或生成egg文件的名稱version="1.0",author="Andreas Schroeder",author_email="andreas@drqueue.org",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文件# 注意:模塊與函數(shù)之間是冒號: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 errorzip_safe=False)四、修改后的項目代碼
此時RedisRun模塊是DrQueue模塊的子模塊,這是因為要導入某些公用的模塊
#!/usr/bin/env python# coding=utf-8from setuptools import setup'''把redis服務(wù)打包成C:\Python27\Scripts下的exe文件'''setup(name="RedisRun", #pypi中的名稱,pip或者easy_install安裝時使用的名稱version="1.0",author="Andreas Schroeder",author_email="andreas@drqueue.org",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文件# 注意:模塊與函數(shù)之間是冒號: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 errorzip_safe=False)此時項目的目錄結(jié)構(gòu)為:
?
總結(jié)
以上是生活随笔為你收集整理的Python 的构建工具 setup.py的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 14.04 16.04 安
- 下一篇: python 学习笔记 - for循环: