1

I'm invoking a python script from nodejs, using spawn.

The python script is installed in a conda environment, and my VS code project is in nodejs.

When I want to debug the nodejs code, it needs to call the python script as if it is in the conda environment. How do I do that?

Currently, it errs "Python was not found but can be installed from the Microsoft Store"

David Niki
  • 1,092
  • 1
  • 11
  • 14

2 Answers2

3

When you are calling the script from Nodejs I think you are using the default python installation. Something like this:

const spawn = require("child_process").spawn;
const pythonProcess = spawn('python',["path/to/script.py", arg1, arg2, ...]);

In order to use a particular conda env. Find the path of the python executable of that conda environment and use that python executable to call the script like this (click here to see how to find python executable path for conda environment):

const spawn = require("child_process").spawn;
const pythonProcess = spawn('/full/path/to/example-env/bin/python3',["path/to/script.py", arg1, arg2, ...]);
Yogeshwar Singh
  • 1,245
  • 1
  • 10
  • 22
  • Thanks but I did not mean a python installation, rather a conda environment (one you active with "conda activate [env_name]") – David Niki Dec 23 '19 at 14:21
  • Sorry I totally overlooked that you are talking about conda environment, in my head I was thinking of virtualenv. You can find the full path of the python installation that your conda environment in using and use that to spwan the process. You can see the steps to find the path here https://stackoverflow.com/a/47277111/2313097 – Yogeshwar Singh Dec 23 '19 at 14:35
  • Thank you - it worked! Can you please update your answer (add the link there) so I can accept it? – David Niki Dec 24 '19 at 15:31
  • @Yogeshwar It still somehow doesn't work for me. The modules still cannot be found. It just runs the python executable from that path but does not activate the environment. Similar to when you yourself would go to that path and open the python prompt, then it says Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation I don't know how to solve it. Can you please help. – ashawe Mar 26 '21 at 07:03
  • @ashawe try setting the correct PATH (as mentioned in the link that you have given) in `process.env`. Because that is passed as env variables to the spawn() process. So theoretically the spawned process should search for path in that env and should find and will not give you this warning. It's been a long time and I do not have Python setup on my PC right now so can't try it myself but hope this may help. – Yogeshwar Singh Mar 26 '21 at 10:50
0

It seems path problem. Similar to this one.

Check your paths in the environment variable settings. Had the same issue. The order of your path entries is hierarchical. So if the winapps directory is listed first, it will send you to the app store. Move your python install directory, bin, and lib-scripts to the top

https://www.reddit.com/r/vscode/comments/duxqtq/python_was_not_found_but_can_be_installed_from/

shimo
  • 2,156
  • 4
  • 17
  • 21