Python 包管理器和 Node.js

问题描述

我正在尝试使用 Node.js 编译 python 脚本.python 脚本包括我安装的一些模块.我的 python 包管理器是 Anaconda,所以我尝试提供 {"shell":"path to anaconda prompt"} 选项:

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:UsersdreamAppDataRoamingMicrosoftWindowsStart MenuProgramsAnaconda3 (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:

我怀疑这是因为 Anaconda Prompt 只是一些奇怪的快捷方式,它为 cmd.exe 设置了一些变量(这是快捷方式指向的位置).

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

所以我的问题:

我可以直接用 Node.js 调用 anaconda 提示符吗?pip 也有壳吗?

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

打包程序(pip、anaconda)如何使 python 可以访问模块?-> 这是通过一些环境变量吗?

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

我可以像他们一样准备 cmd.exe 来使用 python 吗?

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


解决方案

我怀疑这是因为 Anaconda Prompt 只是一些奇怪的快捷方式,它为 cmd.exe 设置了一些变量

是的,差不多就是这样.所以,不,我不认为你可以按照建议来称呼它.可能有一种方法可以手动操作 cmd.exe 以使其像 Anaconda Prompt 会话一样运行,但我建议尝试...

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

不确定这是否适用于 Windows,但可以使用 conda run 在 Conda 环境中执行.这是在 Conda v4.6 中作为实验性功能引入(并且仍然保留)的,其明确目的是使人们能够在 Conda 环境中运行某些东西而无需以交互方式激活它.

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.

首先,您可能应该测试 conda run 在 Windows 上的工作.假设您的 conda.exe 位于

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

C:UsersdreamAnaconda3Scriptsconda.exe

启动一个干净的 cmd.exe 会话,其中 conda 未定义(即不是 Anaconda Prompt).然后尝试类似

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

C:UsersdreamAnaconda3Scriptsconda.exe run where python

或者,如果你有另一个环境,说 my_env 你也可以这样做

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

C:UsersdreamAnaconda3Scriptsconda.exe run -n my_env where python

验证运行的 Python 解释器是指定的.

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

如果上述方法有效,那么您应该能够执行类似的操作

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

var exec = require('child_process').exec;
exec('C:UsersdreamAnaconda3Scriptsconda.exe run python hello.py', ..callback)

不确定在这种情况下是否需要指定的 shell.

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

相关文章