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

Question:

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?

Asked By: zhilothebest

||

Answers:

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])
Answered By: jsb

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.

Answered By: brianbhsu

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 "$@"
Answered By: Oswin

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)
Answered By: Flinck Clissan

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
Answered By: AVINASH SRIVASTAVA
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.