LoginSignup
0
0

More than 1 year has passed since last update.

pythonライブラリのversion管理(not best practice)

Posted at

pythonのライブラリを個人で作成していて、バージョン管理方法に悩みました。

解決したい課題は
・setup.py中のversionと__version__.pyのversionが二重管理になること
・出来れば、ビルドするごとに自動でバージョンが挙がってほしい
 の2点です。

検討した方法

ベストプラクティクスとは思いませんが、以下の様な形式にしました。
黒魔術感はありますが、出来るだけ自然な形にしたつもり

①setup.cfgファイルで__version__.pyを参照する
②__version__.pyのヴァージョンをビルドごとに+1する

version.py

__version__ = "0.1.5" 

setup.cfg

[metadata]
version = attr: library.__version__.__version__ 

setup.py

from setuptools import setup
setup()

update_build.py

import library_name.__version__ as v

__major__ = int(v.__version__.split(".")[0])
__minor__ = int(v.__version__.split(".")[1])
__build__ = int(v.__version__.split(".")[2])
__build__ += 1
__version__ = ".".join([f"{__major__}", f"{__minor__}", f"{__build__}"])
with open("library_name/__version__.py", "w") as f:
     f.write(f"__version__ = \"{__version__}\"")
with open("version.txt", "w") as f:
    f.write(__version__)

setup.bat

@setlocal enabledelayedexpansion
call conda activate env_test
for /f %%a in (version.txt) do (
   set version=%%a)
call python setup.py sdist
call pip uninstall library_name --yes
call pip install  dist\library_name-%version%.tar.gz
call python update_build.py

pip installする際のアーカイブ名を指定するために、version.txtを利用していますが
これは__version__.pyを用いても良いと思います。

setup.batを実行することで、自動でビルド番号の更新ができます。

参考にしたリンク

https://qiita.com/tonluqclml/items/6da162d8af1534bf5e6e
https://qiita.com/utkamioka/items/b62d0e03f1bc1aed5df7
https://qiita.com/gyu-don/items/c047098e1a58dc8d8d96
https://astropengu.in/posts/23/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0