【问题标题】:How to call workon or deactivate virtualenvwrapper commands from a Makefile [duplicate]如何从 Makefile 调用 workon 或停用 virtualenvwrapper 命令 [重复]
【发布时间】:2021-02-10 14:45:22
【问题描述】:

我的 virtualenv 在 bash 中完美运行:

$ workon mylovelyenv
// do something
$ deactivate

当我尝试使用 makefile 进行自动化时,我无法做到:

$ cat makefile
all:
    workon mylovelyenv           #doesn't work
    $(shell workon mylovelyenv)  #doesn't work either
    bash -c "workon mylovelyenv" #doesn't work either
    python go.py
    $(shell deactivate) # same here ...

【问题讨论】:

    标签: bash makefile virtualenv virtualenvwrapper


    【解决方案1】:

    make 开箱即用,在单独的 shell 中运行配方中的每一行。这意味着像

    all:
        cd there
        pwd
    

    不要做你可能天真期望的事——cd 发生在一个 shell 实例中,然后退出,然后pwd 在一个新的 shell 中运行,该 shell 重新开始在 make 的当前工作目录中过程。

    同样,workon 和朋友必须在与依赖它的后续命令相同的 shell 实例中运行。在你的情况下,可能

    all:
        workon mylovelyenv; \
        python go.py
    

    或(在 GNU Make 3.82 或更新版本中)

    .ONESHELL:
    all:
        workon mylovelyenv
        python go.py
    

    没有必要明确地deactivate,因为当子shell退出时,无论如何都会发生这种情况。

    【讨论】:

    • 对我不起作用 - 我得到了 /bin/sh: 3: workon: not found,两种方法都试过了,我得到了 4.1 ...有什么建议吗?
    • 听起来你正在运行make 在一个你没有你在交互式shell 中的所有定义的地方。 workon 无论如何都是为了交互式使用。如果virtualenvwrapper(我猜它提供了workon)与常规Python virtualenv 的工作方式相同,您可以使用mylovelyenv/bin/python 来运行virtualenv 的Python,就好像您已经激活了环境一样。
    • Arrrrgggh ...我将把makefile包装在一个bash脚本中...
    猜你喜欢
    • 2014-12-26
    • 2017-07-22
    • 2017-01-21
    • 2017-01-07
    • 2013-08-23
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多