動機
pip installの-c/--constraintオプションが
tl;dr
前提とpip install -c constraints.txt, setup.py / setup.cfgの
具体例
初回セットアップ時
setup.pyを
from setuptools import setup
install_requires = [ # ここはrequirements.txtに書き出すのもアリ
"fastapi",
"sqlalchemy ~= 1.4.32",
# その他必要なパッケージを列挙
]
tests_require = [
"pytest",
"freezegun",
# 他、テストに必要なパッケージ
]
setup(
# nameなど他省略
install_requires=install_requires
extras_require={
"test": tests_require,
"deploy": "aws-sam-cli",
"research": "jupyterlab"
}
)これをpip install -e ."[test,deploy,research]"と-r requirements.txtオプションも
pip freeze --exclude-editable > constraints.txtと
aws-sam-cli==1.40.1
fastapi==0.75.0
freezegun==1.2.0
ipykernel==6.9.2
ipython==8.1.1
jupyterlab==3.3.2
pandas-1.4.1
pytest==7.1.0
SQLAlchemy==1.4.32Cloneしてきて 使う 時
pip install -e ."[test,deploy,research]" -c constraints.txtで-e: editableは
依存パッケージ更新時
venvをpip install -e ."[test,deploy,research]"
pip freeze --exclude-editable > constraints.txtで
pip install -c constraints.txtが 何を しているか
pip install --helpを
-c, --constraint <file> Constrain versions using the given constraints
なる
constraints は、
pip install の -c オプションを 使います。 -r オプションと 違い、 これで 指定しただけでは ライブラリは インストール されません。 ただ バージョンを 制限するだけです。
つまりpip install -e ."[test,deploy]" -c constraints.txtと-rオプションとは
まとめ
今日Poetryに
追記 on 2022/7/4
PEP 621 がsetup.py/setup.cfgを
PEP 621を 踏まえたpyproject.tomlの 例
まず
from setuptools import setup
setup()そして、
[build-system]
requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"]
build-backend = "setuptools.build_meta"
[tool.setuptools_scm]
[project]
name = "package_name"
authors = [{name="Yoichi Takai", email="[email protected]"}]
dynamic = ["version"]
requires-python = ">=3.10" # 指定したい場合書く
dependencies = [ # ここはrequirements.txtに書き出すのもアリ
"fastapi",
"sqlalchemy ~= 1.4.32",
]
urls = {homepage = "https://github.com/peacock0803sz/package_name"} # privateでもよい
[project.optional-dependencies]
test = ["pytest", "freezegun"]
deploy = ["aws-sam-cli"]
research = ["jupyterlab"]