我的本地系统中有一个结构文件,我用它在远程服务器(在 virtualenv 中)部署代码并想测试它。方法定义如下。
def test_deploy(gitid = '930bfc4'):
# manually put the commit id at the end
path = '/tmp/testapp/app1/source/demo4'
with cd(path):
commit_id = 'git fetch https://myaccount@github.org/myaccount/demo4.git/heads/master:https://myaccount@github.org/myaccount/demo4.git/remotes/origin/master/'+gitid
# change the settings.py file and update the database
run (commit_id)
run ('cat .git/FETCH_HEAD')
path1 = '/tmp/testvehic/vehic'
with cd(path1):
run ('pwd')
env.activate = 'source /tmp/testapp/app1/bin/activate'
run ('python /tmp/testapp/app1/source/demo4/manage.py test')`
表明
ImportError: No module named django.core.management
在谷歌搜索后,我才知道它实际上并没有定位我的 django 环境。
我的虚拟环境路径是/tmp/testapp/app1
源代码在/tmp/testapp/app1/source/app1/
当我在服务器终端运行命令 python manage.py test
时,它工作正常。我如何通过织物进行测试?
最佳答案
我从下面的链接得到了答案
http://nurupoga.org/articles/deployment-with-fabric-and-virtualenv/
我的代码片段现在看起来像这样
VENV_DIR = '/home/rootuser/testvehic/vehic'
PROJECT_DIR = '/home/rootuser/testvehic/vehic/source/cabsdemo4'
@contextmanager
def source_virtualenv():
with prefix('source ' + os.path.join(VENV_DIR, 'bin/activate')):
yield
def test_deploy(gitid = '930bfc4'):
# manually put the commit id at the end
path = '/tmp/testapp/app1/source/demo4'
with cd(path):
commit_id = 'git fetch https://myaccount@github.org/myaccount/demo4.git/heads/master:https://myaccount@github.org/myaccount/demo4.git/remotes/origin/master/'+gitid
# change the settings.py file and update the database
run (commit_id)
run ('cat .git/FETCH_HEAD')
with settings(warn_only=True):
with cd(PROJECT_DIR):
with source_virtualenv():
run('pip install -r requirements.txt')
run('python manage.py test')
关于python - fabric 没有获取 Django 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769974/