2

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.

Brady Dowling
  • 4,920
  • 3
  • 32
  • 62
  • not too familiar with node, but it looks like you're basically running command line. Can you run something like `proc.execFile("workon", {args:""})`. I guess this assumes you have virtualenvwrapper. – it's-yer-boy-chet Feb 22 '18 at 18:50
  • 1
    The answer is the same as in the question you've pointed to — you don't need to activate virtual env to run `pip` in it — you just use `python` from the virtual env with full path. – phd Feb 22 '18 at 21:04
  • Of course! Go ahead and make this into a full answer and I'll accept it. – Brady Dowling Feb 23 '18 at 19:39

1 Answers1

4

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
});
Brady Dowling
  • 4,920
  • 3
  • 32
  • 62
  • How would you actiavte a virtual environment in generel? – J. Doe Sep 12 '19 at 18:22
  • @J.Doe typically you'd run something like `source virtualenv/bin` assuming your virtualenvironment is called `virtualenv` – Brady Dowling Sep 12 '19 at 20:59
  • I do run this command, but without success. I'm asking because I have exact this problem : https://stackoverflow.com/questions/57875421/activating-an-virtual-environment-from-python-in-nodejs – J. Doe Sep 13 '19 at 05:36