How do I find the name of the conda environment in which my code is running?
Asked Answered
V

10

103

I'm looking for a good way to figure out the name of the conda environment I'm in from within running code or an interactive python instance.

The use-case is that I am running Jupyter notebooks with both Python 2 and Python 3 kernels from a miniconda install. The default environment is Py3. There is a separate environment for Py2. Inside the a notebook file, I want it to attempt to conda install foo. I'm using subcommand to do this for now, since I can't find a programmatic conda equivalent of pip.main(['install','foo']).

The problem is that the command needs to know the name of the Py2 environment to install foo there if the notebook is running using the Py2 kernel. Without that info it installs in the default Py3 env. I'd like for the code to figure out which environment it is in and the right name for it on its own.

The best solution I've got so far is:

import sys

def get_env():
    sp = sys.path[1].split("/")
    if "envs" in sp:
        return sp[sp.index("envs") + 1]
    else:
        return ""

Is there a more direct/appropriate way to accomplish this?

Victual answered 11/4, 2016 at 3:59 Comment(6)
Unless I am mistaken, conda install x will install into whatever the current environment is: so if the notebook is in the Python 2 environment then it will install into that environment.Zeeba
I just tried the following experiment. In a Jupyter notebook launched from my default anaconda environment (but with both Py2 and Py3 ipython kernels available) I ran !conda env list in one cell and import subprocess; print(subprocess.check_output(['conda','env', 'list']).decode()) in another. Both show the default env as being active whether I run the notebook in Py2 or Py3, so it may be that commands being issued against the os operate in whatever env the jupiter server was launched using.Victual
I need something simpler, how do find out what conda env you are running from the shell? (side note you could call that command from python)Vitreous
answer to my own comment conda env list and the * points to the current turned on env. Reason I do this is despite the name appearing on the terminal name prompt, I've had issues when the prompt does not match the env I'm using (e.g. using vscode).Vitreous
If you want absolute path do $ echo $CONDA_PREFIX see output: /home/miranda9/miniconda3/envs/metalearningVitreous
from within python do: import os; print(os.environ["CONDA_PREFIX"]) print /Users/miranda9/.conda/envs/synthesisVitreous
D
160

You want $CONDA_DEFAULT_ENV or $CONDA_PREFIX:

$ source activate my_env
(my_env) $ echo $CONDA_DEFAULT_ENV
my_env

(my_env) $ echo $CONDA_PREFIX
/Users/nhdaly/miniconda3/envs/my_env

$ source deactivate
$ echo $CONDA_DEFAULT_ENV  # (not-defined)

$ echo $CONDA_PREFIX  # (not-defined)

In python:

import os
print(os.environ['CONDA_DEFAULT_ENV'])

for the absolute entire path which is usually more useful:

Python 3.9.0 | packaged by conda-forge | (default, Oct 14 2020, 22:56:29) 
[Clang 10.0.1 ] on darwin
import os; print(os.environ["CONDA_PREFIX"])
/Users/miranda9/.conda/envs/synthesis

The environment variables are not well documented. You can find CONDA_DEFAULT_ENV mentioned here: https://www.continuum.io/blog/developer/advanced-features-conda-part-1

The only info on CONDA_PREFIX I could find is this Issue: https://github.com/conda/conda/issues/2764

Dittmer answered 8/3, 2017 at 0:7 Comment(4)
Python solution doesn't work for py3 because... parentheses... For py3:Friar
print(os.environ['CONDA_DEFAULT_ENV'])Friar
Excelent. I just want to add that source deactivate is deprecated, now you have to use conda deactivate.Dc
but this doesn't the entire path, just the name of the head....something like this /home/miranda9/miniconda3/envs/metalearning is what I want. So do import os; print(os.environ["CONDA_PREFIX"])Vitreous
R
36
conda info

directly lists all the information where in the first lines you can see the

active environment: (some name)
active env location: (location of active environment)

I guess this is the most clear way.

In an interactive environment like Jupyter Notebook or Jupyter Lab, you should use % before typing the commands, like the following,

%conda info
Remindful answered 24/11, 2020 at 11:56 Comment(0)
P
19

I am using this:

import sys
sys.executable.split('/')[-3]

it has the advantage that it doesn't assume the env is in the path (and is nested under envs). Also, it does not require the environment to be activated via source activate.

Edit: If you want to make sure it works on Windows, too:

import sys
from pathlib import Path
Path(sys.executable).as_posix().split('/')[-3]

To clarify: sys.executable gives you the path of the current python interpreter (regardless of activate/deactivate) -- for instance '/Users/danielsc/miniconda3/envs/nlp/bin/python'. The rest of the code just takes the 3rd from last path segment, which is the name of the folder the environment is in, which is usually also the name of the python environment.

Practise answered 23/2, 2019 at 20:37 Comment(3)
How does this give the name of the conda environment?Sweetie
This is incorrect if you are running a python that is not from the active environment.Aggri
for me it's Path(sys.executable).as_posix().split('/')[-2]Admissible
S
7

