有人建议我尝试使用fabric将Django部署到生产服务器,并使用python而不是bash来自动化任务。
我想轻松启动,只需自动激活我的虚拟服务器,并在其中启动Django开发服务器。
我创建了一个名为fabfile.py的文件:
from fabric.api import local
def activate_env():
local("source /.../expofit_env/bin/activate")
def run_local_server():
local("/.../server/manage.py runserver")
def start():
activate_env()
run_local_server()但是,当我跑的时候
fab start我收到以下信息:
[localhost] local: source /.../expofit_env/bin/activate
/bin/sh: 1: source: not found
Fatal error: local() encountered an error (return code 127) while executin
'source /.../expofit_env/bin/activate'我做错了什么?
更新
根据Burhan Khalid的建议,我尝试了以下几点:
....
def activate_env():
local("/bin/bash /.../expofit_env/bin/activate")
....只跑
fab activate_env结果:
[localhost] local: /bin/bash /.../expofit_env/bin/activate
Done.但是,在执行之后,virtualenv没有被激活。关于下列代码:
def start_env():
with prefix('/bin/bash /.../expofit_env/bin/activate'):
local("yolk -l")我仍然会遇到一个错误,就像virtualenv没有被激活一样。
alan@linux ~/Desktop/expofit $ fab start_env
[localhost] local: yolk -l
/bin/sh: 1: yolk: not found当我手动激活virtualenv时,蛋黄工作正常:
alan@linux ~/.../expofit_env $ source bin/activate
(expofit_env)alan@linux ~/.../expofit_env $ yolk -l
DateUtils - 0.5.2 - active
Django - 1.4.1 - active
Python - 2.7.3rc2 - active development (/usr/lib/python2.7/lib-dynload)
....更新
尝试了this question的一种新方法。
from __future__ import with_statement
from fabric.api import *
from contextlib import contextmanager as _contextmanager
env.activate = 'source /.../expofit_env/bin/activate'
@_contextmanager
def virtualenv():
with prefix(env.activate):
yield
def deploy():
with virtualenv():
local('yolk -l')给出同样的错误:
[localhost] local: yolk -l
/bin/sh: 1: source: not found
Fatal error: local() encountered an error (return code 127) while executing 'yolk -l'
Aborting.即使是第一个命令传递时也没有错误:
alan@linux ~/.../expofit_env/bin $ fab virtualenv
[servername] Executing task 'virtualenv'
Done.更新
可以使用自定义shell运行local。
from fabric.api import local
def start_env():
local('source env/bin/activate',shell='/bin/bash')但是,这并没有激活虚拟服务器,就好像它是手动完成的一样。
发布于 2012-10-04 00:13:31
要使用fab文件中的虚拟服务器,您需要运行命令,如下所示:
def task():
# do some things outside the env if needed
with prefix('source bin/activate'):
# do some stuff inside the env
pip install django-audiofieldwith框中的所有命令都将在virtualenv中执行。
发布于 2012-10-03 11:20:17
默认情况下,您使用的是sh shell,而source命令是bashism (也就是说,只在bash中工作)。
要激活您的环境,需要直接使用bash执行它。/bin/bash /path/to/bin/activate。
https://stackoverflow.com/questions/12707220
复制相似问题