Check what conda environment is currently activated
When a conda
environment is activated, it will export
following related environment variables:
$CONDA_DEFAULT_ENV
, name of current activated env$CONDA_PREFIX
, path to the current activate env
How do I find the name of the conda environment in which my code is running?
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
how do you check to see if any conda environment is active with a flag
I found the answer myself. There is a CONDA_SHLVL
environment variable.
[[ $CONDA_SHLVL == 1 ]] && echo "conda environment is active"
Another option would be to run
conda info
but the output must be parsed
when a conda env is not active it will display
william‣ wmbp‣ ~ % conda info
active environment : None
...
Check if conda env exists and create if not in bash
update 2022
i've been receiving upvotes recently. so i'm going to bump up that this method overall is not natively "conda" and might not be the best approach. like i said originally, i do not use conda. take my advice at your discretion.
rather, please refer to @merv's comment in the question suggesting the use of the --prefix
flag
additionally take a look at the documentation for further details
NOTE: you can always use a function within your bash script for repeated command invocations with very specific flags
e.g
function PREFIXED_CONDA(){
action=${1};
# copy $1 to $action;
shift 1;
# delete first argument and shift remaining indeces to the left
conda ${action} --prefix /path/to/project ${@}
}
i am not sure how conda env list
works (i don't use Anaconda); and your current if
-tests are vague
but i'm going out on a limb and guessing this is what you're looking for
#!/usr/bin/env bash
# ...
find_in_conda_env(){
conda env list | grep "${@}" >/dev/null 2>/dev/null
}
if find_in_conda_env ".*RUN_ENV.*" ; then
conda activate RUN_ENV
else
# ...
instead of bringing it out into a separate function, you could also do
# ...
if conda env list | grep ".*RUN_ENV.*" >/dev/null 2>&1; then
# ...
bonus points for neatness and clarity if you use command grouping
# ...
if { conda env list | grep 'RUN_ENV'; } >/dev/null 2>&1; then
# ...
if
simply checks the exit code. and grep
exits with 0
(success) as long as there's at least one match of the pattern provided; this evaluates to "true" in the if
statement
(grep
would match and succeed even if the pattern is just 'RUN_ENV' ;) )
the awk
portion of ENVS=$(conda env list | awk '{print }' )
does virtually nothing. i would expect the output to be in tabular format, but {print }
does no filtering, i believe you were looking for {print $n}
where n
is a column number or awk /PATTERN/ {print}
where PATTERN is likely RUN_ENV
and only lines which have PATTERN are printed.
but even so, storing a table in a string variable is going to be messing. you might want an array.
then coming to your if
-condition, it's plain syntactically wrong.
- the
[[
construct is for comparing values: integer, string, regex - but here on the left of
=
we have a commandconda env list
- which i believe is also the contents of
$ENVS
- which i believe is also the contents of
- hence we can assume you meant
[[ "${ENVS}" == *"RUN_ENV"* ]]
- or alternately
[[ $(conda env list) == *"RUN_ENV"* ]]
- or alternately
- but still, regex matching against a table... not very intuitive imo
- but it works... sort of
- the proper clean syntax for regex matching is
[[ ${value} =~ /PATTERN/ ]]
In which conda environment is Jupyter executing?
Question 1: Find the current notebook's conda environment
Open the notebook in Jupyter Notebooks and look in the upper right corner of the screen.
It should say, for example, "Python [env_name]" if the language is Python and it's using an environment called env_name.
Question 2: Start Jupyter Notebook from within a different conda environment
Activate a conda environment in your terminal using source activate <environment name>
before you run jupyter notebook
. This sets the default environment for Jupyter Notebooks. Otherwise, the [Root] environment is the default.
You can also create new environments from within Jupyter Notebook (home screen, Conda tab, and then click the plus sign).
And you can create a notebook in any environment you want. Select the "Files" tab on the home screen and click the "New" dropdown menu, and in that menu select a Python environment from the list.
How to activate a miniconda environment in anaconda?
It is perhaps a bit hidden or unexpected but you can use absolute path to activate every conda environment. Copying from conda activate --help
:
ActivateHelp: usage: conda activate [-h] [--[no-]stack] [env_name_or_prefix]
Activate a conda environment.
Options:
positional arguments:
env_name_or_prefix The environment name or prefix to activate. If the
prefix is a relative path, it must start with './'
(or '.' on Windows).
More concretely, you can write:
conda activate /Users/my_name/opt/miniconda3/envs/my_miniconda_env
and it should work.
Note: This post is written for the sake of completion, since Brian said in the comments that it works for him.
When I activate a conda environment, username and current directory disappear in the terminal
You can set changeps1
to False
by running the following command in the terminal:
conda config --set changeps1 False
or manually update .condarc
.
https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html#change-command-prompt-changeps1
How to activate an Anaconda environment
If this happens you would need to set the PATH for your environment (so that it gets the right Python from the environment and Scripts\ on Windows).
Imagine you have created an environment called py33 by using:
conda create -n py33 python=3.3 anaconda
Here the folders are created by default in Anaconda\envs, so you need to set the PATH as:
set PATH=C:\Anaconda\envs\py33\Scripts;C:\Anaconda\envs\py33;%PATH%
Now it should work in the command window:
activate py33
The line above is the Windows equivalent to the code that normally appears in the tutorials for Mac and Linux:
$ source activate py33
More info:
https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/8T8i11gO39U
Does `anaconda` create a separate PYTHONPATH variable for each new environment?
Related Topics
.Net Core 3.1 Deploy on Centos 7
How to Get Awk to Print Without White Space
Error: You Must Install at Least One Postgresql-Client-<Version> Package
Environment Variables in Symbolic Links
Mqtt Socket Error on Client <Unknown>
Difference Between $() and () in Bash
Releasing All Keys After Disabling The Keyboard in X11/Linux Using Xinput
Linux Usb Driver Probe Not Called If Device Already Plugged
Polling a Loop Device Through a Kernel Module
Partitioning Images Based on Their White Space
Command Line Video Editing Tools
Sendmail/Procmail - Get Mail Sender and Mail Subject, Utf8 Encoding Issues
Javafx: Tested/Confirmed Hardware (Gpu) Acceleration on Linux