2

I am trying to compile python scripts using Node.js. The python scripts include some modules I have installed. My package manager for python is Anaconda, so I tried to supply the {"shell":"path to anaconda prompt"} option in :

var exec = require('child_process').exec;
exec('python hello.py',{"shell":"path to anaconda prompt"}, ..callback)

However, I get an error:

{ Error: spawn C:\Users\dream\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit)\Anaconda Prompt (Anaconda3) ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:232:19)
    at onErrorNT (internal/child_process.js:407:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall:
   'spawn C:\\Users\\dream\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Anaconda3 (64-bit)\\Anaconda Prompt (Anaconda3)',
  path:
   'C:\\Users\\dream\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Anaconda3 (64-bit)\\Anaconda Prompt (Anaconda3)',
  spawnargs: [ '/d', '/s', '/c', '"python hello.py"' ],
  cmd: 'python hello.py' }
stdout:

stderr:

I suspect that this is because the Anaconda Prompt is just some wierd shortcut that sets up some variables for cmd.exe (which is the location the shortcut points to).

So my questions:

Can I call the anaconda prompt directly with Node.js? Does pip also have a shell?

How do the packagers (pip,anaconda) make modules accessible to python? -> Is this through some environment variables?

Can I prepare cmd.exe for work with python the same way that they do?

thehorseisbrown
  • 390
  • 2
  • 14

2 Answers2

2

I don't think you want to call the Anaconda prompt.

Just call python: python print('hello').

What happens on the command line if you call: Anaconda Prompt (Anaconda3) print('hello')?

(This should be a comment, but I cannot comment.)

shanecandoit
  • 581
  • 1
  • 3
  • 11
  • I have several shells on my machine, and while they can compile 'python hello.py' they struggle with 'python script_that_uses_a_module_such_as_numpy.py'. Except for Anaconda Prompt..Furthermore, when I type 'import numpy as np;x=np.array([1]);print(x)' into python 3.7.exe, it doesn't know the module either... – thehorseisbrown Oct 18 '19 at 18:21
  • Then you have a problem with your environment. (Which is worse). Find out where all your pythons are on your system path and either 1. remove/hide them, or 2. install numpy for them. On unix I can run `whereis python` and get back a list of all python installs, this is a first start. – shanecandoit Oct 18 '19 at 18:38
  • 1
    Thank you, I removed all environment variables with python at the end, reinstalled anaconda, and cmd.exe actually notified me that I should 'activate the environment'... conda activate loads the modules... Now I need to figure out how to make the activation permanent – thehorseisbrown Oct 18 '19 at 19:26
  • I'm glad to see you are still making prgoress. Maybe this will help you further: https://stackoverflow.com/questions/20081338/how-to-activate-an-anaconda-environment – shanecandoit Oct 18 '19 at 19:33
1

I suspect that this is because the Anaconda Prompt is just some weird shortcut that sets up some variables for cmd.exe

Yes, that's pretty much it. So, no I don't think you can call it as proposed. There is probably a way to manipulate the cmd.exe manually to get it running like an Anaconda Prompt session, but instead I'd propose to try...

conda run

Not sure if this will work in Windows, but it might be possible to use conda run to execute within the Conda environment. This was introduced (and still remains) as an experimental feature in Conda v4.6, with the express purpose of enabling one to run something inside a Conda environment without interactively having to activate it.

Prerequisite

First, you should probably test that conda run works on Windows. Let's assume your conda.exe is located at

C:\Users\dream\Anaconda3\Scripts\conda.exe

Start a clean cmd.exe session, where conda is undefined (i.e., not Anaconda Prompt). Then try things like

C:\Users\dream\Anaconda3\Scripts\conda.exe run where python

or, if you have another env, say my_env you can also do

C:\Users\dream\Anaconda3\Scripts\conda.exe run -n my_env where python

to verify that the Python interpreter that gets run is the one specified.

(Possible) Solution

If the above works, then you should be able to do something like

var exec = require('child_process').exec;
exec('C:\Users\dream\Anaconda3\Scripts\conda.exe run python hello.py', ..callback)

Not sure if you'll need the shell specified in this case.

merv
  • 67,214
  • 13
  • 180
  • 245
  • 1
    Thank you, `conda.exe run python hello.py` works! I also figured that I can do: `exec("conda activate & python hello.py",...)` which looks similar. Thank you.. Do you think that activating the environment is a big overhead? – thehorseisbrown Oct 18 '19 at 22:01
  • @thehorseisbrown `conda.exe run python` is going to activate as well, but I presume it cuts out any unnecessary steps that have to do with interactivity. It may be helpful to create a minimal env that only includes the packages you require to run `hello.py`. I observe noticeable differences in the activation times across my different envs - not sure how a base Anaconda would compare (I use Miniconda). – merv Oct 18 '19 at 22:23