zhilothebest
zhilothebest

Reputation: 323

How do you activate an Anaconda environment within a Python Script?

I have searched through stack overflow, but no post helped specifically with Anaconda.

I am trying to my own Command line prompt in Python. But to do that, I need to activate my Anaconda environment. I cant find anywhere on the web how to run a basic python script to be able to activate said environment. Does anyone know how to do so?

Upvotes: 33

Views: 41775

Answers (6)

Martin David Grunnill
Martin David Grunnill

Reputation: 41

An improvement on @Oswin's solution. This works for me. Replace conda list with your own command after testing.

import sys
import subprocess
executable = sys.executable
conda_init_shell = '/'.join([*executable.split('/')[:-4], 'etc/profile.d/conda.sh'])
conda_init_shell
cmd = f'. {conda_init_shell} && conda activate YOUR-ENVIORONMENT-HERE && conda list'
subprocess.run(cmd, shell=True)

Upvotes: 0

AVINASH SRIVASTAVA
AVINASH SRIVASTAVA

Reputation: 18

the steps of creating a virtual environment using conda interface:

Step 1: Check if conda is installed in your path.

conda -V

Step 2: Update the conda environment

conda update conda

Step 3: Set up the virtual environment

conda create -n envname python=x.x anaconda

Upvotes: -2

Ivan Kakhaev
Ivan Kakhaev

Reputation: 154

Here I found this code worked for me(Ubuntu)

import os
os.system("conda run -n <env_name> python <file_name>.py")

Or you can use subprocess as well

import subprocess
subprocess.run('conda run -n <env_name> python <file_name>.py', shell=True)

Upvotes: 7

Oswin
Oswin

Reputation: 21

I need the same thing. But we CANNOT use source in subprocess in Linux. Here is my solution.

For a subprocess, I append the PYTHON script after a shell, in which the conda environment is setted.

# This is a Python code
script = 'python test.py'
cmd_dict = {
¦   ¦   'activate_env': 'activate_env_%s.sh' % (random.randint(1000,9999)),
¦   ¦   'script': script,
¦   ¦   'conda_name': conda_name,
¦   ¦   }
cmd = 'cp activate.sh {activate_env} && echo "{script}" >> {activate_env} && sh {activate_env} {conda_name}'.format(**cmd_dict)
env = {}
env.update(os.environ)
result = subprocess.run(cmd, shell=True, env=env)

Here is my activate.sh,

#!/bin/sh
_CONDA_ROOT="/home/***/miniconda3"
. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
_conda_activate "$@"

Upvotes: 2

brianbhsu
brianbhsu

Reputation: 710

The following will work in Python 3.5 using the subprocess module:

subprocess.run('source activate environment-name && "enter command here" && source deactivate', shell=True)

Replace the "enter command here" with the command you want to run. You don't need the "source deactivate" at the end of the command but it's included just to be safe.

This will temporarily activate the Anaconda environment for the duration of the subprocess call, after which the environment will revert back to your original environment. This is useful for running any commands you want in a temporary environment.

Upvotes: 21

jsb
jsb

Reputation: 80

You should be able to use the command line to activate specific environments just by typing:

activate environment-name

You will have to do use source on Linux.

source activate environment-name

http://conda.pydata.org/docs/using/envs.html#change-environments-activate-deactivate

EDIT (3/29/2016)

Sorry, I misread the question. You should be able to use the call method in the subprocess module to run the shell command through a Python script.

Edit: A basic example of the subprocess:

subprocess.call(["activate", value])

Upvotes: -6

Related Questions