1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
Installation and use with setuptools
====================================
Since cppy is nothing else than a collection of header that are only compiled
when used, installing it is extremely straightforward using pip::
$ pip install cppy
If you want to run the development version, you can install directly from
Github::
$ pip install https://github.com/nucleic/cppy
Using cppy in an extensions
---------------------------
To use cppy in your extension (written in C++), you simply need to include it.
.. code:: c++
#include <cppy/cppy.h>
Cppy includes Python.h so when including cppy.h you do not need to also include
Python.h.
Every functions, classes exposed by cppy are stored in the `cppy` namespace.
.. code:: c++
cppy::ptr obj_ptr( PyUnicode_FromString("test") )
Use with setuptools
-------------------
Cppy is only needed during the installation step of the projects using it. The
following example setup.py script illustrates how to use cppy without requiring
it to be installed before `setup.py` is run.
.. code:: python
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
ext_modules = [
Extension(
'project',
['module.cpp],
include_dirs=['.'],
language='c++',
),
]
class BuildExt(build_ext):
def build_extensions(self):
# Delayed import of cppy to let setup_requires install it if
# necessary
import cppy
ct = self.compiler.compiler_type
for ext in self.extensions:
# cppy.get_include() collect the path of the header files
ext.include_dirs.insert(0, cppy.get_include())
build_ext.build_extensions(self)
setup(
name='project',
python_requires='>=3.5',
setup_requires=['cppy'],
ext_modules=ext_modules,
cmdclass={'build_ext': BuildExt},
)
|