我正在使用virtualenvwrapper为我的Django部署使用virtualenv.
以下是我的Fabric任务:
proj_path = '/path/to/proj'
def setup_code():
sudo('pip install virtualenvwrapper')
run('export WORKON_HOME=$HOME/.virtualenvs')
run('source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv myenv')
run('source /usr/local/bin/virtualenvwrapper.sh && workon myenv')
cd(proj_path)
req_file = os.path.join(proj_path, 'requirements.txt')
run('pip install -r %s' % req_file)
我执行了上面的fab任务,但它表现得很奇怪. pip开始检索所有包,然后开始为它们执行安装文件.在执行安装文件时,它崩溃说Permission denied.
但为什么?它在〜和virtualenv里面工作.
我究竟做错了什么?
解决方法:
弄清楚问题:
对于面料:
cd('dir') # doesn't works.
以下作品:
with cd('dir'):
print('pwd') # Directory change reflects here.
同样,其他环境因素如:
run('export WORKON_HOME=$HOME/.virtualenvs')
run('source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv myenv')
run('source /usr/local/bin/virtualenvwrapper.sh && workon myenv')
但改为:
with prefix('WORKON_HOME=$HOME/.virtualenvs'):
with prefix('source /usr/local/bin/virtualenvwrapper.sh'):
with prefix('workon myenv'): # Assuming there is a env called `myenv`
run('pip install -r requirements.txt') # Works in virtualenv
从官方文档中找出它:http://docs.fabfile.org/en/stable/api/core/context_managers.html