Edit: Oops, I hadn't noticed Ivo's answer. Let's say that I am expanding a little bit on it.


If you run your python script from terminal:

import os
os.system("conda env list")

This will list all conda environments, as from terminal with conda env list.

Slightly better:

import os
_ = os.system("conda env list | grep '*'")

The _ = bit will silence the exist status of the call to os.system (0 if successful), and grep will only print out the line with the activated conda environment.

If you don't run your script from terminal (e.g. it is scheduled via crontab), then the above won't have anywhere to "print" the result. Instead, you need to use something like python's subprocess module. The simplest solution is probably to run:

import subprocess
output = subprocess.check_output("conda env list | grep '*'", shell=True, encoding='utf-8')
print(output)

Namely output is a string containing the output of the command conda env list, not its exit status (that too can be retrieved, see documentation of the subprocess module).

Now that you have a string with the information on the activated conda environment, you can perform whichever test you need (using regular expressions) to perform (or not) the installs mentioned in your question.

Remark.
Of course, print(output) in the block above will have no effect if your script is not run from terminal, but if you test the block in a script which you run from terminal, then you can verify that it gives you what you want. You can for instance print this information into a log file (using the logging module is recommended).

Sweetie answered 11/6, 2019 at 10:46 Comment(0)
Z
5

very simply, you could do

envs = subprocess.check_output('conda env list').splitlines()
active_env = list(filter(lambda s: '*' in str(s), envs))[0]
env_name = str(active_env).split()[0]
Zoniazoning answered 11/4, 2016 at 4:11 Comment(1)
I get an error running this unless I put the command as a list -- ['conda','env','list'] -- and then I get the same answer (root) whether I run this using a Py2 or a Py3 kernel. Examining the sys.path, however, shows different results for each.Victual
S
5

On Windows (might work but untested on Linux):

import sys
import os

# e.g. c:\Users\dogbert\Anaconda3\envs\myenvironment
print( sys.exec_prefix.split(os.sep)[-1] )

Answers using environment variables or assuming the path separator is "/" didn't work in my Windows/Anaconda3 environment.

This assumes you are in an environment.

Serpentiform answered 29/8, 2019 at 19:11 Comment(0)
P
3

Since similar searches related to 'how do I determine my python environment' leads to this answer I thought I will also mention a way I find out which environment I am currently running my code from. I check the location of my pip binary which points to a location within the current environment. By looking at the output of the following command you can easily determine which environment you are in. (Please note that this solution is not applicable if you have inherited pip packages from your global environment/other environment)

In Windows command prompt:

where pip

If you are inside a Jupyter Notebook add an exclamation mark(!) before the command to execute the command in your host command prompt:

in[10]: !where pip

The output will look something like this:

C:\Users\YourUsername\.conda\envs\YourEnvironmentName\Scripts\pip.exe
C:\ProgramData\Anaconda3\Scripts\pip.exe

YourEnvironmentName gives out the name of your current environment.

In Linux/Mac, you can use the which command instead of where: (Not tested).

For python3 environment

which pip3

From Jupyter notebook:

in[10]: !which pip3

This should directly point to the location within your current environment.

Parkinson answered 19/5, 2019 at 12:12 Comment(1)
This answers my confusion. I installed jupyter notebook with pip3 install and the jupyter notebook will run in the environment given by pip3 instead of conda. I have conda installed but Anaconda is not installed on my CentOS linux.Laundes
S
2
  1. Several answers suggest the use of 'which pip', 'which python', or 'conda env list to grep the default'. This work if the user is doing something like: $ conda activate env_name; $ python ... or $ jupyter notebook/jupyterlab.

  2. When a user invokes python directly without conda activate, method #1 would not work: e.g. $ /opt/conda/envs/my_env/bin/python (where my_env is the name of env)

  3. In a more general case with jupyter notebook, one can select any one of the available conda env/kernel, and the one selected may not be the same as the default.

  4. So the solution is to examine the executable or path of your current python, like several folks have posted before. Basically, sys.path returns the full path of executable, and one can then use split to figure out the name after envs/ which would be the env_name. The person who asked this question gave a pretty good answer, except missing this ....

  5. I don't think any post took care of the special case of the base env. Note python from base env is just /opt/conda/bin/python. So one can simply add the following code fragment do a match if /opt/conda/bin/python in sys.path: return 'base'

  6. Here we assume conda is installed on /opt/conda. For really generic solution, one can use $ conda info --root to find the installation path.

So answered 12/5, 2020 at 23:31 Comment(0)
M
2
  1. Open Anaconda Prompt
  2. Type "conda info --env"

It will give list of all virtual environment To activate virtual Environment

  1. "conda activate virtual_environment_name"
Messaline answered 9/8, 2022 at 17:6 Comment(0)
V
1

For the absolute entire path which is usually more useful:

Python 3.9.0 | packaged by conda-forge | (default, Oct 14 2020, 22:56:29) 
[Clang 10.0.1 ] on darwin
import os; print(os.environ["CONDA_PREFIX"])
/Users/miranda9/.conda/envs/synthesis
Vitreous answered 19/8, 2021 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.