Cmake似乎没有使用它确认它使用的Python解释器

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (264)

我有以下CMakeLists.txt文件,它被指示使用Python 4.4

cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../cmake/")

project(aConfigd VERSION 1.0)
string(TOLOWER aConfigd project_id)

find_package(PythonInterp 3.4 REQUIRED)

include(FindPythonInterp)
set(PYTHON ${PYTHON_EXECUTABLE})
message(STATUS "\${PYTHON_EXECUTABLE} == ${PYTHON_EXECUTABLE}")
set(pkgdatadir /usr/share/configd)
set(configdir /etc/amy)
set(SONARCONFIGID_SOURCE_DIR etc/configd)
set(SRC_DIR configd/src/)

include(common)
#        "${SRC_DIR}/systemd_client.py"
#        "${SRC_DIR}/amyconfig_service.py"
        "${SRC_DIR}/__init__.py"
        "${SRC_DIR}/main.py"
        "${SRC_DIR}/application.py"
        DESTINATION ${pkgdatadir}/configd/
)

#general
set(CPACK_PACKAGE_NAME "a-config")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "a-config-manager")
set(CPACK_PACKAGE_DESCRIPTION "a-config-manager")

# redhat
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION 
      /etc/amy
      )

include(cpack)

实际上,它确认了${PYTHON_EXECUTABLE} == /usr/bin/python3.4(见下面的第4行):

$ make clean ; cmake -DCMAKE_BUILD_TYPE=Release -DSHORT_VERSION=NO -DCUSTOMER=NO .. ;  make -j12 ; make package
-- Found PythonInterp: /usr/bin/python3.4 (found suitable version "3.4.5", minimum required is "3.4") 
-- Found PythonInterp: /usr/bin/python3.4 (found version "3.4.5") 
-- ${PYTHON_EXECUTABLE} == /usr/bin/python3.4
-- Build Type: Release
-- Detected distribution: rhel fedora
-- Detected aConfigd version: 2.3.0-3030-gf7733cf659
-- Detected distribution: rhel fedora
-- Configuring done
-- Generating done
-- Build files have been written to: /local/raid0/git/amy/aConfig/build
Run CPack packaging tool...
CPack: Create package using RPM
CPack: Install projects
CPack: - Run preinstall target for: aConfigd
CPack: - Install project: aConfigd
CPack: Create package
CPackRPM:Warning: CPACK_SET_DESTDIR is set (=ON) while requesting a relocatable package (CPACK_RPM_PACKAGE_RELOCATABLE is set): this is not supported, the package won't be relocatable.
CPackRPM: Will use GENERATED spec file: /local/raid0/git/my/aConfig/build/_CPack_Packages/Linux/RPM/SPECS/a-config.spec
CPack: - package: /local/raid0/git/my/aConfig/build/a-config-2.3.0-3030-gf7733cf659.el7.my.x86_64.rpm generated.
$ 

但是,如果我取消注释该"${SRC_DIR}/systemd_client.py"行,我会收到错误:

Compiling /local/raid0/git/my/aConfig/build/_CPack_Packages/Linux/RPM/a-config-2.3.0-3030-gf7733cf659.el7.my.x86_64/usr/share/configd/configd/systemd_client.py ...
  File "/usr/share/configd/configd/systemd_client.py", line 21
    def __init__(self, systemd_proxy:Gio.DBusProxy):
                                    ^
SyntaxError: invalid syntax

不是def __init__(self, systemd_proxy:Gio.DBusProxy): a valid Python 3.4语法吗? 如果是的话,为什么Cmake抱怨?

提问于
用户回答回答于

当你刚刚运行时"${SRC_DIR}/systemd_client.py",你告诉它以与shell运行相同的方式运行该脚本:通过查看该#!行并使用在那里指定的任何解释器运行它。这可能是#! /usr/bin/python或类似的东西#! /usr/bin/env python

如果要使用特定的解释器运行脚本,则必须运行该解释器并将其传递给脚本 - 就像在shell中一样。我对CMake非常生疏,但我认为你是这样做的:

"${PYTHON_EXECUTABLE}" "${SRC_DIR}/amyconfig_service.py"

或者,由于这是您的代码,您可能希望以setuptools编程方式为您的入口点生成脚本,这意味着它将#!为它们创建一个运行任何Python版本的行setup.py

扫码关注云+社区

领取腾讯云代金券