为什么 conda init 错误地更新了我的 .bash_profile?

2022-01-10 00:00:00 python conda bash anaconda

问题描述

按照 这些步骤 并安装 conda 后,似乎 conda init 更新了我的 .bash_profile 由于某种原因不正确.它在运行 .bashrc 之后添加它的内容,因此当 bash 启动时,由于某种原因,我的所有 conda 内容都没有正确启动.我最终不得不手动将 .bash_profile 更改为如下所示:

After following these steps and installing conda it seems that conda init updates my .bash_profile incorrectly for some reason. It adds it's content AFTER running .bashrc and thus when bash gets started all my conda stuff does not get initiated correctly for some reason. I ended up having to change the .bash_profile manually to look like this:

(automl) brandBrandoParetoopareto~ $ cat .bash_profile
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/brandBrandoParetoopareto/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/brandBrandoParetoopareto/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/brandBrandoParetoopareto/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/brandBrandoParetoopareto/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

我安装它的方式是使用我认为的官方安装:

The way I installed it was using what I thought was the official installation:

sh Anaconda3-2020.02-MacOSX-x86_64.sh

但这似乎会导致问题(例如,conda init bash 没有将内容添加到 .bash_profile 的正确位置.图形/dmg 安装程序甚至将内容安装在~/opt 出于某种原因).我概述了对我有用的 hacky 解决方案here,但我认为这不是正确的做事方式.

but that seems to be causing issues (e.g. conda init bash not adding the stuff in the right place to .bash_profile. The graphical/dmg installer even installs things at ~/opt for some reason). I outlined the hacky solution that worked for me here but I assume that is not the right way to be doing things.

是什么导致了我的问题,我该如何解决?

What is causing my problems and how do I fix it?


解决方案

正如 chepner 的评论所说,正如我自己最终发现的那样,conda init 不能开箱即用某些原因(例如,如果它修改了 .bash_profile 很明显用户将在某处运行他们的 .bashrc 文件,因此它应该在此之前正确添加它的内容,或者至少在恕我直言).无论如何,这是一个工作文件的例子:

As the comment by chepner says as I discovered on my own eventually is that conda init doesn't work out of the box for some reason (e.g. if it modifies .bash_profile it's obvious the user will run their .bashrc file somewhere there so it should add it's contents correctly before that, or at least in imho). Anyway this is an example of a working file:

(automl) brandBrandoParetoopareto~/automl-meta-learning $ cat ~/.bash_profile 
echo ----Running .bash_profile

conda -V
python -V
echo $PATH
echo $PATH | tr ":" "
"

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/brandBrandoParetoopareto/anaconda3/envs/automl/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/brandBrandoParetoopareto/anaconda3/envs/automl/etc/profile.d/conda.sh" ]; then
        . "/Users/brandBrandoParetoopareto/anaconda3/envs/automl/etc/profile.d/conda.sh"
    else
        export PATH="/Users/brandBrandoParetoopareto/anaconda3/envs/automl/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

echo ----Completed running .bash_profile

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

在我手动更改后.

我没想到我需要自己修改这些东西.也许我不应该信任命令/安装程序等,而是更频繁地查看他们终端的输出.

I would have not guessed I needed to modify these things myself. Perhaps I should not trust commands/installers etc and see what the output to their terminal is more often.

另外,如果你像我一样在 vscode 中使用集成终端,情况会更糟.为此,请阅读此内容以避免错误/奇怪的行为:https:///code.visualstudio.com/updates/v1_36#_launch-terminals-with-clean-environments

Also, things are worse if your using the integrated terminal in vscode like I was. For that read this to avoid errors/strange behaviours: https://code.visualstudio.com/updates/v1_36#_launch-terminals-with-clean-environments

在干净的环境中启动终端 VS 中的集成终端代码的行为总是与普通终端略有不同,特别是在 Linux 和 macOS 上.原因是环境总是继承自 VS Code 的窗口(实例)和 VS代码/电子相关的环境变量被删除,而普通终端通常会从 Dock/Start 菜单启动并使用系统环境.这可能会导致某些问题场景,例如 Python 虚拟环境被破坏,因为他们如何使用 $PATH 变量.

Launch terminals with clean environments The Integrated Terminal in VS Code has always acted a little differently to normal terminals, particularly on Linux and macOS. The reason is that the environment was always inherited from VS Code's window (instance) and VS Code/Electron-related environment variables were removed, whereas a normal terminal would typically be launched from the Dock/Start menu and use the system environment. This could cause issues in certain scenarios, for example Python virtual environments were broken because of how they use the $PATH variable.

有一个新的预览选项,terminal.integrated.inheritEnv,它当 false 导致终端不使用 VS Code 的环境.

There's a new preview option, terminal.integrated.inheritEnv, that when false causes the terminal to not use VS Code's environment.

相反,根据平台,它将执行以下操作:

Instead, depending on the platform, it will do the following:

Linux:获取和使用VS的父进程的环境代码的主要过程".macOS:拉一把重要的环境当前环境中的变量,仅包含它们.最终我们希望 macOS 的行为与 Linux 相同,但目前是获取环境的问题.窗户:目前此设置不影响 Windows.主要可见结果将 inheritEnv 设置为 false 是 $SHLVL(shell 级别)现在应该是1 和 $PATH 不应包含重复的路径,前提是您的启动脚本不会有意包含它们.

Linux: Fetch and use the environment of the parent process of VS Code's "main process". macOS: Pull a handful of important environment variables off the current environment and only include them. Eventually we would like macOS to behave the same as Linux but there are currently issues with fetching environments. Windows: Currently this setting does not affect Windows. The main visible result of setting inheritEnv to false is that $SHLVL (shell level) should now be 1 and $PATH should not include duplicate paths, provided your launch scripts don't intentionally include them.

terminal.integrated.inheritEnv 的默认值为 true,即以前的行为,但我们可能会将值切换为 false未来.

The default value for terminal.integrated.inheritEnv is true, which is the previous behavior, but we will probably switch the value to false in the future.

完全关闭和打开 vscode 似乎也很有帮助.

希望这将节省人们更改 bash 文件以及重新安装和卸载一堆东西的日子.

Hopefully, this will save people's days of changing bash files around and re-installing and uninstalling a bunch of things.

另一个有用的提示是转到 vscode 左上角的 code 点击它然后转到首选项然后设置.然后您可以通过取消单击/选择将 terminal.integrated.inheritEnv 更改为 false.

Another helpful tip is to go the top left of vscode where it says code click it then go to preferences and then settings. Then you can change the terminal.integrated.inheritEnv to false by unclicking/selecting it.

相关文章