在NodeJS中运行Conda命令 [英] Running Conda commands in NodeJS

查看:82
本文介绍了在NodeJS中运行Conda命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在NodeJS应用中使用 exec 运行Conda命令.

I can't run a Conda command using exec with my NodeJS app.

var conda_path = '~/miniconda3/bin/conda'

var cmd = conda_path + ' init bash & ' + conda_path + ' activate XYZ'
exec(command,
    function(error, stdout, stderr){

    }
);

我收到以下错误:

/bin/sh:/Users/username/Desktop/repos/project/XYZ:是目录

/bin/sh: /Users/username/Desktop/repos/project/XYZ: is a directory

CommandNotFoundError:您的外壳尚未正确配置为使用"conda激活".要初始化外壳,请运行

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run

$ conda init <SHELL_NAME>

当前受支持的shell是:-重击- 鱼-tcsh-xonsh-zsh-Powershell

Currently supported shells are: - bash - fish - tcsh - xonsh - zsh - powershell

有关更多信息和选项,请参见"conda init --help".

See 'conda init --help' for more information and options.

重要提示:您可能需要在运行后关闭并重新启动Shell'conda init'.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

我在网上找不到有关从NodeJS应用程序运行conda命令的相关信息.

I can find no related information online regarding running conda commands from NodeJS app.

我该如何进行这项工作?

How can I make this work?

推荐答案

t似乎可以使用& 字符来运行多个命令.

t seems to work using the & character to run multiple commands.

我还需要指定 conda.sh 的路径,该路径位于/users/username/miniconda ??/etc/profile.d/中.

I also needed to specify the path to conda.sh which resides inside /users/username/miniconda??/etc/profile.d/.

此文件可以复制到任何位置,例如您的NodeJS应用程序的根文件夹.

This file can be copied to any location, e.g your NodeJS app's root folder.

这是工作代码:

Windows 10

var commands = [
        'C:\\anaconda\\Scripts\\activate.bat C:\\anaconda',
        'conda activate XYZ_v0_1_env',
        'XYZ arg1  <some_file_input> arg2 arg3 arg4 <some_file_output>',
    ]

exec(commands.join(' & '),
    function(error, stdout, stderr){
        console.log(error)
        console.log(stdout)
        console.log(stderr)
    }
);

OSX

const exec = require('child_process').exec;

var conda_path = __dirname + '/conda.sh'

var commands = [
        conda_path,
        conda_path + ' init',
        conda_path + ' activate XYZ_v0_1_env',
        'XYZ arg1  <some_file_input> arg2 arg3 arg4 <some_file_output>',
    ]

exec(commands.join(' & '),
    function(error, stdout, stderr){
        console.log(error)
        console.log(stdout)
        console.log(stderr)
    }
);


使用 spawn 而不是 exec 来读取流程的实时输出,即生成实时进度:


Using spawn instead of exec to read the live output of the process, i.e to generate live progress:

var commands = [
        'C:\\anaconda\\Scripts\\activate.bat C:\\anaconda',
        'conda activate XYZ_v0_1_env',
        'XYZ arg1  <some_file_input> arg2 arg3 arg4 <some_file_output>',
    ]

var spawn_ = spawn(commands.join('&'), { shell: true });

spawn_.stdout.on('data', function (data) {
    //do something
});

spawn_.stderr.on('data', function (data) {
    //do something
});

spawn_.on('exit', function (code) {
    //do something
});

这篇关于在NodeJS中运行Conda命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