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

标签 python bash anaconda conda

关注 these steps 后安装 conda 后,conda init 似乎由于某种原因错误地更新了我的 .bash_profile 。它在运行 .bashrc 后添加其内容,因此当 bash 启动时,我的所有 conda 内容由于某种原因都无法正确启动。我最终不得不手动更改 .bash_profile 如下所示:

(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 official installation :

sh Anaconda3-2020.02-MacOSX-x86_64.sh

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

是什么导致了我的问题以及如何解决它?

最佳答案

正如 chepner 的评论所说,我最终自己发现 conda init 由于某种原因无法开箱即用(例如,如果它修改 .bash_profile code> 很明显,用户将在某个地方运行他们的 .bashrc 文件,因此它应该在此之前正确添加其内容,或者至少在恕我直言中)。无论如何,这是一个工作文件的示例:

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

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

# >>> 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

在我手动更改它之后。

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


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

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.

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: 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.

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 文件以及重新安装和卸载大量内容的时间。

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

关于python - 为什么 conda init 错误地更新了我的 .bash_profile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60905133/

相关文章:

Python python : should I use `conda activate` or `source activate` in linux

python - python 中没有模块 lxml.html 的库 stub 文件

python - Django - error_403() 得到了一个意外的关键字参数 'exception'

python - 将 float 转换为任意基数

python - 在 Windows 上设置atom linter-python?

python - anaconda 找不到 pip3 并且不想使用 pip 或 pip2

python - 从 numpy 数组中逐一减去行

bash - 以前的参数快捷方式?

bash - 获取 FUNCNAME 中的参数数量

linux - Bash 脚本在文件名中生成项目符号