Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate Anaconda Python environment from makefile

I want to use a makefile to build my project's environment using a makefile and anaconda/miniconda, so I should be able to clone the repo and simply run make myproject

myproject: build

build:
  @printf "\nBuilding Python Environment\n"
  @conda env create --quiet --force --file environment.yml
  @source /home/vagrant/miniconda/bin/activate myproject

If I try this, however, I get the following error

make: source: Command not found

make: *** [source] Error 127

I have searched for a solution, but [this question/answer(How to source a script in a Makefile?) suggests that I cannot use source from within a makefile.

This answer, however, proposes a solution (and received several upvotes) but this doesn't work for me either

( \
source /home/vagrant/miniconda/bin/activate myproject; \

)

/bin/sh: 2: source: not found

make: *** [source] Error 127

I also tried moving the source activate step to a separate bash script, and executing that script from the makefile. That doesn't work, and I assume for the a similar reason, i.e. I am running source from within a shell.

I should add that if I run source activate myproject from the terminal, it works correctly.

like image 367
Philip O'Brien Avatar asked Aug 10 '16 15:08

Philip O'Brien


People also ask

How can I activate conda environment automatically?

Reload Window from Command Palette, select base:conda as python interpreter then press Ctrl+Shift+` to open a new integrated Terminal, conda environment should be activated automatically in it.

How do I enable conda in Python?

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 .


1 Answers

I had a similar problem; I wanted to create, or update, a conda environment from a Makefile to be sure my own scripts could use the python from that conda environment.
By default make uses sh to execute commands, and sh doesn't know source (also see this SO answer). I simply set the SHELL to bash and ended up with (relevant part only):

SHELL=/bin/bash
CONDAROOT = /my/path/to/miniconda2
.
.
install: sometarget
        source $(CONDAROOT)/bin/activate && conda env create -p conda -f environment.yml && source deactivate

Hope it helps

like image 60
Ludo Avatar answered Sep 18 '22 15:09

Ludo