| 1 |
======================================================= |
| 2 |
Installation and Status |
| 3 |
======================================================= |
| 4 |
|
| 5 |
Quick installation for CPython (cffi is distributed with PyPy): |
| 6 |
|
| 7 |
* ``pip install cffi`` |
| 8 |
|
| 9 |
* or get the source code via the `Python Package Index`__. |
| 10 |
|
| 11 |
.. __: http://pypi.python.org/pypi/cffi |
| 12 |
|
| 13 |
In more detail: |
| 14 |
|
| 15 |
This code has been developed on Linux, but should work on any POSIX |
| 16 |
platform as well as on Windows 32 and 64. (It relies occasionally on |
| 17 |
libffi, so it depends on libffi being bug-free; this may not be fully |
| 18 |
the case on some of the more exotic platforms.) |
| 19 |
|
| 20 |
CFFI is tested with CPython 3.10-3.15. |
| 21 |
|
| 22 |
The core speed of CFFI is better than ctypes, with import times being |
| 23 |
either lower if you use the post-1.0 features, or much higher if you |
| 24 |
don't. The wrapper Python code you typically need to write around the |
| 25 |
raw CFFI interface slows things down on CPython, but not unreasonably |
| 26 |
so. On PyPy, this wrapper code has a minimal impact thanks to the JIT |
| 27 |
compiler. This makes CFFI the recommended way to interface with C |
| 28 |
libraries on PyPy. |
| 29 |
|
| 30 |
Requirements: |
| 31 |
|
| 32 |
* CPython 3.10+, or PyPy (PyPy 2.0 for the earliest |
| 33 |
versions of CFFI; or PyPy 2.6 for CFFI 1.0). |
| 34 |
|
| 35 |
* in some cases you need to be able to compile C extension modules. |
| 36 |
On non-Windows platforms, this usually means installing the package |
| 37 |
``python-dev``. Refer to the appropriate docs for your OS. |
| 38 |
|
| 39 |
* on CPython, on non-Windows platforms, you also need to install |
| 40 |
``libffi-dev`` in order to compile CFFI itself. |
| 41 |
|
| 42 |
* pycparser >= 2.06: https://github.com/eliben/pycparser (automatically |
| 43 |
tracked by ``pip install cffi``). |
| 44 |
|
| 45 |
* `pytest`_ is needed to run the tests of CFFI itself. |
| 46 |
|
| 47 |
.. _`pytest`: http://pypi.python.org/pypi/pytest |
| 48 |
|
| 49 |
Download and Installation: |
| 50 |
|
| 51 |
* https://pypi.python.org/pypi/cffi |
| 52 |
|
| 53 |
* Or grab the most current version from `GitHub`_: |
| 54 |
``git clone https://github.com/python-cffi/cffi`` |
| 55 |
|
| 56 |
* running the tests: ``pytest c/ testing/`` (if you didn't |
| 57 |
install cffi yet, you need to first run ``python setup_base.py build_ext -f |
| 58 |
-i``) |
| 59 |
|
| 60 |
.. _`GitHub`: https://github.com/python-cffi/cffi |
| 61 |
|
| 62 |
Demos: |
| 63 |
|
| 64 |
* The `demo`_ directory contains a number of small and large demos |
| 65 |
of using ``cffi``. |
| 66 |
|
| 67 |
* The documentation below might be sketchy on details; for now the |
| 68 |
ultimate reference is given by the tests, notably |
| 69 |
`testing/cffi1/test_verify1.py`_ and `testing/cffi0/backend_tests.py`_. |
| 70 |
|
| 71 |
.. _`demo`: https://github.com/python-cffi/cffi/blob/main/demo |
| 72 |
.. _`testing/cffi1/test_verify1.py`: https://github.com/python-cffi/cffi/blob/main/testing/cffi1/test_verify1.py |
| 73 |
.. _`testing/cffi0/backend_tests.py`: https://github.com/python-cffi/cffi/blob/main/testing/cffi0/backend_tests.py |
| 74 |
|
| 75 |
|
| 76 |
Platform-specific instructions |
| 77 |
------------------------------ |
| 78 |
|
| 79 |
``libffi`` is notoriously messy to install and use --- to the point that |
| 80 |
CPython includes its own copy to avoid relying on external packages. |
| 81 |
CFFI does the same for Windows, but not for other platforms (which should |
| 82 |
have their own working libffi). |
| 83 |
Modern Linux distributions work out of the box thanks to ``pkg-config``. Here are some |
| 84 |
(user-supplied) instructions for other platforms. |
| 85 |
|
| 86 |
|
| 87 |
MacOS X |
| 88 |
+++++++ |
| 89 |
|
| 90 |
**Homebrew** (Thanks David Griffin and Mark Keller for this) |
| 91 |
|
| 92 |
1) Install homebrew: http://brew.sh |
| 93 |
|
| 94 |
2) Run the following commands in a terminal |
| 95 |
|
| 96 |
:: |
| 97 |
|
| 98 |
brew install pkg-config libffi |
| 99 |
PKG_CONFIG_PATH=$(brew --prefix libffi)/lib/pkgconfig pip install --no-binary cffi cffi |
| 100 |
|
| 101 |
(the ``--no-binary cffi`` might be needed or not.) |
| 102 |
|
| 103 |
|
| 104 |
Alternatively, **on OS/X 10.6** (Thanks Juraj Sukop for this) |
| 105 |
|
| 106 |
For building libffi you can use the default install path, but then, in |
| 107 |
``setup.py`` you need to change:: |
| 108 |
|
| 109 |
include_dirs = [] |
| 110 |
|
| 111 |
to:: |
| 112 |
|
| 113 |
include_dirs = ['/usr/local/lib/libffi-3.0.11/include'] |
| 114 |
|
| 115 |
Then running ``python setup.py build`` complains about "fatal error: error writing to -: Broken pipe", which can be fixed by running:: |
| 116 |
|
| 117 |
ARCHFLAGS="-arch i386 -arch x86_64" python setup.py build |
| 118 |
|
| 119 |
as described here_. |
| 120 |
|
| 121 |
.. _here: http://superuser.com/questions/259278/python-2-6-1-pycrypto-2-3-pypi-package-broken-pipe-during-build |
| 122 |
|
| 123 |
|
| 124 |
Windows (32/64-bit) |
| 125 |
+++++++++++++++++++ |
| 126 |
|
| 127 |
Win32 and Win64 work and are tested at least each official release. |
| 128 |
|
| 129 |
The recommended C compiler compatible with Python 2.7 is this one: |
| 130 |
http://www.microsoft.com/en-us/download/details.aspx?id=44266 |
| 131 |
There is a known problem with distutils on Python 2.7, as |
| 132 |
explained in https://bugs.python.org/issue23246, and the same |
| 133 |
problem applies whenever you want to run compile() to build a dll with |
| 134 |
this specific compiler suite download. |
| 135 |
``import setuptools`` might help, but YMMV |
| 136 |
|
| 137 |
More generally, the solution that should always work is to download the |
| 138 |
sources of CFFI (instead of a prebuilt binary) and make sure that you |
| 139 |
build it with the same version of Python that will use it. |
| 140 |
For example, with virtualenv: |
| 141 |
|
| 142 |
* ``virtualenv ~/venv`` |
| 143 |
|
| 144 |
* ``cd ~/path/to/sources/of/cffi`` |
| 145 |
|
| 146 |
* ``~/venv/bin/python setup.py build --force`` # forcing a rebuild to |
| 147 |
make sure |
| 148 |
|
| 149 |
* ``~/venv/bin/python setup.py install`` |
| 150 |
|
| 151 |
This will compile and install CFFI in this virtualenv, using the |
| 152 |
Python from this virtualenv. |
| 153 |
|
| 154 |
|
| 155 |
NetBSD |
| 156 |
++++++ |
| 157 |
|
| 158 |
You need to make sure you have an up-to-date version of libffi, which |
| 159 |
fixes some bugs. |