Python package managers and Node.js

霸气de小男生 提交于 2020-01-28 11:07:45

问题


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?


回答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.




回答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.)



来源:https://stackoverflow.com/questions/58456098/python-package-managers-and-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!