Why is conda init updating my .bash_profile incorr

2020-05-08 07:16发布

问题:

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

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?

回答1:

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 ":" "\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

after I manually changed it.

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.


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

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.

also closing and opening vscode completely seems to be helpful.

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

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.