结构全局设置环境变量 [英] Fabric set environment variable globally
问题描述
我正在编写一个fabfile,我想为我用于开发的virtualenv包括一个环境变量.我不想放
I am writing a fabfile that I want to include an environment variable for the virtualenv that I'm using for development. I don't want to have to put
with shell_env(venv=VIRTUALENV):
...
每个函数中的
.我希望能够在文件的开头添加此代码,并将其应用于全局所有任务.有办法吗?
in every single function. I'd like to be able to add this at the beginning of the file and have it apply to all tasks globally. Is there a way to do this?
例如,我有任务
def setup_dev_env():
with shell_env(venv=VIRTUALENV):
local('virtualenv $(venv)')
with prefix('workon $(venv)'):
local('pip install -r requirements.txt')
test()
def test():
with shell_env(venv=VIRTUALENV):
with prefix('workon $(vnenv)'):
local('python3 manage.py test')
如果我不必在两个函数的开头重复相同的行,那将是很好的选择.
and it would be nice if I didn't have to repeat exactly the same line at the beginning of both functions.
推荐答案
I'm not sure if this is considered a hack of command_prefixes
but it works:
env.command_prefixes=["export myvar='Hello world'",]
def echo_env():
run(r"echo $myvar")
输出:
C:\Users\swozn\PycharmProjects\aaetuea>fab echo_env
[swozn@localhost] Executing task 'echo_env'
[swozn@localhost] run: echo $myvar
[swozn@localhost] out: Hello world
优点是无法忘记,因为它会自动添加到所有命令的前面.
The advantage is that it is impossible to forget, because it is automatically prepended to all your commands.
这篇关于结构全局设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!