Conda activate/deactivate environment breaking PATH - python

I am finding that when I create a Conda environment source activate environment and then deactivate that environment source deactivate environment, my native bash commands no longer work.
I've attached an image of ls working before enabling an environment and then not afterwards. I assume that this is something because of my PATH - but I am a novice and am not sure specifically what to fix. I am doing this in Git Bash on Windows. Anaconda 3 and Python 3.5.
Please work ls, come on

Most probably your conda environment is replacing the parameters and environment variables defined in your local bash profile. I am sure if you open a new environment(bash session), you will find everything is working fine.
I would suggest you add a command to reload bash profile after deactivating the environment, so that the profile variable values will again be applied. It should solve the problem.

There are a few bugs in the activate/deactivate scripts, which I've fixed in versions I call activate.cygwin and deactivate.cygwin.
activate.cygwin: fixes 2 places where the conda command returns a string with a return "\r" at the end that confounds processing.
deactivate.cygwin: fixes conversion of /cygdrive/... to avoid error
CondaValueError: Could not find environment: /cygdrivec:\Users\rjp\Anaconda2\envs...
I've posted the modified scripts to https://bitbucket.org/snippets/plevin/. See instructions at the top of activate.cygwin.

The correct way to deactivate a conda environment is to run conda deactivate, rather than source deactivate environment
Source: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=activate#deactivating-an-environment

