# Create and activate Python conda environment with CMake

**URL:** <https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777>\
**Category:** Code\
**Created:** [June 1, 2022, 7:47pm UTC](https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777 "2022-06-01T19:47:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wigging](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wigging/32/2468_2.png) [@wigging](https://discourse.cmake.org/u/wigging)\
**Post date:** [June 1, 2022, 7:47pm UTC](https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777/1 "2022-06-01T19:47:55Z")

</div>

I have the following in a `CMakeLists.txt` file. The goal is to use cmake to check if a Python conda environment named `myenv` is already installed 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](https://www.anaconda.com) (or Miniconda).

```auto
# 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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 1, 2022, 10:19pm UTC](https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777/2 "2022-06-01T22:19:43Z")

</div>

`conda activate` won’t work in CMake because it modifies the caller’s environment through either being sourced (if `conda` is a shell function) or by launching a new command (which also can’t affect CMake).

Environments like that need to be set up outside of CMake whether it be HomeBrew, Nix, vcpkg, spack, or conda.

---

<div class="post-metadata">

**Author:** ![wigging](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wigging/32/2468_2.png) [@wigging](https://discourse.cmake.org/u/wigging)\
**Post date:** [June 2, 2022, 12:38am UTC](https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777/3 "2022-06-02T00:38:15Z")

</div>

So for a project with C++ and Python code, does the following sound reasonable.

Step 1. Create the Python environment by running the following in the terminal

```auto
$ conda env create -f environment.yml
$ conda activate myenv

```

Step 2. Build and install the C++ environment using CMakeLists.

Or is there another approach that could accomplish this in one step?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 2, 2022, 12:41am UTC](https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777/4 "2022-06-02T00:41:43Z")

</div>

> [@wigging](#):
>
> Or is there another approach that could accomplish this in one step?

Not really, no. You could write a shell script or `Makefile`-like thing to do it for you, but unless `conda` is actually the only way to build the software, I would just document the recommended way of building it using `conda` (e.g., someone using `vcpkg` probably isn’t going to care about `conda`). Compilers and the build environment are the choice of the builder. The project can sanity check it, but making things _too_ strict is closer to premature optimization IMO.

---

<div class="post-metadata">

**Author:** ![wigging](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wigging/32/2468_2.png) [@wigging](https://discourse.cmake.org/u/wigging)\
**Post date:** [June 2, 2022, 12:44am UTC](https://discourse.cmake.org/t/create-and-activate-python-conda-environment-with-cmake/5777/5 "2022-06-02T00:44:37Z")

</div>

Ok, thank you @ben.boeckel for the comments.
