目標作業時間
- 5~10分
Python を使った開発実務経験がある方であれば数分でできる作業です(⁎˃ᴗ˂⁎)
前提
以下の状態の方向けの記事です
- 「
cProfileはすでに実行していて、その出力結果を可視化したい」 - snakeviz がなんかインストールできないんですけど...
参考までに、 cProfile は以下のように実行できます。
# cProfile 実行例
$ python -m cProfile -o ./profile_result.prof main.py
| 項目 | 説明 |
|---|---|
profile_result.prof |
cProfile の出力結果を格納するファイルのパスを指定する |
main.py |
プロファイリング対象コード |
./profile_result.prof にはバイナリファイルが出力され、そのままでは中身を見ることができません。そこで、snakeviz が必要になります。
目標作業時間
- snakeviz インストール: スムーズに行けば 5分
snakeviz とは
問題
cProfile は docker 上でも使えるけど、 docker からでは UI (browser) にアクセスできないため、 snakeviz の実行は docker 無しで MacOS 上で直接実行する必要がある。
snakeviz インストール時エラーへの対処
MacOS で snakeviz をインストールして使いたかったのだけど、以下のようなエラーが出ていた。
% pip3 install snakeviz --user
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
xyz, where xyz is the package you are trying to
install.
If you wish to install a Python library that isn't in Homebrew,
use a virtual environment:
python3 -m venv path/to/venv
source path/to/venv/bin/activate
python3 -m pip install xyz
If you wish to install a Python application that isn't in Homebrew,
it may be easiest to use 'pipx install xyz', which will manage a
virtual environment for you. You can install pipx with
brew install pipx
You may restore the old behavior of pip by passing
the '--break-system-packages' flag to pip, or by adding
'break-system-packages = true' to your pip.conf file. The latter
will permanently disable this error.
If you disable this error, we STRONGLY recommend that you additionally
pass the '--user' flag to pip, or set 'user = true' in your pip.conf
file. Failure to do this can result in a broken Homebrew installation.
Read more about this behavior here: <https://peps.python.org/pep-0668/>
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
解決策
これで解決!
% brew install pipx
% pipx install snakeviz
% snakeviz {cProfileで出力した結果}
実行結果
以下のような結果になります
# pipx を install
% brew install pipx
==> Fetching downloads for: pipx
==> Downloading https://ghcr.io/v2/homebrew/core/pipx/manifests/1.8.0-1
################################################################################################################# 100.0%
==> Fetching dependencies for pipx: ca-certificates, openssl@3, readline, sqlite, expat and python@3.14
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2025-11-04-1
...
🍺 /opt/homebrew/Cellar/python@3.14/3.14.0_1: 3,759 files, 69.6MB
==> Installing pipx
==> Pouring pipx--1.8.0.all.bottle.1.tar.gz
🍺 /opt/homebrew/Cellar/pipx/1.8.0: 157 files, 1MB
==> Running `brew cleanup pipx`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> Caveats
zsh completions have been installed to:
/opt/homebrew/share/zsh/site-functions
# snakeviz を pipx 経由でインストール
% pipx install snakeviz
installed package snakeviz 2.2.2, installed using Python 3.14.0
These apps are now globally available
- snakeviz
done! ✨ 🌟 ✨
# snakeviz 実行
% snakeviz {cProfileで出力した結果}
# 例
% snakeviz ./example.prof
snakeviz web server started on 127.0.0.1:8080; enter Ctrl-C to exit
http://127.0.0.1:8080/snakeviz/%2FUsers%2F....
これを実行すると、開いているブラウザに自動的にタブが追加されて、以下のようなプロファイリング結果が確認できます!
