如何使用makefile激活virtualenv? [英] How to activate a virtualenv using a makefile?

查看:165
本文介绍了如何使用makefile激活virtualenv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的makefile文件的顶部,我有这行:

At the top of my makefile I have this line:

SHELL := /bin/sh

大多数命令都需要.但是,我还希望有一个make命令来激活虚拟环境,该环境位于不同的路径上.

which is needed for most of the commands. However, I would like to also have a make command to activate my virtual env, which is on a different path.

这是我为此编写的代码:

Here is the code that I wrote for it:

activate:
    source ~/.envs/$(APP)/bin/activate; \

问题是,这只会打印出此处编写的内容,而不会执行.我读到它可能与bash知道源代码有关,但是我不知道如何在activate命令中临时切换模式.

The problem with this is, that this just prints out what is written here, and it doesn't get executed. I read that it might have something todo with only bash knowing about source, but I can't figure out how to temporarily switch modes within the activate command.

我将如何编写此方法,以便激活我的virtualenv?

How would I have to write this method, so that it activates my virtualenv?

推荐答案

它确实得到执行.

Virtualenv通过修改当前进程的环境来工作(这就是为什么您必须提供"它的原因).但是,一个进程不能修改另一进程的环境.因此,要运行您的配方,make会调用一个外壳程序并将其传递给您的virtualenv命令,它会起作用,然后外壳程序退出,您的virtualenv消失了.

Virtualenv works by modifying your current process's environment (that's why you have to "source" it). However, one process cannot modify the environment of the other process. So, to run your recipe make invokes a shell and passes it your virtualenv command, it works, then the shell exits, and your virtualenv is gone.

简而言之,在makefile中没有简单的方法可以做到这一点.最简单的操作是创建一个脚本,该脚本首先获取virtualenv的资源,然后运行make,然后运行该脚本,而不是运行make.

In short, there's no easy way to do this in a makefile. The simplest thing to do is create a script that first sources the virtualenv then runs make, and run that instead of running make.

这篇关于如何使用makefile激活virtualenv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