Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile can't use `conda activate`

I need to activate a conda environment in my makefile in order to run some python scripts, however, whenever I try to run conda activate env_name, I get the following message:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. If your shell is Bash or a Bourne variant, enable conda for the current user with

$ echo ". /Users/MY_USERNAME/anaconda3/etc/profile.d/conda.sh" >> ~/.bash_profile

or, for all users, enable conda with

$ sudo ln -s /Users/MY_USERNAME/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh

The options above will permanently enable the 'conda' command, but they do NOT put conda's base (root) environment on PATH. To do so, run

$ conda activate

in your terminal, or to put the base environment on PATH permanently, run

$ echo "conda activate" >> ~/.bash_profile

Previous to conda 4.4, the recommended way to activate conda was to modify PATH in your ~/.bash_profile file. You should manually remove the line that looks like

export PATH="/Users/MY_USERNAME/anaconda3/bin:$PATH"

^^^ The above line should NO LONGER be in your ~/.bash_profile file! ^^^

I've tried changing the shell for the makefile by adding SHELL := /bin/zsh at the top, but this doesn't fix the problem. Additionally, I need this makefile to be able to run using whatever the default shell is for the computer (some of my teammates use zsh, others use bash). It seems like no matter what I do, I can't get conda activate to work in the makefile.

What can I do to get it to work, or is this impossible?

like image 714
matt_js Avatar asked Nov 19 '18 20:11

matt_js


People also ask

How do I activate my conda?

To activate your Conda environment, type source activate <yourenvironmentname> . Note that conda activate will not work on Discovery with this version. To install a specific package, type conda install -n <yourenvironmentname> [package] . To deactivate the current, active Conda environment, type conda deactivate .

How do I run a conda in bash?

for anaconda 4 : bashrc and then copy the path into the file and save it after that you activate the changes using source . bashrc. When I type export PATH=~/anaconda3/bin:$PATH into the terminal and then run conda --version it works fine.

How can we create and activate conda environment?

Managing Python If you want to use a different version of Python, for example Python 3.5, simply create a new environment and specify the version of Python that you want. When conda asks if you want to proceed, type "y" and press Enter. Activate the new environment: Windows: conda activate snakes.

How do I activate a conda environment in my Bashrc?

you can simply add the anaconda bin folder (eg.: ~/anaconda3/bin ) to the system PATH and then source activate ENV_NAME in your ~/. bashrc or ~/.


2 Answers

After a bit of searching around, I came up with adding this pattern to my Makefile to make conda activate work. Others may be able to simplify.

# Need to specify bash in order for conda activate to work.
SHELL=/bin/bash
# Note that the extra activate is needed to ensure that the activate floats env to the front of PATH
CONDA_ACTIVATE=source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate ; conda activate

py3build:
    ($(CONDA_ACTIVATE) py3.6 ; python setup.py build )
like image 186
Traveler Avatar answered Oct 12 '22 00:10

Traveler


You should use .ONESHELL: directive at beginning of script. This run all in the same shell.

like image 41
Felipe Maza Avatar answered Oct 12 '22 00:10

Felipe Maza