I use Git Bash in Windows 10 and encountered the same problem too. The previous answer does not work for me, probably because I don't have any path set in ~/.bash_profile and ~/.bashrc.
I checked my path variable in a fresh git bash, it looks like:
/c/Users/chen_x/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/chen_x/bin....(more)
After . C:/Users/chen_x/Miniconda3/Scripts/activate C:/Users/chen_x/Miniconda3 and . activate snowflakes, the path becomes:
/C/Users/chen_x/Miniconda3/envs/snowflakes:(blalbla..):C:\Users\chen_x\Miniconda3\Library\bin;C:\Users\chen_x\Miniconda3;....(more)
I assume that git bash does not recognize pathes like C:\program files..., then added the following line to C:/Users/chen_x/Miniconda3/Scripts/activate right after the last EXPORT PATH= command (about line 78):
export PATH="$($_CONDA_PYTHON -c "import re; p=re.sub(r'\\\', r'/', r'$PATH'); p=re.sub('(;|:)([A-Z]):', lambda m: ':/'+m.group(2).lower(), p); print(p)")"
It works.
By the way, I create a ~/condaenv script to start conda environment:
#!/bin/bash
. C:/Users/chen_x/Miniconda3/Scripts/activate C:/Users/chen_x/Miniconda3
It would be convinent to :
. ~/condaenv
. activate snowflakes

Related

How to display conda's environment in zsh (not using oh-my-zsh)?

I use zsh but not equipted with oh-my-zsh, since I cloned many git repos on my machine and oh-my-zsh's default theme or 'josh' theme makes it very slow, for cd and ls commands, etc.
Thus I delete oh-my-zsh and only use zsh's default recommended config, except adding some alias and path variables.
Now comes the question: I manage my python environment by conda, and no current enviroment name displayed in my prompt.
I have executed the command:
conda init zsh
And re-open my terminal (default shell is zsh), still not display "(base)" or "(py36)" stuffs.
How can I tell my terminal to show that stuff? The git branch does not matters for my work, but the current python version really matters, since DeepLearning related tools use many different python versions.
For those users with oh-my-zsh
I add anaconda element in ~/.zshrc config file
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(anaconda ...ENVS)
and the conda env shows up
If you're not using oh-my-zsh and have not copy-pasted the relevant sections from there, then the correct answer is that when you do conda init zsh, the environment variable CONDA_PROMPT_MODIFIER is introduced. You want to add that to your PROMPT. You should do so as part of the precmd function, so that the an updated value of the environment variable is shown every time a prompt is printed.
This was caused due to this line in default zsh's config:
prompt adam1
Once delete that line, re-open terminal, conda environment are shown.
You can try,
# init your shell first
conda init zsh
source ~/.zshrc # refresh your shell
source activate base # (optional, on linux) activate base env

How to activate anaconda environment within R

I have qiime2 program https://qiime2.org in my directory /home/owner/anaconda3/envs/qiime2-2019.1. In Linux terminal, I run source activate /home/owner/anaconda3/envs/qiime2-2019.1 to start this program.
I tried doing this within R studio as system('source activate /home/owner/anaconda3/envs/qiime2-2019.1'), but it it gives me this error: sh: 1: activate: not found
Warning message:
In system('activate /home/owner/anaconda3/envs/qiime2-2019.1') :
error in running command
Is there a way to activate anaconda env within R or Rstudio?
Yes there are multiple ways I recommend looking into the reticulate package but basically, R Studio preview 1.2 is capable of "finding" your conda environments.
My prefered way is:
library(reticulate)
library(tidyverse)
# Seeing your enviroments
conda_list()
#Using it
conda_list()[[1]][1] %>%
use_condaenv(required = TRUE)
#Checking python
import platform
print(platform.python_version())
Links
Reticulate: https://rstudio.github.io/reticulate/
My blog: https://twosidesdata.netlify.com/2019/03/23/exploratory-data-analysis-basic-pandas-and-dplyr/#how-to-set-up-reticulate
I think it could not be successful to enter the conda environment in R console, but you still can use the environment command by indicating the path.
For instance, my path of qiime is /home/username/miniconda3/envs/qiime2-2019.7/bin/qiime.
If you want to run the code like qiime info, you can use the command:
system("/home/username/miniconda3/envs/qiime2-2019.7/bin/qiime info")
Generally, I do not use Rstudio but from some searches, I can suggest you try by setting python path instead of activating environment by Conda activate.
You can select which python interpreter you are going to use and here it.
library(reticulate)
path_to_python <- "/anaconda3/envs/qiime2-2019.1/python"
use_python(path_to_python, required = TRUE)
here are some answers for the same type of question:
1> https://stackoverflow.com/a/54813273/9071644
2> https://stackoverflow.com/a/45891929/9071644
3> https://stackoverflow.com/a/43411909/9071644
I know this is an old question, but found some ways one can activate. One is using the library reticulate mentioned above, but I use it this way:
library(reticulate)
use_condaenv("py2_env") #activate an environment e.g py2_env
base::system(paste0("sh py2_env_program_to_run.sh")) #run a program that requires that environment
With reticulate im not sure though how you would deactivate the environment.
The second way, I wrote a little bash script called 'env_eval.sh'
#!/bin/bash
PATH=/opt/conda/bin:$PATH
export PATH
eval "$(conda shell.bash hook)"
Then I reference it in my R script everytime i want to use conda e.g:
base::system2(paste0("sh ",run_folder,"/code/bash/env_eval.sh conda activate py2_env && run_py2_env_program.sh"))
I can even deactivate a conda environment with this method:
base::system(paste0("sh ",run_folder,"/code/bash/env_eval.sh conda deactivate && run_program_that_should_not_be_in_any_conda_env.sh"))
If you want to use Python in RStudio, the best way to get it going is to create a separate 'reticulate' environment using Anaconda.
Part of the reason is that so you can use RMarkdown for your output requires PyQt5 which will break your Jupyter/Spyder environments if you overwrite PyQt.
Then you have to make an .Renviron file like this setup. which points R to the proper Python env. Otherwise, the default for RStudio seems to be a miniconda environment.
Once your separate reticulate environment is set and you have .Renviron pointing to it, all of your Python package installs should go into that environment.

Activating conda environment with cloud-init

We are trying to run batch scripts on load on a AWS EC2 instance using userdata (which I understand is based off of cloud-init). Since the code runs in a conda environment, we are trying to activate it prior to running the Python/Pandas code. We noticed that the PATH variable isn't getting set correctly. (even though it was set correctly prior to making the image, and is set correctly for all users after SSH'ing into instance)
We've tried modifiying the path in the shell script, but nothing is sticking. We ran the following code:
#!/bin/bash
export=/opt/conda/bin:$PATH
which python
which conda
conda activate etl
We checked $PATH before and after running export=/opt/conda/bin (no change). which python returns the wrong python, and which conda returns not found.
$PATH before and after is:
/sbin:/usr/sbin:/bin:/usr/bin
After hours of work, the two critical lines needed (regardless of your usage of the Miniconda AMI) is:
#!/bin/bash
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc #or the path to your conda.sh
source ~/.bashrc
conda activate <full path to environment>
for some reason, cloud-init ignores all other attempts to modify path

How to set the default python path for anaconda on Linux?

