bash 从 makefile 激活 Anaconda Python 环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38878088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:02:01  来源:igfitidea点击:

Activate Anaconda Python environment from makefile

pythonbashmakefilevirtualenvanaconda

提问by Philip O'Brien

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] Error 127

制作:来源:找不到命令

制作:*** [来源] 错误 127

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

我已经搜索了一个解决方案,但是 [这个问题/答案(如何在 Makefile 中获取脚本?)表明我不能source在 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

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

)

/bin/sh: 2: 来源:未找到

制作:*** [来源] 错误 127

I also tried moving the source activatestep 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 sourcefrom within a shell.

我还尝试将source activate步骤移至单独的 bash 脚本,并从 makefile 执行该脚本。这是行不通的,我假设出于类似的原因,即我source在 shell 中运行。

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

我应该补充一点,如果我source activate myproject从终端运行,它可以正常工作。

回答by Ludo

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 shto execute commands, and shdoesn't know source(also see this SO answer). I simply set the SHELL to bash and ended up with (relevant part only):

我有一个类似的问题;我想从 Makefile 创建或更新 conda 环境,以确保我自己的脚本可以使用该 conda 环境中的 python。
默认情况下,make 使用sh来执行命令,而sh不知道来源(另请参阅此 SO answer)。我只是将 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

希望能帮助到你

回答by William Trigos

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=$< $<" )

回答by Kemin Zhou

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. I experimented with setting up the environment from inside Makefile and no success.

我有同样的问题。基本上唯一的解决方案是 9000。我有一个设置 shell 脚本,我在其中设置了 conda 环境(source activate python2),然后我调用了 make 命令。我尝试从 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. 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; the source command is supposed to change the current 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. But the make program is probably limited in this respect.

从本质上讲,我能够设置我的 conda 环境以使用我有写访问权限的本地 conda。但这并没有被 make 过程所接受。我不明白为什么在我的 shell 脚本中使用 'source' 设置的环境在 make 过程中不可见;source 命令应该更改当前的 shell。我只是想分享这个,这样其他人就不会浪费时间尝试这样做了。我知道 autotools 有一种使用 python 的方法。但是 make 程序可能在这方面受到限制。

My current solution is a shell script:

我目前的解决方案是一个 shell 脚本:

cat py2make.sh

猫 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 solutionfor 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.

不知道我哪里错了。如果有人有更好的解决方案,请分享。