Skip to content

gscatto/CMakePythonVenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMakePythonVenv

CMakePythonVenv integrates Python virtual environments into CMake, enabling isolated Python environments for your build workflows.

Requirements | Installation | Quick Start | API Reference | Contributing

Requirements

  • CMake 3.31.6 (used for development)
  • Python 3.3 (due to venv module)

Installation

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)

Quick Start

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)

API Reference

CMakePythonVenv_create_virtual_environment() | CMakePythonVenv_install_packages()

CMakePythonVenv_create_virtual_environment()

Creates a new Python virtual environment.

CMakePythonVenv_create_virtual_environment(
    NAME <name>
)

Parameters

  • NAME (required) - Name of the virtual environment target

Examples

# Create a virtual environment named "dev"
CMakePythonVenv_create_virtual_environment(NAME dev)

Properties

After creating a virtual environment, access the following properties through get_target_property():

  • ENV_DIR - Root directory of the virtual environment

CMakePythonVenv_install_packages()

Installs Python packages into a virtual environment using pip.

CMakePythonVenv_install_packages(
    NAME <name>
    [REQUIREMENTS_FILE <file>]
)

Parameters

  • NAME (required) - Name of the virtual environment target
  • REQUIREMENTS_FILE (optional) - Path to requirements.txt file

Examples

# From requirements file
CMakePythonVenv_install_packages(
    NAME myenv
    REQUIREMENTS_FILE requirements.txt
)

Contributing

Contributions are welcome! Please submit issues and pull requests on GitHub.

License

MIT License — See LICENSE file for details.

Copyright © 2026 Giulio Scattolin

About

CMakePythonVenv integrates Python virtual environments into CMake, enabling isolated Python environments for your build workflows.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages