[CMake] installing python scripts

Michael Wild themiwi at gmail.com
Mon Jul 30 02:58:35 EDT 2012


On 07/30/2012 02:15 AM, Kyle Husmann wrote:
> Hi all,
> 
> In my project I have a python program (call it program.py) that
> references a python module also in my project (call it pymodule).
> program.py gets installed to ${CMAKE_INSTALL_PREFIX}/bin/program and
> pymodule gets installed to ${PYTHON_INSTALL_DIR}.
> 
> To make sure the program.py program always finds pymodule, I treat it
> like a config script in cmake and then add the following to the top of
> program.py.in <http://program.py.in>:
> 
> import sys
> sys.path.append("@PYTHON_INSTALL_DIR@")
> import pymodule
> 
> This works great, but it means that I can only run program.py after
> running a make install, and running it from the installed location.
> 
> My question is, is there are clever way of doing this so that I can run
> program.py from the build directory, without having to install it? (But
> have it still run properly when installed)
> 
> Thanks,
> Kyle

Call CONFIGURE_FILE() twice, once for the installation and once for the
build-tree:

set(CONFIG_PY_DIR ${PROJECT_SOURCE_DIR})
configure_file(program.py.in ${PROJECT_BINARY_DIR}/program.py @ONLY)
set(CONFIG_PY_DIR ${PYTHON_INSTALL_DIR})
configure_file(program.py.in
  ${PROJECT_BINARY_DIR}/installFiles/program.py @ONLY)
install(PROGRAMS ${PROJECT_BINARY_DIR}/installFiles/program.py
  DESTINATION bin)


You'll need to replace PYTHON_INSTALL_DIR with CONFIG_PY_DIR in your
program.py.in file, of course. Also, I would replace the
sys.path.append("@CONFIG_PY_DIR@") call with sys.path.insert(0,
"@CONFIG_PY_DIR@"). There are pros and cons for each...

HTH

Michael


More information about the CMake mailing list