面料没有得到Django环境

时间:2016-01-13 15:01:48

标签: python django linux virtualenv fabric

我在本地系统中有一个结构文件,我用它在远程服务器(在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:没有名为django.core.management的模块

在谷歌进行一些搜索后,我发现它实际上并没有找到我的django环境 我的虚拟环境路径是/tmp/testapp/app1
源代码在/tmp/testapp/app1/source/app1/中 当我在服务器终端中运行命令python manage.py test时,它工作正常。如何通过面料测试?

1 个答案:

答案 0 :(得分:0)

我从以下链接中得到了答案

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')