-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't execute conda activate
from bash script
#7980
Comments
Functions are not exported by default to be made available in subshells. I'd recommend you do:
Closing as non-issue. Feel free to re-open if you feel otherwise. ******* EDIT ******* |
@nehaljwani How would you do this, for example, in a makefile, and is there a simple way to get the path to |
That seems a messy solution to the issue. Now there is a source line in your bash scripts that has to explicitly reference your conda installation path, making them not very portable. If you use something like Is there a conda variable that always returns your base path regardless of your current location? |
Try this:
|
@nehaljwani When conda is installed and a user's
|
that works perfectly~!!! |
I'm trying that, but get:
I'm on Mojave and installed with |
This is not a non-issue @nehaljwani. As a user, I would expect that when I run "conda init", that it would prepare my shell for use of "conda activate" everywhere, including within scripts. But it doesn't. The expected behavior should be exactly what @mjspeck specified, namely that "conda should activate in a bash script the same way it does on the command line". @rbutleriii is correct that your proposal of adding the line ~/$MY_CONDA_INSTALLATION/etc/profile.d/conda.sh before calling "conda activate" now directly references your personal conda installation. That means that tools that generate "conda activate" calls (e.g. Nextflow) would have to be hand-patched for them to work correctly. @stuarteberg proposed a perfectly reasonable solution that works (I tested it and yes it works), namely that conda init could add the following to your ~/.bashrc:
I respectfully ask that you re-open this issue and use @stuarteberg's solution (or some other solution) fulfill the expected behavior requested by @mjspeck (i.e. "conda should activate in a bash script the same way it does on the command line"), because that behavior is (I believe) what conda's users are expecting. @stuarteberg's solution would be very easy to implement, and would indeed drastically reduce headaches. Thank you!! |
@jasonsydes I fully agree. I also had to add Also note that, until this get fixed, you need to keep these |
+1 for re-opening |
I have noticed that you can do:
|
Happy to reopen this, but we have no intention of doing development on this any time soon. The shell scripts are devilishly complicated, and we don't have time to get into them right now. What looks like a "simple change" may break a terrible number of seemingly unrelated stuff. Community PRs are most welcome. |
for Windows:
|
this works |
Intentionally keep conda activate outside script per conda/conda#7980
This fixed the issue in my makefile: https://stackoverflow.com/a/55696820/4279266 |
I think a cleaner way is: eval "$(conda shell.bash hook)"
conda activate my_env |
On the windows environment use "anaconda prompt" instead of "command prompt". Go to search. |
If you don't trust the project testing enough for doing delicate changes, how can you trust community PRs doing delicate changes? |
It is extremely annoying that there is no properly documented way of how to do this. Many people need this to automate jobs which require activating conda environments from within various kinds of scripts and the internet is full with contradicting and diverse recipes for how to do this, which work for some people and not for others. |
FWIW, in the meantime, there's micromamba with descent docker images and a much faster response rate, which many of us have migrated to: https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html |
I think the only relevant point here would be if mamba/micromamba has a documented and working way for how to activate environments from within a script? How does Mamba differ from Conda in this respect, if at all? |
@johann-petrak here's an example of me using it in a bash script: https://github.com/huggingface/api-inference-community/blob/main/docker_images/sklearn/run_app.sh And the corresponding docker file: https://github.com/huggingface/api-inference-community/blob/main/docker_images/sklearn/Dockerfile |
@adrinjalali please keep this issue on topic, this is about conda having no well-documented way to activate an environment from within a script. I have tried mamba and had problems which should not get discussed here and mamba also does not document wheter this is a way that is supposed to work. The issue here is that there are different recipes floating around for how to do this with Conda, which work for some people and not for others or work for some conda versions only and not for others. Conda should provide a documented standard way for how to activate an environment from within a bash script. Then, if that does not work, it clearly is a bug that can get reported and fixed. |
I very much agree with you on all things here @johann-petrak , but I'm not suggesting cc'ing @wolfv re: docs though. |
For me, neither of the proposed methods work. E.g. I can create a test environment ( 1.) #!/bin/bash
CONDA_BASE=$(conda info --base)
source $CONDA_BASE/etc/profile.d/conda.sh
conda activate test_activate 2.) #!/bin/bash
eval "$(conda shell.bash hook)"
conda activate test_activate 3.) I also tried out this solution and to run the script in an interactive shell: #!/usr/bin/env bash -l
conda activate test_activate I do not get any errors when running these options but still the environment goes not get activated. I am using |
This is not going to be working because |
Since this issue popped up again - I recently had to deal with this, and although I'm the first one to admit that it's a disgusting hack, at least it works (for my use-case). 😅 This is bash-only, not compatible with any other shell. # we need to ensure our current process (in which this script is running) can
# find the activation mechanisms; usually these are inserted into ~/.bashrc,
# but in this shell, this isn't loaded.
# Extract the part that `conda init` has inserted (which conveniently has
# markers around it). Ensure sed-replacement can deal with multiline, see
# https://unix.stackexchange.com/a/152389
if [ ! -f .localinitrc ]; then
extract=$(cat ~/.bashrc | tr '\n' '\f' | sed -E 's/.*\# >>> conda initialize >>>(.*)\# <<< conda initialize <<<.*/\1/g' | tr '\f' '\n')
echo "$extract" > .localinitrc
fi
# run extracted bits in this process
source .localinitrc |
I had the same situation. |
@jmimi : But this would only work for Linux right? |
It should work for macos too. It is only shell script. |
Sorry, phrased the question wrong: This won't work on Windows would be the better question. |
Windows is a whole different story. This issue is about a "bash script" -- bash is a *nix shell. IIUC, conda support powershell, I'd look for solutions there. (Or the older cmd shell) |
For windows ChrisBarker said about that. |
For future Googlers, I am writing out my problem and how #7980 (comment) helped solve it. I originally had source ~/miniconda3/bin/activate
conda activate my_env within a script to which
I found the reason for the error within
The last line attempted to source non-existence environment source ${NEW_SOFT_DIR}/miniconda3/etc/profile.d/conda.sh
conda activate my_env Enjoy ... |
I have faced the same problem and found an unexpectedly simple solution. Some Contexts: I use the shell script to setup a separate environment for each python project. These projects are located in the server (CentOS 7.9) so I connect to the workspace folder by ssh in vscode which is a bash terminal. The solution is to use the Hope it works! |
That sadly is not a solution because the very problem discussed in this issue is to set the conda environment from within a bash script that is getting executed from a new bash command. There are many reasons why one may want to avoid sourcing commands and instead running a script from a new shell and there we face the problem. Sadly the conda developers have not provided any documented standard solution for this and people are continuing to share anectodal evidence of what may or may not work in certain situations for some. |
OK I get it. btw, why should not soucing command for a bash script? Maybe for the security issue? |
+1 that this is a serious problem. It's always a chore to figure out which of these hacks is going to make it work on any given system I have to deal with. |
Nothing here semmed to work, but i read somewhere it's supposed to be an interactive shell command, so my research towards that got me here. so in the end this command did finally work (obv replace $conda_env with your env name) |
perfect ! |
Current Behavior
Trying to use
conda activate my_env
does not work inside a bash script. The workaround is to usesource activate my_env
but this shouldn't be necessary.Steps to Reproduce
Write a bash script with the following code inside, let's call it
my-script.sh
:When you run
./my-script.sh
, you get the following output:However, conda is in fact installed correctly, and runs fine in the command line.
The command
which conda
returns the following:Expected Behavior
conda should activate in a bash script the same way it does on the command line.
Environment Information
`conda info`
`conda config --show-sources`
`conda list --show-channel-urls`
The text was updated successfully, but these errors were encountered: