Skip to content

Python を mise でインストールし、uv の venv で仮装環境を管理する

asdf と似たバージョンマネージャに mise があります。 about には mise の特徴として 3 点、挙げられています。

Its functionality is grouped into 3 categories described below.

mise installs and manages dev tools/runtimes like node, python, or terraform both simplifying installing these tools and allowing you to specify which version of these tools to use in different projects. mise supports hundreds of dev tools.

mise manages environment variables letting you specify configuration like AWS_ACCESS_KEY_ID that may differ between projects. It can also be used to automatically activate a Python virtualenv when entering projects too.

mise is a task runner that can be used to share common tasks within a project among developers and make things like running tasks on file changes easy.

今回は mise を使って Python をインストールし、更に mise と uv を連携させ、uv で作成した venv 環境を利用する手順をメモしておきます。 尚、公式サイトには mise と Python を組み合わせて利用する際の設定例が記載された Mise + Python Cookbook というページもあります。

検証環境

対象 バージョン
Ubuntu 24.04.1 LTS
mise 2025.2.3
uv 0.5.29

インストール

インストール方法は Getting Started に記載されています。 curl https://mise.run | sh のように実行してインストールする方法もありますが、apt や dnf のようなパッケージマネージャでインストールする方法もあります。 今回は Ubuntu なので apt でインストールします。

sudo apt update -y && sudo apt install -y gpg sudo wget curl
sudo install -dm 755 /etc/apt/keyrings
wget -qO - https://mise.jdx.dev/gpg-key.pub | gpg --dearmor | sudo tee /etc/apt/keyrings/mise-archive-keyring.gpg 1> /dev/null
echo "deb [signed-by=/etc/apt/keyrings/mise-archive-keyring.gpg arch=amd64] https://mise.jdx.dev/deb stable main" | sudo tee /etc/apt/sources.list.d/mise.list
sudo apt update
sudo apt install -y mise

今回はバージョン 2025.2.3 がインストールされました。

# apt info mise
Package: mise
Version: 2025.2.3
Priority: optional
Section: default
Maintainer: Jeff Dickey @jdx
Installed-Size: 43.6 MB
Homepage: https://github.com/jdx/mise
License: MIT
Vendor: none
Download-Size: 15.8 MB
APT-Sources: https://mise.jdx.dev/deb stable/main amd64 Packages
Description: The front-end to your dev env
# mise --version
              _                                        __
   ____ ___  (_)_______        ___  ____        ____  / /___ _________
  / __ `__ \/ / ___/ _ \______/ _ \/ __ \______/ __ \/ / __ `/ ___/ _ \
 / / / / / / (__  )  __/_____/  __/ / / /_____/ /_/ / / /_/ / /__/  __/
/_/ /_/ /_/_/____/\___/      \___/_/ /_/     / .___/_/\__,_/\___/\___/
                                            /_/
2025.2.3 linux-x64 (2025-02-09)

mise の activate

mise を利用するには事前に mise を activate する必要があります。 2. Activate mise に activate 用コマンド例が書かれていますが、mise のインストールパスに応じて微修正する必要があります。 インストール方法に応じて mise のインストールパスは異なり、私の試した環境では以下のようになりました。

インストール方法 インストールパス activate 用コマンド
シェルスクリプト ~/.local/bin/mise echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashrc
apt /usr/bin/mise echo 'eval "$(/usr/bin/mise activate bash)"' >> ~/.bashrc

設定後はシェルを再起動し、変更を反映します。

シェル補完が有効化出来ない?

シェル補完について Autocompletion に記載されています。 bash 場合は以下を実行するようにガイドされています。

mkdir -p /etc/bash_completion.d/
mise completion bash --include-bash-completion-lib > /etc/bash_completion.d/mise

ですが、bash-completion インストール済みではあるものの、私の環境では mise コマンドに続けて Tab を入力すると以下のエラーになってしまい、補完されませんず、現時点では解決方法を見つけられませんでした…

Error: usage CLI not found. This is required for completions to work in mise.
See https://usage.jdx.dev for more information.

mise で Python をインストールする

試しに mise で Python をインストールしてみます。 mise ls-remote python でインストール可能な Python のバージョン一覧を確認出来ます。

mise ls-remote python

Python 3.12.9 をインストールしてみます。

mise install python@3.12.9

利用する Python バージョンを指定する

インストールされているバージョンの一覧は mise ls または mise list で一覧表示出来ます。

# mise ls
Tool    Version  Source  Requested
python  3.12.9

インストールした Python を、現在のディレクトリ配下で利用するには以下を実行します。

mise use python@3.12.9

指定したバージョンの Python を (特定ディレクトリ配下だけでは無く) グローバルに利用したい場合は -g オプションを指定します。

mise use -g python@3.12.9

uv で仮装環境を作成する

mise で利用する Python バージョンを指定した際、以下のような内容で mise.toml というファイルが作成されます。

1
2
[tools]
python = "3.12.9"

この状態で uv を使って仮想環境を作成します (今回は uv init は利用しない前提にしています)。

uv venv

mise の設定ファイルに uv で作成した仮想環境 (.venv ディレクトリ) を利用するよう、設定を追加します。

cat << EOF >> mise.toml

[env]
_.python.venv = ".venv"
EOF

これで uv で作成した仮装環境が利用されるようになりました。

タスクを登録する

mise ではタスクを登録し、簡単に実行することが出来ます。 この機能は公式サイトの Tasks に書かれています。 今回は例として「Python のソースコードを ruff で構文チェックし、問題があれば修正する」というタスクを登録してみます。 前提として ruff をインストールしておきます。 ruff はシステム共通で利用したい為、uv tool install でインストールしておきます。 今回の実行環境では ~/.local/bin/ruff にインストールされました。

