Anaconda Environment in prompt/PS1 in MacOS? - bash

When I was in Ubuntu installing Anaconda and activating a virtual environment modified my prompt to look like that:
(current-env)user#machine:dir
or as you define in .bashrc.
Now that I installed Anaconda in MacOS my prompt remain the same. I tried to configure that from Anaconda doing:
conda config --set changeps1 no
but nothing changed.
How can I modify my .bash_profile to have the current environment in PS1?

I managed to have that changing my shell from bash to zsh.
You can install zsh from here.

In recent versions, Conda has provided automated configuration for a number of shells via the conda init command. This command will edit relevant .*rc files to enable such features of PS1 change. Please see conda init --help for details.

Related

Switch from Pyenv to Conda

I've been using pyenv but I want to switch to Conda. I use VScode, managed to create a Conda environment and use the proper version of Python but when installing packages with PIP, they still install on pyenv and not conda, how do I change that? Should I just remove pyenv from my computer or can I just change the path for pip?
Not sure if that'll help but I did an echo $PATH and got this list
/opt/homebrew/Caskroom/miniforge/base/condabin:/Users/marc/.pyenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Marc, it is not clear from your question whether you've configured conda correctly for VS Code, which means
setting the default terminal
ensuring the default terminal is correctly reading ~/.profile args
ensuring you can select your miniforge conda env from list of python interpreters.
More details about ensuring conda is configured properly is in this related stackoverflow post.
Finally, please ensure that you pip install inside the activated conda env? This is obvious.... for sanity sake ensure you can see the env activated at the shell prompt inside your VSCode terminal before doing pip / conda install commands. Running which pip at the prompt should confirm that you are using pip inside the miniforge path.

I have added conda and python path to the environment variable, but jupyter notebook is still not getting opened from cmd

I was trying to add conda and python to the environment variable using SETX Command from CMD but it was failing. I tried setting it using PowerShell and it worked. The path was added successfully but I still can't open Jupyter Notebook from my cmd.
Adding Python to the environment path is bad practice, see Anaconda FAQ. If you haven't installed Anaconda with it's default settings, you first need to:
Initialize your shells
conda init --all
After this you should have ../Anaconda3/condabin only in your path (more information via conda init --help).
But before you can run Jupyter, you also need to activate Anaconda:
C:\> conda activate
(base) C:\> jupyter notebook
The activation will add the following folders of the conda base environment to your PATH:
\Anaconda3;
\Anaconda3\Library\mingw-w64\bin;
\Anaconda3\Library\usr\bin;
\Anaconda3\Library\bin;
\Anaconda3\Scripts;
\Anaconda3\bin;
The python.exe resides in Anaconda3, jupyter.exe in Anaconda3\Scripts, so it's not enough to just add the first folder to your Path. And it's especially important to have the libraries on your Path when you want to run C-based packages like numpy.
But the very point behind the conda activate mechanism is that it allows you to configure and run different environments with different versions of python and 3rd party packages that would otherwise conflict, see Managing environmnts.
On top of that you can even install Python from python.org next to your Anaconda distribution, since conda will make sure that they won't interfere.

How does one do conda (or miniconda) init on a brand new installation?

