1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

requirements.txtのモジュールたちが古くなってしまったので

Posted at

個人利用のpythonバージョンを切り替えて、普段つかっているモジュールのアップデートをしたいときにアップデートしたいけどいい方法が見つからなかったのでバージョンを消すツールを書きました。

pip install -r ./requirements.txt で 読み込んだモジュールにバージョン表記がなかったら最新のものをとってくる!?というのを参考にやってみた。

オリジナルのrequirements.txt

docutils==0.16
jmespath==1.0.1
pyasn1==0.6.1
python-dateutil==2.9.0.post0
PyYAML==6.0.2

処理をしたらこうなるみたいな 理想像的 requirements.txt

docutils
jmespath
pyasn1
python-dateutil
PyYAML

設定ファイル

[path]
req_txt=./requirements.txt
new_one=./new_requirements.txt

requirements.txtからバージョンを消したやつを作成するコード

import os
import configparser


def read_config():
    config = configparser.ConfigParser()
    config.read("./settings.ini", encoding="utf-8")
    org_txt, new_txt = config["path"]["req_txt"], config["path"]["new_one"]
    return org_txt, new_txt


def create_requirements_txt_new_one(org_txt: str, new_txt: str):

    if os.path.exists(org_txt):
        modules = []
        with open(org_txt, mode="r", encoding="utf-8") as req:
            for line in req.readlines():
                module_name = line.split("==")[0]
                modules += [module_name]

        with open(new_txt, mode="w", encoding="utf-8") as wf:
            for module in modules:
                wf.writelines(module + "\n")
    else:
        raise Exception


if __name__ == "__main__":
    try:
        src, dest = read_config()
        create_requirements_txt_new_one(src, dest)
    except Exception as e:
        print(f"\n{e:*^30}")

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?