CMakePythonVenv integrates Python virtual environments into CMake, enabling isolated Python environments for your build workflows.
Requirements | Installation | Quick Start | API Reference | Contributing
- CMake 3.31.6 (used for development)
- Python 3.3 (due to
venvmodule)
Add CMakePythonVenv to your CMake project using FetchContent:
include(FetchContent)
FetchContent_Declare(CMakePythonVenv
GIT_REPOSITORY "https://github.com/gscatto/CMakePythonVenv"
GIT_TAG "3e827c3e503837671640d5df4969dfe07d9b70bb"
)
FetchContent_MakeAvailable(CMakePythonVenv)
get_property(CMakePythonVenv_CMAKE_MODULE_PATH
TARGET CMakePythonVenv
PROPERTY CMAKE_MODULE_PATH
)
list(APPEND CMAKE_MODULE_PATH "${CMakePythonVenv_CMAKE_MODULE_PATH}")
include(CMakePythonVenv)Create a Python virtual environment, install packages from requirements.txt and use the Python interpreter in your build.
# Create a virtual environment
CMakePythonVenv_create_virtual_environment(NAME venv)
# Install packages from requirements file
CMakePythonVenv_install_packages(
NAME venv
REQUIREMENTS_FILE requirements.txt
)
# Use the Python interpreter in your build
get_target_property(Python_ROOT_DIR venv ENV_DIR)
find_package(Python)CMakePythonVenv_create_virtual_environment() | CMakePythonVenv_install_packages()
Creates a new Python virtual environment.
CMakePythonVenv_create_virtual_environment(
NAME <name>
)NAME(required) - Name of the virtual environment target
# Create a virtual environment named "dev"
CMakePythonVenv_create_virtual_environment(NAME dev)After creating a virtual environment, access the following properties through get_target_property():
ENV_DIR- Root directory of the virtual environment
Installs Python packages into a virtual environment using pip.
CMakePythonVenv_install_packages(
NAME <name>
[REQUIREMENTS_FILE <file>]
)NAME(required) - Name of the virtual environment targetREQUIREMENTS_FILE(optional) - Path torequirements.txtfile
# From requirements file
CMakePythonVenv_install_packages(
NAME myenv
REQUIREMENTS_FILE requirements.txt
)Contributions are welcome! Please submit issues and pull requests on GitHub.
MIT License — See LICENSE file for details.
Copyright © 2026 Giulio Scattolin