2025年10月まで、pyenvとconda でpython仮想環境を構築してきたが、uvの方がよい、特にはるかに高速であるという情報をしばしば目にするようになったので、新たにuv そしてuvで標準で使用できるvenvでpython仮想環境を構築する。
uvの仮想環境は、デフォルトでは個人ディレクトリにできるのだが、本研究室では/usr/localの下にこれまで仮想環境を構築してきたので、それを踏襲する。
まずuvをインストールする。
$ curl -LsSf https://astral.sh/uv/install.sh | sudo sh -s -- --install-dir /usr/local/bin
次にディレクトリ/usr/local/uvを作成し、ownerを自分にする。こうするのは、rootで作業をして、間違って重要なファイルを削除するなどの問題を起こさないためである。よりよい方法は、メンテナンスを担うユーザーグループを作って、そのユーザー権限で作業を行うことだそう。
export UV_PYTHON_INSTALL_DIR="/usr/local/uv/python"
export VENV_ROOT="/usr/local/uv/envs"
uv venv /usr/local/uv/envs/py3.13 --python 3.13
source /usr/local/uv/envs/py3.13/bin/activate
# 主要パッケージのインストール
uv pip install cartopy iris netcdf4 spectrum cdsapi dill xgcm xskillscore cf_xarray cmap copernicusmarine seaborn numba scikit-learn dask flake8 beautifulsoup4 pyflakes gsw statsmodels ipython plotly esgf-pyclient
#確かに速度は圧倒的に速い。これなら個人個人で環境構築するのでも良さそうだ。なお、condaで入ったesmf は入らなかった。py3.14(最新)にはnaumba と spectrum も入らなかった。
この環境を使えるようにするには、.bashrcに以下を記入し
export VENV_ROOT="/usr/local/uv/envs"
alias actvpy313="source $VENV_ROOT/py3.13/bin/activate"
たうえで、
$ actvpy313
で環境に入ることができる。
$ deactivate
で環境から抜ける。
Until October 2025, I had been building Python virtual environments using pyenv and conda, but since I began seeing more information suggesting that uv is better—especially much faster—I decided to start constructing Python virtual environments using uv and its standard venv system.
By default, uv creates virtual environments under the user’s home directory, but in our lab we have been setting them up under /usr/local, so I will continue following that approach.
First, install uv:
$ curl -LsSf https://astral.sh/uv/install.sh | sudo sh -s -- --install-dir /usr/local/bin
Next, create the directory /usr/local/uv and change its owner to myself.
This is to avoid potential issues, such as accidentally deleting important files, that might occur if working as root.
A better practice would be to create a user group responsible for maintenance and perform the work under that group’s privileges.
export UV_PYTHON_INSTALL_DIR="/usr/local/uv/python"
export VENV_ROOT="/usr/local/uv/envs"
uv venv /usr/local/uv/envs/py3.13 --python 3.13
source /usr/local/uv/envs/py3.13/bin/activate
uv pip install cartopy iris netcdf4 spectrum cdsapi dill xgcm xskillscore cf_xarray cmap copernicusmarine seaborn numba scikit-learn dask flake8 beautifulsoup4 pyflakes gsw statsmodels ipython plotly esgf-pyclient
Indeed, the installation speed is remarkably fast.
With this performance, it might even be practical for each individual to build their own environment.
However, esmf, which was available through conda, could not be installed.
Also, in Python 3.14 (the latest version), numba and spectrum could not be installed either.
To make this environment easy to use, add the following lines to .bashrc:
export VENV_ROOT="/usr/local/uv/envs"
alias actvpy313="source $VENV_ROOT/py3.13/bin/activate"
Then, you can enter the environment with:
$ actvpy313
and leave it with:
$ deactivate