Skip to main content

manage again via pipenv

pipでインストールしたlibを消して、再びinstallしようと思います。 PyTorchのバージョンをあげるとき、大学サーバーのメモリが上限の30GBを超えており、 datasetscheckpointsを消してみても、22GBも使っていました。

通常ならpip freeze > instaled.txtpip uninstall -r installed.txtで 使っていないlibやバージョンを消せるらしいですが、普段--userオプションでインストールしていろので、 一気に削除しようとするとPermission Errorで処理が止まってしまうので、Pythonでなんとかしようと思いました。

[***@sol ~]$ du -h -d 3 | sort -hr | head -25
22G .
12G ./IED_HOME
9.1G ./.ced_ubuntu
5.7G ./IED_HOME/notebook
5.2G ./IED_HOME/notebook/project
3.9G ./.ced_ubuntu/.cache
3.3G ./IED_HOME/.cache
2.9G ./IED_HOME/.local
2.9G ./.ced_ubuntu/.cache/pip
2.7G ./IED_HOME/.local/lib
2.7G ./IED_HOME/.cache/pip
2.7G ./.ced_ubuntu/.conda/pkgs
2.7G ./.ced_ubuntu/.conda
1.9G ./.ced_ubuntu/.local
1.8G ./.ced_ubuntu/.local/lib
549M ./IED_HOME/.cache/torch
549M ./.ced_ubuntu/.torch/models
549M ./.ced_ubuntu/.torch
549M ./.ced_ubuntu/.cache/torch
464M ./.cache
445M ./.cache/mozilla/firefox
445M ./.cache/mozilla
374M ./.ced_ubuntu/.cache/mozilla
281M ./IED_HOME/notebook/y7_test_super_slomo
206M ./IED_HOME/.local/share

pythonからpipを実行するために、次のような関数を定義しておきます。

import os
import sys
import subprocess
def run(cmd):
proc = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
buf = []
while True:
line = proc.stdout.readline().decode('utf-8')
buf.append(line)
sys.stdout.write(line)
if not line and proc.poll() is not None:
break
return ''.join(buf)

次に、現在の library を取得し、配列を返す関数を用意します。

def get_libs():
run('python3 -m pip freeze > libs.txt')
with open("libs.txt", 'r') as f:
return [l.rstrip('') for l in f.readlines()]

if __name__=='__main__':
pre_libs = get_libs()
# delete libs
cmd = ["python3 -m pip uninstall %s -y"%l.split('==')[0] for l in installed_lib]
_ = [print(run(c)) for c in cmd]
del_libs = [l for l in get_libs() if not l in pre_libs]
# result
libs_len = tuple(len(l) for l in [pre_libs, get_libs(), del_libs])
print("pre:%s, now:%s, del:%s" % libs_len)
_ = [print("\tdel:",l) for l in del_libs ]

結果を確認すると、ちょっとへったかもでした。 pre:250, now:209

22→20G     .
12→7.4G ./.ced_ubuntu
1.9G→0? ./.ced_ubuntu/.local
1.8G→0? ./.ced_ubuntu/.local/lib

conda clean --allしたらちょっと減りました。

20→18G     .
7.4→4.8G ./.ced_ubuntu
1.9G→177M ./.ced_ubuntu/.local
1.8G→?M ./.ced_ubuntu/.local/lib

iedでもcedと同様にためしました。

pri:249 now:239 del:3

18→16G     .
12→11G ./IED_HOME
2.9G→0?M ./.ced_ubuntu/.cache/pip
2.9→1.5G ./IED_HOME/.local
2.7G→0?M ./IED_HOME/.local/lib
2.7→1.3G ./IED_HOME/.local/lib

.cache削除したらすごいことになった(消していいのかよくわかってないです)

8.5G    .
7.3G ./IED_HOME
5.7G ./IED_HOME/notebook
5.2G ./IED_HOME/notebook/project
1.5G ./IED_HOME/.local
1.3G ./IED_HOME/.local/lib
899M ./.ced_ubuntu
549M ./.ced_ubuntu/.torch/models
549M ./.ced_ubuntu/.torch
206M ./IED_HOME/.local/share
177M ./.ced_ubuntu/.local
123M ./.local
114M ./.local/share
113M ./.local/share/Trash
90M ./.ced_ubuntu/.local/lib
87M ./.ced_ubuntu/.local/share