Skip to content

Python

direnv で Python 仮想環境を有効化した際にプロンプト表示も変更するスクリプト

以前に以下のメモを書きました。

一般的に「手動で Python 仮想環境を有効化」した際はプロンプトに仮想環境名が表示される場合が多いと思います。 ですが、上記の過去メモでは direnv で Python 仮想環境を有効化した際、プロンプト表示が変更されない仕様になっていました。 そこで今回は「direnv で Python 仮想環境を有効化した際に、プロンプトに仮想環境名を表示する」ようにスクリプトを改修しました。

Ubuntu 24.04LTS に asdf + uv + direnv で Python 環境を構築する

以前に以下のメモを書きました。

2024/4/24 に Ubuntu 24.04LTS が正式リリースされましたので、改めて Ubuntu 24.04LTS に以下をインストールして Python 環境を構築する手順をメモしておきます。

Ubuntu 22.04.4LTS に uv を入れて Python のパッケージを管理する

比較的新しい Python のパッケージ管理ツールに uv があります。 Rye Grows With UV には以下の記載があります。

Two weeks ago I asked the question again about What Rye should be. There has been one thing that I have not publicly shared before and that is that ever since Rye exists I have also been talking to Charlie Marsh about Python packaging and Python tooling. It turns out that we had some shared ideas of what an ideal Python tooling landscape would look like. That has lead to some very interesting back and forths. To make a potentially very long story short: Together with Astral's release of uv they will take stewardship of Rye. For the details read on.

今回は Ubuntu 24.04.4LTS に uv を入れる手順と基本的な使い方をメモしておきます。 尚、uv で作成した仮想環境へ切り替える際は direnv が便利です。 direnv は過去に以下のメモで触れています。

Python で「ファイルを一行ずつ読み込む」サンプル

Python で「ファイルを一行ずつ読み込む」サンプルをメモしておきます。 よく end="" を忘れて意図しない、余計な改行をしてしまいます…

1
2
3
4
5
#!/usr/bin/env python3

with open("./sample.txt") as f:
    for line in f:
        print(line, end="")

asdf 環境の Python に pip で追加したコマンドが使えない場合の対処

asdf で Python をインストールした環境で pip を使い、CLI から実行可能なコマンドをインストールしたにも関わらず、「コマンドが使えない」という事象が発生します。 下記では例として cmlutils をインストールしていますが、インストール後に cml コマンドへのパスが通っておらず、利用出来ません。

1
2
3
4
# python3 -m pip install cmlutils
(snip)
# which cml
#

この問題は reshim を実行することで解決します。

1
asdf reshim python

これで pip でインストールしたコマンドが利用可能になりました。

1
2
# which cml
/root/.asdf/shims/cml

全ての Docker コンテナを停止・削除する Python スクリプト

以前に 全コンテナを停止・削除する docker サブコマンド「docker purge」 というメモを書きました。 このメモでは Docker のプラグインとして golang で purge というコマンドを作成しました。 Python から Docker を扱うのも簡単なので、今回は Python で同じ目的のスクリプトを書いてみました。