gpt4 book ai didi

CMake - 设置 python virtualenv

转载 作者:行者123 更新时间:2023-12-04 02:22:55 24 4
gpt4 key购买 nike

我希望我的项目正在用 cmake 中的部分构建 - 让 cmake 为我的 python 代码设置一个 virtualenv。

这是通常完成的还是我应该假设我的 python 环境将成为一个全局环境?

最佳答案

链接的要点有效

cmake_minimum_required(VERSION 3.6)

project(CmakeVirtualenv)

enable_testing()

# Find Python and Virtualenv. We don't actually use the output of the
# find_package, but it'll give nicer errors.
find_package(PythonInterp 2.7 REQUIRED)
find_program(VIRTUALENV virtualenv)
if(NOT VIRTUALENV)
message(FATAL_ERROR "Could not find `virtualenv` in PATH")
endif()
set(VIRTUALENV ${VIRTUALENV} -p python2.7)

# Generate the virtualenv and ensure it's up to date.
add_custom_command(
OUTPUT venv
COMMAND ${VIRTUALENV} venv
)
add_custom_command(
OUTPUT venv.stamp
DEPENDS venv requirements.txt
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/requirements.txt requirements.txt
COMMAND ./venv/bin/pip install -r requirements.txt --upgrade
)

# Build command line to run py.test.
set(PYTEST
${CMAKE_CURRENT_BINARY_DIR}/venv/bin/python2
${CMAKE_CURRENT_BINARY_DIR}/venv/bin/py.test
)


add_custom_target(Tests ALL
DEPENDS venv.stamp
SOURCES requirements.txt
)

add_test(NAME run_tests
COMMAND ${PYTEST} ${CMAKE_CURRENT_SOURCE_DIR}/test_sample.py
)

请注意,您可能需要使用 ${CMAKE_CURRENT_BINARY_PATH/venv}或类似,如果您使用 WORKING_DIRECTORY覆盖当前工作目录。

关于CMake - 设置 python virtualenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30802485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com