問題描述
我正在嘗試使用 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.
這篇關于Python 包管理器和 Node.js的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!