我可以使用单个 python 脚本来创建 virtualenv 并安装 requirements.txt 吗? [英] Can I use a single python script to create a virtualenv and install requirements.txt?

查看:40
本文介绍了我可以使用单个 python 脚本来创建 virtualenv 并安装 requirements.txt 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,如果尚未创建 virtualenv,我将在其中创建它,然后在其中安装 requirements.txt.

I am trying to create a script where i create a virtualenv if it has not been made, and then install requirements.txt in it.

我无法调用正常源/env/bin/activate并激活它,然后使用pip安装requirements.txt.有没有办法激活 virtualenv,然后从单个 python 脚本安装我的要求?

I can't call the normal source /env/bin/activate and activate it, then use pip to install requirements.txt. Is there a way to activate the virtualenv and then install my requirements from a single python script?

我现在的代码:

    if not os.path.exists(env_path):
        call(['virtualenv', env_path])

    else:
        print "INFO: %s exists." %(env_path)



    try:
        call(['source', os.path.join(env_path, 'bin', 'activate')])

    except Exception as e:
        print e

错误是没有这样的文件目录"

the error is "No such file directory"

谢谢

推荐答案

source 是 shell 内置命令,不是程序.它不能也不应该用 subprocess 执行.您可以通过在当前进程中执行 activate_this.py 来激活新的虚拟环境:

source is a shell builtin command, not a program. It cannot and shouldn't be executed with subprocess. You can activate your fresh virtual env by executing activate_this.py in the current process:

if not os.path.exists(env_path):
    call(['virtualenv', env_path])
    activate_this = os.path.join(env_path, 'bin', 'activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))

else:
    print "INFO: %s exists." %(env_path)

这篇关于我可以使用单个 python 脚本来创建 virtualenv 并安装 requirements.txt 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