もりはやメモφ(・ω・ )

ITとか読書感想文とか

asdfでセットアップしたpythonでpipで入れたコマンドが使えない問題が解決しました

TL;DR

pipで何かしらのツールをインストールした後、以下のコマンドを実行します。

asdf reshim python

答えは以下のサイトにありました。asdf公式ページをチラ見しても解決策がなかったので助かりました。 Thank you chriserin !

Get pip installed executables into the asdf path - Today I Learned

起きていたこと

同僚氏からお勧めしてもらったasdfを最近触り始めています。

asdf-vm.com

terraformkubectl などのコマンドは早々に導入していて、最近は python もasdfで管理するようにしています。 ところがpipで入れたツールが使えない問題が発生しました。 具体的には以下のブログを参考に checkov というツールをpipで導入しましたが、パスが通っておらず利用できない状態でした。

faun.pub

pip install checkov 自体は成功していたため実行可能なバイナリなどはどこかにはあると判断し find / -name checkov で探索したところ以下のパスに存在していることはわかりました。

/System/Volumes/Data/Users/morihaya/.asdf/repository/plugins/checkov
/System/Volumes/Data/Users/morihaya/.asdf/installs/python/3.10.8/bin/checkov
/System/Volumes/Data/Users/morihaya/.asdf/installs/python/3.10.8/lib/python3.10/site-packages/checkov

試しで /System/Volumes/Data/Users/morihaya/.asdf/installs/python/3.10.8/bin/checkov を指定することで、checkovを実行することはできました。 あとはこれをパス指定不要で実行したいのですが、当初は fish.config へ"/System/Volumes/Data/Users/morihaya/.asdf/installs/python/3.10.8/bin/" を追記しようと考えました。

しかしこれではせっかく asdf でバージョンを管理することの意味が失われてしまいます。そのため公式ドキュメントを確認しましたがパッと解決策は見つからず、"asdf pip how to set path" でWeb検索したところに上述したサイトが見つかり、無事に asdf reshim python で解決できたという話です。

asdf reshim コマンド

今回の解決策となった asdf reshim ですが公式ドキュメントによると以下とあります。ここでは nodejs が例として挙げられていますが python でも同じことが起きていたようです。

This recreates the shims for the current version of a package. By default, shims are created by plugins during installation of a tool. Some tools like the npm CLI allow global installation of executables, for example, installing Yarn via npm install -g yarn. Since this executable was not installed via the plugin lifecycle, no shim exists for it yet. asdf reshim nodejs will force recalculation of shims for any new executables, like yarn, for of nodejs .

Core | asdf

抜粋すると以下が行われたってことですね。

will force recalculation of shims for any new executables