I was trying to install miniconda and use it but I get issues on Mac Os. I use their official installation dmg (https://docs.conda.io/en/latest/miniconda.html) and used that to install miniconda. It install it in ~./opt. Then I do:
conda init bash
it says nothing was changed:
(base) brandBrandoParetoopareto~/automl-meta-learning $ conda init bash
no change /Users/brandBrandoParetoopareto/opt/miniconda3/condabin/conda
no change /Users/brandBrandoParetoopareto/opt/miniconda3/bin/conda
no change /Users/brandBrandoParetoopareto/opt/miniconda3/bin/conda-env
no change /Users/brandBrandoParetoopareto/opt/miniconda3/bin/activate
no change /Users/brandBrandoParetoopareto/opt/miniconda3/bin/deactivate
no change /Users/brandBrandoParetoopareto/opt/miniconda3/etc/profile.d/conda.sh
no change /Users/brandBrandoParetoopareto/opt/miniconda3/etc/fish/conf.d/conda.fish
no change /Users/brandBrandoParetoopareto/opt/miniconda3/shell/condabin/Conda.psm1
no change /Users/brandBrandoParetoopareto/opt/miniconda3/shell/condabin/conda-hook.ps1
no change /Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change /Users/brandBrandoParetoopareto/opt/miniconda3/etc/profile.d/conda.csh
no change /Users/brandBrandoParetoopareto/.bash_profile
No action taken.
but then when I try to start a new bash session it says their are issues:
(base) brandBrandoParetoopareto~/automl-meta-learning $ bash
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
brandBrandoParetoopareto~/automl-meta-learning $
and then deactivates my previous environment. Why is that?
There is definitively something wrong with this miniconda because when I do:
python script.py
it says there is a syntax error which means its using a different version of python despite me telling my env to use python3.7 why? Why doesn't it use the version I said?
Related question with nearly no details: conda init not initialising new shell
As far as I know the best thing is to not trust installations blindly, specially if they modify your files (since figuring out if they are changing your bash code correctly is equivalent to the halting problem). So read the output of the installation and conda init <Shell> carefully. For more detail see this: Why is conda init updating my .bash_profile incorrectly?

Why my conda activate does not work though I have a list of conda environments?

I have already created my own environment using conda in linux. When I check conda info --envs, it gives a list of environment like
# conda environments:
#
base * /home1/sriparna/anaconda3
copy_Env_deepgo /home1/sriparna/anaconda3/envs/copy_Env_deepgo
deepgo_2 /home1/sriparna/anaconda3/envs/deepgo_2
deepgo_3 /home1/sriparna/anaconda3/envs/deepgo_3
enzy /home1/sriparna/anaconda3/envs/enzy
parth /home1/sriparna/anaconda3/envs/parth
protein_struc /home1/sriparna/anaconda3/envs/protein_struc
py2 /home1/sriparna/anaconda3/envs/py2
But I cant activate a particular environment. e.g. when I tried conda activate parth it shows
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
Please help me regarding this.
This worked for me:
source {path_to_anaconda}/anaconda3/etc/profile.d/conda.sh
conda activate parth
Conda is not initialized in your shell. Run the following commands in an interactive shell,
conda init
Conda will detect the type of your shell, and write init scripts into the shell's configuration file. (Running it once is enough.)
Detail
Conda updated its environment activation after 4.6.
Quote from conda 4.6 release log
Conda 4.4 allowed “conda activate envname”. The problem was that setting up your shell to use this new feature was not always straightforward. Conda 4.6 adds extensive initialization support so that more shells than ever before can use the new “conda activate” command. For more information, read the output from “conda init –help”
In previous conda, the binaries installed by the default env "base" are exposed into the shell.
After conda init is introduced in conda 4.6, conda only expose command
conda into the PATH. And environment switch is unified by conda activate env-name and conda deactivate on all platforms. But to make these commands work, you have to do an additional initialization with conda init.
Read the conda 4.6 release log for more detail.
I had installed anaconda in Ubuntu VM environment but base was not loading, tried adding the PATH to .bashrc, didn't solve as Conda:not found was error.
Then I solved through:
Press F1>>Terminal: Select Default Profile
You might have available options :
bash
fish
tcsh
xonsh
zsh
powershell
Choose YOUR_SHELL_NAME
Run:
eval "$(/home/mishra/anaconda3/bin/conda shell.YOUR_SHELL_NAME hook)"
#example
eval "$(/home/mishra/anaconda3/bin/conda shell.bash hook)"
Immediately (base) will show

running source activate <env> in Anacaonda Promt fails

There are already many posts describing how to solve 'source' is not recognized as an internal or external command," by adding anaconda to path.
Following the recommended approach is to not check the box to add Anaconda to your path.
How can i start my env from the Anaconda Promt?
conda 4.6.11
conda env list show my env's
conda activate environmentNameHere in the Anaconda terminal will do it. If you want to deactivate an environment you just type conda deactivate

Resources