从 Node 脚本激活 Python virtualenv [英] Activate a Python virtualenv from a Node script

查看:34
本文介绍了从 Node 脚本激活 Python virtualenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Node 脚本,我在其中使用 pip 安装 Python 模块.我想激活一个 virtualenv,然后在该 virtualenv 中安装 pip 模块.我可以执行以下操作:

<前>proc.execFile("virtualenv", { args: "venv" }, function() {proc.execFile("source", { args: "venv/bin/activate" }, function() {proc.execFile("pip", { args: ["install", "myPipModule"]}, function() {//做东西});});});

这样做的问题是它会丢失我的 virtualenv 的上下文,因此不会在我想要的地方安装模块.如何在 Node 脚本中保留 virtualenv 的上下文,以便 pip install 将模块放在正确的位置?

<小时>

注意:类似于 Python 的这个问题 但我使用的是 Node.

解决方案

您无需激活虚拟环境即可在其中运行 pip,您只需使用 的路径pip 二进制文件在 virtualenv 中,它将安装在该 virtualenv 中.

proc.execFile("myapp/venv/bin/pip", { args: ["install", "myPipModule"]}, function() {//做东西});

I'm writing a Node script where I install Python modules using pip. I'd like to activate a virtualenv and then install pip modules in that virtualenv. I could do something like the following:

proc.execFile("virtualenv", { args: "venv" }, function() {
    proc.execFile("source", { args: "venv/bin/activate" }, function() {
        proc.execFile("pip", { args: ["install", "myPipModule"]}, function() {
                                // do stuff
        });
    });
});

The issue with this is that it would lose the context of my virtualenv and so wouldn't install the modules where I want them. How can I keep the context of my virtualenv in my Node script so pip install puts modules in the right place?


Note: Similar to this question for Python but I'm using Node.

解决方案

You don't need to activate a virtual env to run pip in it, you just use the path of the pip binary in the virtualenv and it will install it within that virtualenv.

proc.execFile("myapp/venv/bin/pip", { args: ["install", "myPipModule"]}, function() {
    // do stuff
});

这篇关于从 Node 脚本激活 Python virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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