日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 的构建工具 setup.py

發(fā)布時間:2025/3/15 python 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 的构建工具 setup.py 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、構(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 包名稱
--version (-V) 包版本
--author 程序的作者
--author_email 程序的作者的郵箱地址
--maintainer 維護者
--maintainer_email 維護者的郵箱地址
--url 程序的官網(wǎng)地址
--license 程序的授權(quán)信息
--description 程序的簡單描述
--long_description 程序的詳細描述
--platforms 程序適用的軟件平臺列表
--classifiers 程序的所屬分類列表
--keywords 程序的關(guān)鍵字列表
--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目錄中,另外,這個包內(nèi)可能還有aaa.txt文件和data數(shù)據(jù)文件夾。另外,也可以排除一些特定的包

? ? ? ? ? ? ? ? ? ? ? ? ? find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])

--install_requires = ["requests"] 需要安裝的依賴包
--entry_points 動態(tài)發(fā)現(xiàn)服務(wù)和插件,下面詳細講

? ? ?下列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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。