python的構建工具setup.py的方法使用示例
本文介紹了python的構建工具setup.py,分享個大家,具體如下:
一、構建工具setup.py的應用場景
在安裝python的相關模塊和庫時,我們一般使用“pip install 模塊名”或者“python setup.py install”,前者是在線安裝,會安裝該包的相關依賴包;后者是下載源碼包然后在本地安裝,不會安裝該包的相關依賴包。所以在安裝普通的python包時,利用pip工具相當簡單。但是在如下場景下,使用python setup.py install會更適合需求:
在編寫相關系統(tǒng)時,python 如何實現(xiàn)連同依賴包一起打包發(fā)布?
假如我在本機開發(fā)一個程序,需要用到python的redis、mysql模塊以及自己編寫的redis_run.py模塊。我怎么實現(xiàn)在服務器上去發(fā)布該系統(tǒng),如何實現(xiàn)依賴模塊和自己編寫的模塊redis_run.py一起打包,實現(xiàn)一鍵安裝呢?同時將自己編寫的redis_run.py模塊以exe文件格式安裝到python的全局執(zhí)行路徑C:\Python27\Scripts下呢?
在這種應用場景下,pip工具似乎派不上了用場,只能使用python的構建工具setup.py了,使用此構建工具可以實現(xiàn)上述應用場景需求,只需在 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 包名稱
--version (-V) 包版本
--author 程序的作者
--author_email 程序的作者的郵箱地址
--maintainer 維護者
--maintainer_email 維護者的郵箱地址
--url 程序的官網(wǎng)地址
--license 程序的授權信息
--description 程序的簡單描述
--long_description 程序的詳細描述
--platforms 程序適用的軟件平臺列表
--classifiers 程序的所屬分類列表
--keywords 程序的關鍵字列表
--packages 需要處理的包目錄(包含__init__.py的文件夾)
--py_modules 需要打包的python文件列表
--download_url 程序的下載地址
--cmdclass
--data_files 打包時需要打包的數(shù)據(jù)文件,如圖片,配置文件等
--scripts 安裝時需要執(zhí)行的腳步列表
--package_dir 告訴setuptools哪些目錄下的文件被映射到哪個源碼包。一個例子:package_dir = {'': 'lib'},表示“root package”中的模塊都在lib 目錄中。
--requires 定義依賴哪些模塊
--provides定義可以為哪些模塊提供依賴
--find_packages() 對于簡單工程來說,手動增加packages參數(shù)很容易,剛剛我們用到了這個函數(shù),它默認在和setup.py同一目錄下搜索各個含有 __init__.py的包。
其實我們可以將包統(tǒng)一放在一個src目錄中,另外,這個包內可能還有aaa.txt文件和data數(shù)據(jù)文件夾。另外,也可以排除一些特定的包
find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])
--install_requires = ["requests"] 需要安裝的依賴包
--entry_points 動態(tài)發(fā)現(xiàn)服務和插件,下面詳細講
下列entry_points中: console_scripts 指明了命令行工具的名稱;在“redis_run = RedisRun.redis_run:main”中,等號前面指明了工具包的名稱,等號后面的內容指明了程序的入口地址。
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-8
from setuptools import setup
'''
把redis服務打包成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 error
zip_safe=False
)
四、修改后的項目代碼(此時RedisRun模塊是DrQueue模塊的子模塊,這是因為要導入某些公用的模塊)
#!/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",
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 error
zip_safe=False
)
此時項目的目錄結構為:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)簡單的ui界面的設計步驟(適合小白)
當我們書寫一個python程序時,我們在控制臺輸入信息時,往往多有不便,并且為了更加美觀且直觀的方式輸入控制命令,我們常常設計一個ui界面,這樣就能方便執(zhí)行相關功能,如計算器、日歷等界面,本博客是為了給ui設計的小白進行講解,需要的朋友可以參考下2024-07-07
python GUI庫圖形界面開發(fā)之PyQt5滾動條控件QScrollBar詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5滾動條控件QScrollBar詳細使用方法與實例,需要的朋友可以參考下2020-03-03
python?Pandas之DataFrame索引及選取數(shù)據(jù)
這篇文章主要介紹了python?Pandas之DataFrame索引及選取數(shù)據(jù),文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07

