How to put swig wrappers in a reachable location to be tested by python tests?
<p>I got a simple test example like this one:</p>

<p><a href="http://i.stack.imgur.com/4GHE0.png" rel="nofollow"><img src="http://i.stack.imgur.com/4GHE0.png" alt="enter image description here"></a></p>

<p>And my CMakeLists.txt looks like this:</p>

<pre><code>cmake_minimum_required(VERSION 3.7)

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
set(SWIG_MODULE_example_EXTRA_DEPS example.cxx example.h)
SWIG_ADD_MODULE(example python example.i example.cxx)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})
</code></pre>

<p>After generating the ST files and building the wrapper I'll get this output in the build directory:</p>

<p><a href="http://i.stack.imgur.com/NIPIL.png" rel="nofollow"><img src="http://i.stack.imgur.com/NIPIL.png" alt="enter image description here"></a></p>

<p>Problem here is my test <code>runme.py</code> isn't able to load those libraries. So, my question is, how can I place the <code>example.py</code> and <code>example.pyd</code> (only those ones) to be in a reachable place to be tested properly? I've tried adding this line to the CMakeLists.txt <code>set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/..)</code> and the result was placing the .py and the generated wrappers alone:</p>

<p><a href="http://i.stack.imgur.com/KrrcL.png" rel="nofollow"><img src="http://i.stack.imgur.com/KrrcL.png" alt="enter image description here"></a></p>

<p>And the .pyd remaining in the build folder:</p>

<p><a href="http://i.stack.imgur.com/9R3yO.png" rel="nofollow"><img src="http://i.stack.imgur.com/9R3yO.png" alt="enter image description here"></a></p>

<p>So, how can I put the wrappers in a reachable place to be tested by my python test files?</p>

<p>I am assuming that you want to import the libraries into your runme.py. In order to import python files in a sub-directory, that sub-directory needs an __init__.py file in it. You can place an empty one into your build folder. This should allow your program to import the files.</p>

<p>You could then use:</p>

<pre><code>import build.example
import build._example
</code></pre>

<p>This way it wouldn't matter if the files aren't in the same directory, assuming that there is not another reason you want them there.</p>

<p>Edit:</p>

<p>Since the build folder doesn't exist ahead of time you can always create and compile the __init__.py file in the runme.py file by doing:</p>

<pre><code>import os
import py_compile

dir_path = os.path.dirname(os.path.realpath(__file__))
open(dir_path + os.sep + "build" + os.sep + "__init__.py", "w+")
py_compile.compile(dir_path + os.sep + "build" + os.sep + "__init__.py")
py_compile.compile(dir_path + os.sep + "build" + os.sep + "example.py")

import build.example
import build._example
</code></pre>

<p>This will create the file you need and then be able to do the import without using CMake.</p>

python cmake swig