How to create and activate conda env in one line? - python

It often happens that when I create a conda environment, I forgot about activating it, despite the clear conda message. And I end up installing my packages in the conda base environment. (yeah... I'm a bit of a dreamer)
My questions are:
Are there use cases where you create a conda environment but don't activate it right after?
How to create a conda environment and activate it in a single line? (in a Linux prompt shell, and non-interactive)

As a concrete example in answer to your first question, I have used conda as a way to wrap a disposable build environment in some Makefile targets, i.e., I create the environment, and then subsequent commands or targets may make use of the environment via conda run.
Adapting a snippet from one Makefile, you could create a function in a bash startup file:
conda_create_and_run() {
ENV_NAME=$1
CONDA_PY_VER=$2
. ${CONDA_ENV_FILE}
conda config --append envs_dirs ${CONDA_DIR}
conda create -p ${CONDA_DIR}/${ENV_NAME} python=${CONDA_PY_VER} -y
conda activate ${ENV_NAME}
}
Here CONDA_PY_VER is the non-default python version you want the environment to possibly be created with, and CONDA_DIR and CONDA_ENV_FILE are, respectively, the location where conda keeps its environments and the conda environment file you need to source (or have part of your shell init) in order to have the conda commands available.
You would then use it as:
conda_create_and_run myenv 3.8
to create an environment for python3.8 named myenv.

Related

Create and activate conda environment with cmake

I have the following in a CMakeLists.txt file. I'm trying to use cmake to check if a conda environment named myenv is installed on the system and activate that environment. If the environment does not exist, then create the environment and activate it. This assumes that conda is already installed via Anaconda (or Miniconda).
# Create and activate a Python environment.
cmake_minimum_required(VERSION 3.18)
# Define the project
project(MyExample)
# Specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Make sure Python is installed
find_package(Python REQUIRED)
# Activate conda environment, assume Anaconda or Miniconda is already installed
if(EXISTS /opt/miniconda3/envs/myenv)
execute_process(COMMAND conda activate myenv)
else()
execute_process(COMMAND conda create --yes --quiet --name myenv python)
execute_process(COMMAND conda activate myenv)
endif()
When I run the above cmake file, I get the error:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
However, conda is installed on my system and I can activate the environment manually in the terminal. Why does the environment not get activated via cmake?
"Why does the environment not get activated via cmake?"
The first issue is in the error message, which indicates that the shell that CMake evaluates the execute_process command under doesn't know what conda activate means.
This could be because conda init <shell> has never been run for the particular shell that CMake uses (bash, perhaps?). This answer has some details on what conda init does. It could also be because the Conda initialization code that defines conda activate only gets loaded in interactive shells - which I wouldn't expect CMake to be using. There might be hacky ways to force execution in an interactive shell (bash -l -c 'conda activate foo'), but that doesn't matter because...
Even if the above were working, the procedures here don't make sense: a Conda environment's activation status is scoped to the shell process. I would expect that the (sub)shell dies with the completion of the execute_process. So, even if the activation worked, it wouldn't persist any further in the CMake script.
Discussion
Generally, this CMake script does not seem like a good approach. Tight-coupling the compilation of code to the existence of a particularly-named Conda environment at the user level seems to go against the spirit of CMake, which aims to automate the discovery of software dependencies so that users don't need commonly hardcoded locations. Perhaps it might be worth reassessing what is trying to be accomplished and the strategy to get there.
For example, on Conda Forge, lots of packages compile with CMake, but there CMake is executed in the context of an already activated environment and itself knows nothing about Conda. This makes it so the CMake code is completely agnostic to how its dependencies are provided, which I regard as cleaner engineering.

How to activate base env on conda prompt

Conda works as expected and activates the base env on every new installation when I run conda in my miniconda prompt. However, it does not activate the base env anymore at when I rerun after installation, I have added paths to system environment and conda activate doesn't work
Don't add miniconda/python.exe to the path. It won't help you, since more sophisticated packages like numpy would also need the paths to the C-libraries. This is exactly what is achieved by activating an environment via
conda activate
in the command shell. (Check PATH before and after.)
For further information read the docs: https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#activating-an-environment

conda environment name changes upon using conda activate

Output of conda info --envs:
conda info --envs-o/p-1
I ran conda activate tfgpu (which is one of the existing environments, please refer to the above picture using the link).
Then I ran conda info --envs, the output of which is as follows:
conda info --envs-o/p-2
As you can see, the name of the base environment is no longer visible and when I use conda activate base, the environment isn't changing anymore.
I found a workaround for this, which is as follows:
Run conda activate <base_env_path> from tfgpu environment
Run conda activate base
Finally , run conda info --envs. Output is: conda info --envs-o/p-1
My question:
What should I do to make sure that conda doesn't change the name of the base environment, so that I wouldn't have to do steps 1-3 to resolve it?
Please note that I cannot post inline images as my reputation is quite low.
While you are on tfgpu just do
conda deactivate
It will automatically deactivate your tfgpu environment and return to base environment. ( It does in my PC ).
In conda environment nesting is possible, afaik so when you are doing your mentioned steps 1-3 it nests your base environment over tfgpu instead of switching, this can cause issues.
[ Source - https://www.fatalerrors.org/a/a-series-of-path-problems-caused-by-nesting-conda-virtual-environments.html ]
For details you may refer - https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
I have deleted the second virtual environment tfgpu and create a new one.

How to fix: Conda activation is not working?

installed Python 3.7.3 and Anaconda and tried to activate it in the Anaconda Prompt.
activate %PATH%
but i get the error
activate does not accept more than one argument
What can I do about it ?
I think you didn't quite understand how conda works.
In conda, you need to first create your own environment. In this case, let's call it my_env.
conda create -n my_env python
Then, you can activate that environment with
conda activate my_env
Also, pay attention that the conda precedes the activate, using activate directly is obsolete.
If you have space in path, please try this
activate "C:\Users\USER\New Project\"
Generally you have to specify the conda env full path.
yes writing your argument in a closed quote worked for me if there is a space in between characters. So here's what I did with my issue:
>>> conda activate "C:\Users\Name\Desktop\sample_project_1\env"
If you use Linux you can start conda with command (assuming that conda was installed # ~/miniconda3/)
source ~/miniconda3/bin/activate
or
source [conda_install_path]/bin/activate
or
source [conda_install_path]/bin/activate base
By default, (base) virtual environment is loaded.
You can switch to a different environment by
conda activate [env_name]
A sorter path is to type directly
source [conda_install_path]/bin/activate [env_name]
As usual, you can avoid re-typing activation commands every time by augmenting the .bash_rc script

Python Anaconda: should I use `conda activate` or `source activate` in linux

So I am used to typing source activate <environment> when starting a python Anaconda environment. That works just fine. But when I create new conda environments I am seeing the message on Ubuntu 16.04 to start the environments with conda activate instead. Besides the errors about how to set up my shell to use conda activate instead, I am still not clear on what is the difference between source activate ... and conda activate ... Is there a reason to change? Does anyone know the difference between these two commands? Thanks.
As of conda 4.4, conda activate is the preferred way to activate an environment. Generally, you won't find too much of a difference between conda activate and the old source activate, except that it's meant to be faster, and work the same across different operating systems (the latter difference makes conda activate a huge improvement IMO).
From the docs, regarding the release of conda version 4.4.0 (released December 2017):
conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activate and conda deactivate are now the preferred commands for activating and deactivating environments. You’ll find they are much more snappy than the source activate and source deactivate commands from previous conda versions. The conda activate command also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like python virtualenv’s activate script.
Here is one difference I found. source activate can be used at the beginning of a bash script to load conda environment, whereas conda activate would give me an error:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
This makes a huge difference to me since I often submit bash jobs to cluster and source activate is the only way to change conda environment.
Please correct me if anyone can use conda activate in a bash script.
I am not sure who might find this useful, but if
Your terminal lags due to the addition ">>> conda initialize
" in your .bashrc, then you decide to remove it and add anaconda to the path. If that is the case, then "conda activate env_name"
won't work, but "source activate env_name" will work, and then after
that, you can use either source activate or conda activate. If you
close the shell then to activate the environment again use "source
activate env_name"
FYI, removing ">>> conda initialize >>>" from my .bashrc file has
speedup my terminal and it doesn't lag anymore and I just default in
using "source activate env_name"
I have Ubuntu 20.04, conda version : 4.10.3, and conda-build version
: 3.21.5

Categories

Resources