我正在构建一个Electron
应用程序(运行Angular应用程序),该应用程序充当python
下面程序的用户界面。
该python
程序anaconda
用于程序包管理(我正在miniconda
用于开发)。
应用启动时,它将检查所需的conda
环境是否存在,如果不存在,则创建它。
以下代码是的一部分,Service
它负责管理python
程序。
public doEnvironmentSetup() {
let stdOutSub = new Subject<string>();
let stdErrSub = new Subject<string>();
let completeSubject = new Subject<string>();
this.runningSetup = true;
const TEMP_ENV_FILE = join(tmpdir(), "env.yml");
return Promise.resolve()
.then(() => {
// Copy packaged environment.yml to TEMP_ENV_FILE
})
.then(() => this.electron.getApplicationStoragePath())
.then((stor) => {
setTimeout(() => {
let runProcess = this.electron.childProcess.spawn("conda", ["env", "create", "--file", TEMP_ENV_FILE, "--name", CONDA_ENV_NAME], {
cwd: stor
});
const stdOutReaderInterface = createInterface(runProcess.stdout);
const stdErrReaderInterface = createInterface(runProcess.stderr);
stdOutReaderInterface.on('line', (line) => {
stdOutSub.next(line);
});
stdErrReaderInterface.on('line', (line) => {
stdErrSub.next(line);
});
runProcess.on('close', (code: number) => {
this.electron.fs.unlinkSync(TEMP_ENV_FILE);
this.runningSetup = false;
completeSubject.next("");
});
}, 2000);
return {
stdOut: stdOutSub,
stdErr: stdErrSub,
onComplete: completeSubject
};
});
}
现在,当我需要运行实际的python
代码时,运行的代码是(不给出全部内容,因为对于我们这里的目的来说太长了):
execCmd.push(
`conda init ${this.electron.os.platform() === "win32" ? "powershell" : "bash"}`,
`conda activate ${CONDA_ENV_NAME}`,
// long python spawn command
`conda deactivate`,
)
setTimeout(() => {
logLineSubject.next({ out: "--- Setting up Execution Environment ---", err: "" });
logLineSubject.next({ out: `Running in ${dir}`, err: "" });
const cmd = execCmd.join(" && ");
let runProcess = this.electron.childProcess.spawn(cmd, {
detached: false,
windowsHide: true,
cwd: cwd,
shell: this.getShell()
});
const stdOutInterface = createInterface(runProcess.stdout);
const stdErrInterface = createInterface(runProcess.stderr);
stdOutInterface.on('line', (line) => {
// get this line back to the component
});
stdErrInterface.on('line', (line) => {
// get this line back to the component
});
runProcess.on("error", (err) => {
// get this back to the component
});
runProcess.on('close', (code: number) => {
// get this line back to the component
});
}, 1000);
其中getShell
定义为:
private getShell() {
return process.env[this.electron.os.platform() === "win32" ? "COMSPEC" : "SHELL"];
}
但是,每当我尝试运行此命令时,它都会返回:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
blah blah blah ...
当我尝试使用时source activate ${CONDA_ENV_NAME}
,它会返回:
/bin/bash: activate: No such file or directory
不太确定我在这里做错了什么。有人能指出我正确的方向吗?
PS:它可以与一起使用source $(conda info --root)/etc/profile.d/conda.sh
,但是由于我也需要支持Windows,所以我不能真正使用它!
免责声明:我是python
/的新手anaconda
。
我不确定要在Windows Powershell中运行什么,但是对于bash,您需要运行将conda init
bash配置为在启动时运行的脚本,而不是conda init
。那是,
miniconda3/etc/profile.d/conda.sh
所以应该像
execCmd.push(
`. ${CONDA_ROOT}/etc/profile.d/conda.sh`,
`conda activate ${CONDA_ENV_NAME}`,
// long python spawn command
`conda deactivate`,
)
我怀疑Windows的情况下运行在的.bat或名为.ps1文件中的一个condabin
目录。
另外,如果conda
已定义,并且您拥有Python脚本(例如script.py
),那么您也许可以使用conda run
,例如
execCmd.push(
`conda run -n ${CONDA_ENV_NAME} python script.py`
)
并且可能跨平台工作。请注意,conda run
只有最近添加的对交互式I / O的支持,并且必须使用该--live-stream
标志将其启用(请参阅v4.9.0发行说明)。否则,它只会缓冲所有击中stdout / stderr的内容,直到进程退出后才返回。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句