如何导出virtualenv?

mno*_*tka 35 python django virtualenv

我是virtualenv的新手,但我正在编写django应用程序,最后我将不得不以某种方式部署它.

所以我们假设我的应用程序在我的本地virtualenv上工作,我安装了所有必需的库.我现在要做的是运行某种脚本,这将采用我的virtualenv,检查内部安装的内容并生成一个脚本,将所有这些库安装在其他机器上的新virtualenv上.怎么做到这一点?请帮忙.

Bib*_*ath 77

你不复制粘贴你的virtualenv.您导出所有安装的包的列表,如 -

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

然后将requirements.txt文件推送到您想要部署代码的任何位置,然后只需执行您在开发机器上所做的操作 -

$ virtualenv <env_name>
$ source <env_name>/bin/activate
(<env_name>)$ pip install -r path/to/requirements.txt
Run Code Online (Sandbox Code Playgroud)

在那里,你已经安装了所有的软件包和确切的版本.

您还可以使用以下功能查看Fabric以自动执行此任务 -

def pip_install():
    with cd(env.path):
        with prefix('source venv/bin/activate'):
            run('pip install -r requirements.txt')
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用虚拟环境进行比跟踪软件包版本更复杂的事情,那么这是完全不够的。例如,如果您对虚拟虚拟环境有不同的 python 路径,或者正在配置环境的其他方面。即使像使用 python 版本这样简单的事情也会被这种方法忽略! (2认同)