Reputation: 245
I have a lots of python scripts and each has own dependency.
And I create conda environment to each python script and install dependency too.
And I tried many ways like below
const childProcess = require('child_process');
const pythonScript = 'test.py';
const environmentName = 'test';
const command = [
`conda activate ${environmentName}`,
`python ${pythonScript}`
]
.map(v => `(${v})`)
.join(' && ');
const pythonProcess = childProcess.spwan(command, { shell: true });
pythonProcess.stdin.on('data', (data) => console.log(data.toString()));
pythonProcess.stderr.on('data', (data) => console.error(data.toString()));
pythonProcess.on('close', (code) => {
console.log('Process Exited:', code);
});
const command = [
`conda activate ${environmentName}`,
`python ${pythonScript}`
]
.map(v => `(${v})`)
.join(' && ');
const pythonProcess = childProcess.spwan(`bash -lc "${command}"`, { shell: true });
const command = [
`source /opt/conda/etc/profile.d/conda.sh`,
`conda activate ${environmentName}`,
`python ${pythonScript}`
]
.map(v => `(${v})`)
.join(' && ');
const pythonProcess = childProcess.spwan(`bash -lc "${command}"`, { shell: true });
But, in python script, conda environment doesn't enabled (just enabled default conda environemnt).
How can I run python script in specific conda environment in nodejs?
Upvotes: 2
Views: 3303
Reputation: 76750
The conda activate
command is a shell function, not a true CLI, so it's not available without first launching a shell in interactive mode (assuming you have previously run conda init
). Try using conda run
instead. In your first example, I think it would be something like
const command = `conda run -n ${environmentName} python ${pythonScript}`
This enables one to execute commands in an environment without having to manually activate the environment.
Upvotes: 2