uv tool install ruff

mise にタスクを登録します。 mise.toml に以下を追加します。 以下の tasks.lint のうち、lint という部分が実際のタスク名になります。

[tasks.lint]
run = "ruff check . --fix"

mise run [TASK] のように実行すると指定したタスクを実行出来ます。 下記は lint という名前で定義したタスクを実行しています。 この実行方法の場合、タスク名は完全に入力する必要があり、今回であれば mise run lint は実行可能ですが mise run l などでは実行出来ません。

# mise run lint
[lint] $ ruff check . --fix
All checks passed!

もしくはタスク名を指定せず、mise run を実行すると定義されたタスク名の一覧が表示されます。 タスク名の入力を促されますので lint のように実行すれば先ほど同様、タスクを実行出来ます。 この実行方法の (完全なタスク名では無く) l のように入力してもタスクは実行可能です。 もし同じアルファベットで始まるタスクが複数、定義されている場合は「mise.toml ファイルで先に定義されているタスク」が実行されるようです。

# mise run
Tasks
Select a task to run
❯ lint
/

参考

mise のヘルプ

# mise --help
mise manages dev tools, env vars, and runs tasks. https://github.com/jdx/mise

Usage: mise [OPTIONS] [TASK] [COMMAND]

Commands:
  activate      Initializes mise in the current shell session
  alias         Manage aliases [aliases: a]
  backends      Manage backends [aliases: b]
  bin-paths     List all the active runtime bin paths
  cache         Manage the mise cache
  completion    Generate shell completions
  config        Manage config files [aliases: cfg]
  deactivate    Disable mise for current shell session
  doctor        Check mise installation for possible problems [aliases: dr]
  en            [experimental] starts a new shell with the mise environment built from the current configuration
  env           Exports env vars to activate mise a single time [aliases: e]
  exec          Execute a command with tool(s) set [aliases: x]
  fmt           Formats mise.toml
  generate      [experimental] Generate files for various tools/services [aliases: gen]
  implode       Removes mise CLI and all related data
  install       Install a tool version [aliases: i]
  install-into  Install a tool version to a specific path
  latest        Gets the latest available version for a plugin
  link          Symlinks a tool version into mise [aliases: ln]
  ls            List installed and active tool versions [aliases: list]
  ls-remote     List runtime versions available for install.
  outdated      Shows outdated tool versions
  plugins       Manage plugins [aliases: p]
  prune         Delete unused versions of tools
  registry      List available tools to install
  reshim        Creates new shims based on bin paths from currently installed tools.
  run           Run task(s) [aliases: r]
  self-update   Updates mise itself.
  set           Set environment variables in mise.toml
  settings      Manage settings
  shell         Sets a tool version for the current session. [aliases: sh]
  sync          Synchronize tools from other version managers with mise
  tasks         Manage tasks [aliases: t]
  test-tool     Test a tool installs and executes
  tool          Gets information about a tool
  trust         Marks a config file as trusted
  uninstall     Removes installed tool versions
  unset         Remove environment variable(s) from the config file.
  unuse         Removes installed tool versions from mise.toml [aliases: rm, remove]
  upgrade       Upgrades outdated tools [aliases: up]
  use           Installs a tool and adds the version to mise.toml. [aliases: u]
  version       Display the version of mise [aliases: v]
  watch         Run task(s) and watch for changes to rerun it [aliases: w]
  where         Display the installation path for a tool
  which         Shows the path that a tool's bin points to.
  help          Print this message or the help of the given subcommand(s)

Arguments:
  [TASK]
          Task to run.

          Shorthand for `mise task run <TASK>`.

Options:
  -C, --cd <DIR>
          Change directory before running command

  -E, --env <ENV>
          Set the environment for loading `mise.<ENV>.toml`

  -j, --jobs <JOBS>
          How many jobs to run in parallel [default: 8]

          [env: MISE_JOBS=]

      --output <OUTPUT>


      --raw
          Read/write directly to stdin/stdout/stderr instead of by line

      --no-config
          Do not load any config files

          Can also use `MISE_NO_CONFIG=1`

  -y, --yes
          Answer yes to all confirmation prompts

  -q, --quiet
          Suppress non-error messages

      --silent
          Suppress all task output and mise non-error messages

  -v, --verbose...
          Show extra output (use -vv for even more)

  -h, --help
          Print help (see a summary with '-h')

Examples:

    $ mise install node@20.0.0       Install a specific node version
    $ mise install node@20           Install a version matching a prefix
    $ mise install node              Install the node version defined in config
    $ mise install                   Install all plugins/tools defined in config

    $ mise install cargo:ripgrep            Install something via cargo
    $ mise install npm:prettier             Install something via npm

    $ mise use node@20               Use node-20.x in current project
    $ mise use -g node@20            Use node-20.x as default
    $ mise use node@latest           Use latest node in current directory

    $ mise up --interactive          Show a menu to upgrade tools

    $ mise x -- npm install          `npm install` w/ config loaded into PATH
    $ mise x node@20 -- node app.js  `node app.js` w/ config + node-20.x on PATH

    $ mise set NODE_ENV=production   Set NODE_ENV=production in config

    $ mise run build                 Run `build` tasks
    $ mise watch build               Run `build` tasks repeatedly when files change

    $ mise settings                  Show settings in use
    $ mise settings color=0          Disable color by modifying global config file