简体   繁体   English

从makefile激活Anaconda Python环境

[英]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 我想使用makefile来使用makefile和anaconda / miniconda构建我的项目环境,所以我应该能够克隆repo并简单地运行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:找不到命令

make: *** [source] Error 127 make:*** [来源]错误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. 我已经搜索了一个解决方案,但是[这个问题/答案( 如何在Makefile中获取脚本? )表明我不能在makefile中使用source

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; source / home / vagrant / miniconda / bin / activate myproject; \\ \\

)

/bin/sh: 2: source: not found / bin / sh:2:source:not found

make: *** [source] Error 127 make:*** [来源]错误127

I also tried moving the source activate step to a separate bash script, and executing that script from the makefile. 我还尝试将source activate步骤移动到单独的bash脚本,并从makefile执行该脚本。 That doesn't work, and I assume for the a similar reason, ie I am running source from within a shell. 这不起作用,我假设出于类似的原因,即我在shell中运行source

I should add that if I run source activate myproject from the terminal, it works correctly. 我应该补充说,如果我从终端运行source activate myproject ,它可以正常工作。

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. 我想从Makefile创建或更新conda环境,以确保我自己的脚本可以使用来自该conda环境的python。
By default make uses sh to execute commands, and sh doesn't know source (also see this SO answer ). 默认情况下,使用sh来执行命令,而sh不知道 (也参见此SO答案 )。 I simply set the SHELL to bash and ended up with (relevant part only): 我只是将SHELL设置为bash并最终得到(仅限相关部分):

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 希望能帮助到你

You should use this, it's functional for me at moment. 你应该使用它,它现在对我有用。

report.ipynb : merged.ipynb
    ( bash -c "source ${HOME}/anaconda3/bin/activate py27; which -a python; \
        jupyter nbconvert \
        --to notebook \
        --ExecutePreprocessor.kernel_name=python2 \
        --ExecutePreprocessor.timeout=3000 \
        --execute merged.ipynb \
        --output=$< $<" )

I had the same problem. 我有同样的问题。 Essentially the only solution is stated by 9000. I have a setup shell script inside which I setup the conda environment (source activate python2), then I call the make command. 基本上唯一的解决方案由9000声明。我有一个安装shell脚本,我在其中设置conda环境(源激活python2),然后我调用make命令。 I experimented with setting up the environment from inside Makefile and no success. 我尝试从Makefile内部设置环境,但没有成功。

I have this line in my makefile: 我在makefile中有这一行:

installpy :
   ./setuppython2.sh && python setup.py install

The error messages is: 错误消息是:

make
./setuppython2.sh && python setup.py install
running install
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/test-easy-install-29183.write-test'

Essentially, I was able to set up my conda environment to use my local conda that I have write access. 从本质上讲,我能够设置我的conda环境以使用我具有写访问权限的本地conda。 But this is not picked up by the make process. 但制作过程并没有提到这一点。 I don't understand why the environment set up in my shell script using 'source' is not visible in the make process; 我不明白为什么在make进程中看不到使用'source'在我的shell脚本中设置的环境; the source command is supposed to change the current shell. source命令应该更改当前shell。 I just want to share this so that other people don't wast time trying to do this. 我只想分享这个,以便其他人不会浪费时间尝试这样做。 I know autotoools has a way of working with python. 我知道autotoools有一种使用python的方法。 But the make program is probably limited in this respect. 但制作计划在这方面可能有限。

My current solution is a shell script: 我目前的解决方案是shell脚本:

cat py2make.sh cat py2make.sh

#!/bin/sh

# the prefix should be change to the target
# of installation or pwd of the build system
PREFIX=/some/path
CONDA_HOME=$PREFIX/anaconda3
PATH=$CONDA_HOME/bin:$PATH
unset PYTHONPATH
export PREFIX CONDA_HOME PATH
source activate python2
make

This seems to work well for me. 这似乎对我有用。

There were a solution for similar situation but it does not seems to work for me: 有类似情况的解决方案 ,但它似乎不适合我:

My modified Makefile segment: 我修改过的Makefile段:

installpy :
   ( source activate python2; python setup.py install )

Error message after invoking make: 调用make后出现错误信息:

make
( source activate python2; python setup.py install )
/bin/sh: line 0: source: activate: file not found
make: *** [installpy] Error 1

Not sure where am I wrong. 不知道我哪里错了。 If anyone has a better solution please share it. 如果有人有更好的解决方案,请分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM