python - fabric 没有获取 Django 环境

标签 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: 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/

相关文章:

Python:均匀分布具有不同字符串长度的输出数据

python - lxml - 具有多个命名空间的 XML 文档

python - 行李箱工作标签没有吗? ---导入错误: No module named 2. 1.2

python - Qt 在 OSX 中与 skimage 相关的问题

python - AttributeError: 'builtin_function_or_method' 对象没有属性 'replace'

python - Django 权限组

python - 如何修复 Django 中的 "abort"问题?

linux - 在 Bash 脚本中嵌入密码

python - 在 Python 中捕获 netcat shell 命令输出

linux - 无法启动 Apache Web 服务器