我在运行 macOS Mojave 的 Mac 上。我运行 Iterm2 作为终端和 fish 版本 3.0.0。我还使用 pipenv 来管理我的本地开发。
我遇到了这个问题,当我通过 激活 python 虚拟环境时,>pipenv shell
虚拟环境路径被插入到 PATH 列表的中间,/usr/local/bin
在 Mac 上,Homebrew 会将二进制文件安装到 PATH 列表的后面。因此,当我输入 时虚拟环境也会被激活>which python
,我从系统中获取 python,而不是我期望的虚拟环境。
例子
使用 pipenv shell 命令激活虚拟环境之前的路径
PATH=/Users/napo/bin:/Users/napo/.local/bin:/Users/napo/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/sbin:/usr/local/opt/python@2/bin
使用 pipenv shell 命令激活虚拟环境后的路径
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Users/napo/.local/share/virtualenvs/learn-gXU4lJPg/bin:/usr/local/Cellar/pipenv/2018.11.26/libexec/tools:/Users/napo/bin:/Users/napo/.local/bin:/Users/napo/.cargo/bin:/usr/local/sbin:/usr/local/opt/python@2/bin
除了路径条目重复的问题之外,我们还可以看到它/Users/napo/.local/share/virtualenvs/learn-gXU4lJPg/bin
被放置在该列表的中间某处。
我将非常感激您的修复。以下是我目前的完整配置文件
if status --is-login
# Homebrew
set -x HOMEBREW_INSTALL_CLEANUP 1
# Golang
set -gx GOPATH ~/go/
# Don't write bytecode, Python!
set -x PYTHONDONTWRITEBYTECODE 1
set -x PIPENV_SHELL_FANCY 1
set -x PIPENV_MAX_SUBPROCESS 64
set -x EDITOR nvim
set -gx OSTYPE macOS
set -gx WORKON_HOME /Users/napo/.local/share/virtualenvs
# android sdk
set -gx ANDROID_NDK_HOME /usr/local/share/android-ndk
set -gx ANDROID_SDK_ROOT /usr/local/share/android-sdk
# Make fzf respect .gitignore using fd command
set -gx FZF_DEFAULT_COMMAND 'rg --files'
# Now fzf (w/o pipe) will use fd instead of find
# To apply the command to CTRL-T as well
set -gx FZF_CTRL_T_COMMAND $FZF_DEFAULT_COMMAND
set -gx VIMCONFIG ~/.config/nvim
set -gx VIMDATA ~/.local/share/nvim
set -gx VISUAL nvim
# PATHS
set -gx PATH /Users/napo/bin /Users/napo/.local/bin /Users/napo/.cargo/bin $PATH
set -gx PATH $PATH /usr/local/sbin /usr/local/opt/python@2/bin
neofetch
end
# Use personal aliases
if test -e "$HOME/.config/fish/path.fish"
source ~/.config/fish/path.fish
end
if test -e "$HOME/.config/fish/aliases.fish"
source ~/.config/fish/aliases.fish
end
if test -e "$HOME/.config/fish/chpwd.fish"
source ~/.config/fish/chpwd.fish
end
if test -e "$HOME/.config/fish/functions.fish"
source ~/.config/fish/functions.fish
end
# for things not checked into git.
if test -e "$HOME/.extra.fish"
source ~/.extra.fish
end
# Prefer GB English and use UTF-8
set -x LC_ALL en_GB.UTF-8
set -x LANG en_GB
set -g theme_nerd_fonts yes
set -g theme_show_exit_status yes
# Allow 256 colors in iTerm2 for pretty vim colors
#set -gx TERM xterm-256color
set -gx CLICOLOR 1
# better ls colors
eval (gdircolors --c-shell $HOME/dircolors)
# Remove fish greeting
set fish_greeting
# highlighting inside manpages and elsewhere
set -gx LESS_TERMCAP_mb \e'[01;31m' # begin blinking
set -gx LESS_TERMCAP_md \e'[01;38;5;74m' # begin bold
set -gx LESS_TERMCAP_me \e'[0m' # end mode
set -gx LESS_TERMCAP_se \e'[0m' # end standout-mode
set -gx LESS_TERMCAP_so \e'[38;5;246m' # begin standout-mode - info box
set -gx LESS_TERMCAP_ue \e'[0m' # end underline
set -gx LESS_TERMCAP_us \e'[04;38;5;146m' # begin underline
# jump config
status --is-interactive
and source (jump shell |psub)
# direnv config
eval (direnv hook fish)
谢谢
答案1
这是 fish 3.0.0的一个问题 - 请参阅https://github.com/fish-shell/fish-shell/issues/5456。
建议在启动时添加重新排序功能:
function path-reordered -d "Echos the elements of $PATH, reordered such that custom paths precede standard system paths. This can be used to work around a fish 3.0.0 bug in which sub-shells fail to respect path ordering."
set -l sys_paths
for p in $PATH
if string match -q -r '^/usr/|^/s?bin(/|$)' $p
set -a sys_paths $p
else
echo $p
end
end
for p in $sys_paths; echo $p; end
end
然后,在config.fish
:
if status --is-login
set PATH $HOME/.local/bin $PATH
# etc.
else
set PATH (path-reordered)
end