I have installed anaconda on a Linux machine. I noticed that after deactivating the anaconda environment with:
source deactivate
When running:
which python
I get:
/home/user/anaconda/bin/python
Instead of
/usr/bin/python
How can I restore this setting?
The comments somewhat cover the answer to the question, but to clarify:
When you installed Anaconda you must have agreed to have it added to your PATH. You'll want to check in your ~/.bash* files and look for any export PATH= lines to check this. So Anaconda is always on your path. The source deactivate command will only deactivate "sub" Conda environments. It will never remove what is called the "root" Conda environment (the one you originally installed). If you don't want Anaconda on your PATH by default then remove it from your ~/.bash* startup files. Then when you want to use Anaconda you'll need to add it to your PATH. Or just add the specific Conda environment you are interested in to your PATH directly, and don't worry about the activate and deactivate scripts. At their core all they do is modify PATH.
I hope that helps clarify things.
Anaconda comes with its own everything, and they ask if you wish to use their software as a default when you install it by adding their bin first to your PATH variable. If you do that, you can only manually remove it later from .bashrc to undo this action.
I chose not to do it, but i made a shell script to start spyder and use the anaconda distribution when i wish to, without altering my PATH by calling spyder like this from the shell script:
PATH=/home/<... path to where i installed anaconda>/bin:$PATH spyder &
This means that i am adding their distribution's bin to the path only for the extent of running that command (spyder), otherwise my environment is unaffected by anaconda.
If i wish to add things to it, i pass an option to the shell when i source it and that triggers these actions:
PATH=/home/<... path to where i installed anaconda>/bin:$PATH
PS1='\[\033[1;34m\](A)\w:\[\033[0m\] '
so that i see (with colors!) that in this terminal i am using an altered PATH, the one with python3 and such from anaconda, etc... When done, i kill the terminal! :)
To get back to the former default, I comment out the entry setting the pathbin the .bashrc file and get these results:
$ which python
/usr/bin/python
$ python -v
python 2.7.*
If you have installed something, you want to use it before older installs, in general.
Your path is well set: your install is searched before system install.
If you want to use system python, run % /usr/bin/python.
If you don't want anaconda, just trash it (or move it somewhere ouside of path).
This may help:
% which -a python.

Why is Anaconda source activate non-existent?

I successfully created two separate Python environments in Anaconda, yet seem to be unable to activate either one of them. I have tried to read up on this topic as much as possible here on Stackoverflow, yet no solution did resolve my issue. I added information asked for by comments in this question (Anaconda Environment Doesnt activate).
When trying to activate an environment, the console output is -bash: activate: No such file or directory.
The output of which conda is /Users/username/anaconda3/bin/conda.
The output of type source is source is a shell builtin.
When trying which activate, the shell returns nothing.
In my bin folder, I also seem not to have an activate executable, but only one which is called activate-global-python-argcomplete.
Why do I lack the standard activate file and how I could resolve this issue best?
I experience a similar problem. In my case, the problem is related to the use of the tcsh, but activate only supports bash and zsh.
You can check your current used shell with the command
echo $0.
You have to use a compatible shell in order to use the source activate command.
I think you happened to install a buggy version of anaconda which was quickly patched.
conda update conda
should get you back up and running.
Start a new terminal and try again. Alternatively, type hash -r and see if it helps.
Do not do this!!!, this broke my dnf
First I tried to point source to a activate file,
source /usr/lib64/python3.6/venv/scripts/common/activate environment
That appeared to work, but no actual values got updated. I tried the answer by mattexx, but it complained conda wasn't installed, so I used.
conda install conda
I had to have root permission since it was being installed to the root environment, but after that everything appears to work.
That is what broke my fedora installation
tcsh was the problem for me. Changed to bash and all is ok.
In this case, the problem might be that the virtual environment was created in /Users/username/anaconda3/envs/ and hence the activate will be in:
/Users/username/anaconda3/envs/NAME_OF_YOUR_VIRTUAL_ENV/bin/activate
Hence to activate the environment you could run:
source /Users/username/anaconda3/envs/NAME_OF_YOUR_VIRTUAL_ENV/bin/activate
My problem was that file /opt/pycharm/plugins/terminal/.zshrc (using ZSH) contained line source $JEDITERM_SOURCE. That variable contained correct paths as two parameters <path-to-anaconda-activate>/activate <path-to-anaconda>, but apparently they got interpreted as a single parameter: <path-to-anaconda-activate>/activate <path-to-anaconda>.
I solved that prepending eval to that line, making it eval source $JEDITERM_SOURCE. Now, the variable expands correctly.
If you're using bash then make the same change to /opt/pycharm/plugins/terminal/jediterm-bash.in.
I'm using the bash shell inside pycharm and fixed the problem by making this change:
[~/opt/pycharm-community/plugins/terminal]$ diff jediterm-bash.in.old jediterm-bash.in
65c65
< source "$JEDITERM_SOURCE"
---
> eval source "$JEDITERM_SOURCE"
Essentially making the same change recommended above by https://stackoverflow.com/users/1564931/netchkin, but to ~/opt/pycharm-community/plugins/terminal/jediterm-bash.in.
This fixed my existing projects. But any new projects don't seem to try and activate the conda environment at all. It seems as if new projects created in Pycharm 2017.1.2 don't set the JEDITERM_SOURCE variable when starting a session in the terminal plugin.
There is an open issue on this at https://youtrack.jetbrains.com/issue/PY-23417
Looks like there was a big change to the conda activate script in version 4.4.0. See https://conda.io/docs/release-notes.html. If I downgrade conda to 4.3.34 then I can get conda virtual environments to work in the pycharm terminal again.

Categories

Resources