目錄
- 使用Setuptools構建和分發軟體包
- 開發人員指南
- 安裝setuptools
- 基本使用
- 指定專案的版本
- 新增和更改的setup()關鍵字
- 包括資料檔案
- 參考示例
- 開發人員指南
使用Setuptools構建和分發軟體包
參考官方檔案翻譯
開發人員指南
安裝setuptools
安裝最新版本的setuptools:
python3 -m pip install --upgrade setuptools
基本使用
引入setuptools基本使用
from setuptools import setup, find_packages
setup(
name="HelloWorld",
version="0.1",
packages=find_packages(),
)
要生成源代碼分發,只需呼叫:
setup.py sdist
隨著專案的增大將會包括一些依賴項,以及一些資料檔案和腳本:
from setuptools import setup, find_packages
setup(
name="HelloWorld",
version="0.1",
packages=find_packages(),
scripts=["say_hello.py"],
# Project uses reStructuredText, so ensure that the docutils get
# installed or upgraded on the target machine
install_requires=["docutils>=0.3"],
package_data=https://www.cnblogs.com/zhijiancanxue/p/{
# If any package contains *.txt or *.rst files, include them:"": ["*.txt", "*.rst"],
# And include any *.msg files found in the "hello" package, too:
"hello": ["*.msg"],
},
# metadata to display on PyPI
author="Me",
author_email="[email protected]",
description="This is an Example Package",
keywords="hello world example examples",
url="http://example.com/HelloWorld/", # project home page, if any
project_urls={
"Bug Tracker": "https://bugs.example.com/HelloWorld/",
"Documentation": "https://docs.example.com/HelloWorld/",
"Source Code": "https://code.example.com/HelloWorld/",
},
classifiers=[
"License :: OSI Approved :: Python Software Foundation License"
]
# could also include long_description, download_url, etc.
)
下面我們將解釋大多數這些setup() 引數的作用(元資料除外),以及在您自己的專案中使用它們的各種方式
指定專案的版本
官方的劃分比較多,簡潔的說一下:
版本:發行號+標簽(交替出現)
發行號:2.1.0 (簡單理解格式)
標簽:類似alpha,beta, a,c,dev等,例如:一個不穩定的預發行版本,建議:2.1.0.a9,如果需要加入日期建議使用-,例如:2.1.0.9-20190604
可以使用該pkg_resources.parse_version()功能比較不同的版本號:
>>> from pkg_resources import parse_version
>>> parse_version("1.9.a.dev") == parse_version("1.9a0dev")
True
>>> parse_version("2.1-rc2") < parse_version("2.1")
True
>>> parse_version("0.6a9dev-r41475") < parse_version("0.6a9")
True
新增和更改的setup()關鍵字
include_package_data:如果設定為True,setuptools則表示將在MANIFEST.in檔案指定的包目錄中自動包含找到的所有資料檔案,
exclude_package_data:將包名稱映射到應該從包目錄中排除的全域模式串列的字典
package_data:字典將包名稱映射到全域模式串列,
zip_safe:一個布林值(True或False)標志,指定是否可以安全地安裝該專案并從zip檔案運行該專案,如果未提供此引數,則bdist_egg每次構建雞蛋時,該命令將必須分析專案的所有內容以查找可能的問題,
install_requires:一個字串或字串串列,指定在安裝此版本時還需要安裝其他發行版,
entry_points:字典將入口點組名稱映射到定義入口點的字串或字串串列,
extras_require:字典將名稱“ extras”(專案的可選功能)映射到字串或字串串列的字典,用于指定必須安裝哪些其他發行版來支持這些功能,
python_requires:python版本設定,
版本號大于等于3.3,但是不能超過4
python_requires='~=3.3',
支持2.6 2.7以及所有以3.3開頭的Python 3版本
python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4',
setup_requires:安裝腳本執行時需要依賴的分發包,主要用于構建程序,注意,這里列出的包不會自動安裝,如果需要,同時要在install_requires中指定,
dependency_links:滿足依賴性時要搜索的URL的字串串列
namespace_packages:命名專案的“命名空間包”的字串串列
包括資料檔案
對包含的檔案進行更細粒度的控制(例如,如果您的軟體包目錄中有檔案檔案,并希望將其排除在安裝范圍之外),則還可以使用package_data關鍵字
setup.py
src/
mypkg/
__init__.py
mypkg.txt
data/
somefile.dat
otherdata.dat
from setuptools import setup, find_packages
setup(
...
packages=find_packages("src"), # include all packages under src
package_dir={"": "src"}, # tell distutils packages are under src
package_data=https://www.cnblogs.com/zhijiancanxue/p/{
# If any package contains *.txt files, include them:"": ["*.txt"],
# And include any *.dat files found in the "data" subdirectory
# of the "mypkg" package, also:
"mypkg": ["data/*.dat"],
}
)
如果想去掉readm.md等檔案:
from setuptools import setup, find_packages
setup(
...
packages=find_packages("src"), # include all packages under src
package_dir={"": "src"}, # tell distutils packages are under src
include_package_data=https://www.cnblogs.com/zhijiancanxue/p/True, # include everything in source control
# ...but exclude README.txt from all packages
exclude_package_data={"": ["README.txt"]},
)
參考示例
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from setuptools import setup, find_packages
"""
setuptools使用檔案: https://setuptools.readthedocs.io/en/latest/setuptools.html
生成原始碼發布包, 在靈犀工程目錄(LingXi/)下執行: python3 setup.py sdist
"""
setup(
name="LingXi",
version="1.5.0", # 發布前注意將此處改為本次發布的版本號
packages=find_packages(),
# Project uses reStructuredText, so ensure that the docutils get
# installed or upgraded on the target machine
# install_requires=['docutils>=0.3'],
package_data=https://www.cnblogs.com/zhijiancanxue/p/{
# If any package contains *.txt or *.rst files, include them:'': ['*.txt', '*.otf', '*.ttc', '*.sh', '*.css', '*.js', '*.ttf', '*.eot', '*.svg', '*.woff'],
'perception': ['templates/*',
'blocks/econet/*.yaml',
'compiled/Makefile',
'compiled/center_net/_cpools/src/*.cpp', 'compiled/center_net/_nms/*.c',
'compiled/center_net/_nms//pointer/resnet50/*',
'modules/meter_recognition_v2/cfgs/*',
'modules/meter_recognition_v2/cfgs/resnet50/*',
],
'services': ['*.sh',
'lingxi/static/js/*.js', 'lingxi/static/audio/*.wav',
'lingxi/static/docs/*.pdf', 'lingxi/static/imgs/*.jpg',
'lingxi/static/*.html', 'lingxi/templates/*.html',
'lingxi/static/*.css', 'lingxi/static/*.ico']
},
# metadata to display on PyPI
author="",
author_email="",
maintainer="",
maintainer_email="{dzp, gy, xkk}@kilox.cn",
description="分析系統",
keywords="",
url="",
license="GPLv3",
platforms="UNIX",
long_description="README.md, CHANGELOG.md, LICENSE",
install_requires=['opencv-python', 'numpy', 'Pillow', 'six', 'torch', 'pytz', 'waitress', 'wtforms', 'flask',
'torchvision', 'termcolor', 'scipy', 'matplotlib', 'imageio', 'requests', 'soundfile', 'librosa',
'face_recognition', 'scikit-image', 'imutils', 'tensorflow', 'mmcv', 'pycocotools', 'tabulate',
'psutil', 'py-cpuinfo', 'PyYAML', 'fvcore']
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/175710.html
標籤:Python
