跳转至

Shell

pixi shell 命令类似于 conda activate,但内部工作方式略有不同。 它不需要更改你的 ~/.bashrc 或其他文件,而是启动一个新的 shell。 这也意味着,不需要 conda deactivate,只需退出当前 shell,例如按 Ctrl+D 就足够了。

pixi shell

在 Unix 系统上,shell 命令的工作方式是创建一个"假的"PTY 会话来启动 shell, 然后发送一个字符串如 source /tmp/activation-env-12345.shstdin 以激活环境。 如果你在 shell 命令的底层查看,你会发现这是在新 shell 会话中执行的第一个东西。

我们生成的临时脚本以 echo "PIXI_ENV_ACTIVATED" 结尾,用于检测环境是否成功激活。 如果我们没有在三秒后收到此字符串,我们将向用户发出警告。

Shell 补全#

Shell 补全工具会在 pixi shell 中自动加载。 这意味着如果你的环境中的包提供了补全(例如 gitcargo),它们将在没有任何额外配置的情况下可用。

Pixi Shell 的问题#

如前所述,pixi shell 只有在启动 shell 后执行激活脚本才能正常工作。 某些在 ~/.bashrc 中运行的命令可能会吞掉激活命令,环境不会被激活。

例如,如果你的 ~/.bashrc 包含以下代码,pixi shell 几乎不可能成功:

# 在 WSL 上 - `wsl.exe` 以某种方式接管 `stdin` 并阻止 `pixi shell` 成功
wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit start

# 在 macOS 或 Linux 上,一些用户从他们的 `bashrc` 启动 fish 或 nushell
# 如果你想从 bash 启动替代 shell,最好从 `~/.bash_profile` 或 `~/.profile` 做
if [[ $- = *i* ]]; then
  exec ~/.pixi/bin/fish
fi

为了解决这个问题,我们建议你按照以下步骤改用 pixi shell-hook

Traditional conda activate-like activation#

If you prefer to use the traditional conda activate-like activation, you can use the pixi shell-hook command.

$ which python
python not found
$ eval "$(pixi shell-hook)"
$ (default) which python
/path/to/project/.pixi/envs/default/bin/python

For example, with bash and zsh you can use the following command:

eval "$(pixi shell-hook)"
Custom activation function

With the --manifest-path option you can also specify which environment to activate. If you want to add a bash function to your ~/.bashrc that will activate the environment, you can use the following command:

function pixi_activate() {
    # default to current directory if no path is given
    local manifest_path="${1:-.}"
    eval "$(pixi shell-hook --manifest-path $manifest_path)"
}

After adding this function to your ~/.bashrc/~/.zshrc, you can activate the environment by running:

With fish, you can also evaluate the output of pixi shell-hook:

pixi shell-hook | source

Or, if you want to add a function to your ~/.config/fish/config.fish:

function pixi_activate
    # default to current directory if no path is given
    set -l manifest_path $argv[1]
    test -z "$manifest_path"; and set manifest_path "."

    pixi shell-hook --manifest-path "$manifest_path" | source
end
After adding this function to your ~/.config/fish/config.fish, you can activate the environment by running:

pixi_activate

# or with a specific manifest
pixi_activate ~/projects/my_project
Using direnv

See our direnv page on how to leverage pixi shell-hook to integrate with direnv.