首页 文章

从makefile激活Anaconda Python环境

提问于
浏览
7

我想使用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

但是,如果我尝试这个,我会收到以下错误

make:source:找不到命令make:*** [source]错误127

我已经搜索了一个解决方案,但是[这个问题/答案(How to source a script in a Makefile?)表明我不能在makefile中使用 source .

然而,This answer提出了一个解决方案(并收到了几个赞成票)但这对我来说也不起作用

(\ source / home / vagrant / miniconda / bin / activate myproject; \)/ bin / sh:2:source:not found make:*** [source] Error 127

我还尝试将 source activate 步骤移动到单独的bash脚本,并从makefile执行该脚本 . 这不起作用,我假设出于类似的原因,即我在shell中运行 source .

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

3 回答

  • 1

    你应该使用它,它现在对我有用 .

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

    我有类似的问题;我想从Makefile创建或更新conda环境,以确保我自己的脚本可以使用来自该conda环境的python .
    默认情况下,使用sh来执行命令,而sh不知道源(也见this 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
    

    希望能帮助到你

  • 6

    我有同样的问题 . 基本上唯一的解决方案由9000声明 . 我有一个安装shell脚本,我在其中设置conda环境(源激活python2),然后我调用make命令 . 我尝试从Makefile内部设置环境,但没有成功 .

    我在makefile中有这一行:

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

    错误消息是:

    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'
    

    从本质上讲,我能够设置我的conda环境以使用我具有写访问权限的本地conda . 但制作过程并没有提到这一点 . 我不明白为什么在make进程中看不到使用'source'在我的shell脚本中设置的环境; source命令应该更改当前shell . 我只想分享这个,以便其他人不会浪费时间尝试这样做 . 我知道autotoools有一种使用python的方法 . 但制作计划在这方面可能有限 .

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

    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
    

    这似乎对我有用 .

    有类似的情况solution,但似乎对我不起作用:

    我修改过的Makefile段:

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

    调用make后出现错误信息:

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

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

相关问题