Debian Codegraph @

mypyc/doc/getting_started.rst

raw
1 Getting started
2 ===============
3
4 Here you will learn some basic things you need to know to get started with mypyc.
5
6 Prerequisites
7 -------------
8
9 You need a Python C extension development environment. The way to set this up
10 depends on your operating system.
11
12 macOS
13 *****
14
15 Install Xcode command line tools:
16
17 .. code-block::
18
19 $ xcode-select --install
20
21 Linux
22 *****
23
24 You need a C compiler and CPython headers and libraries. The specifics
25 of how to install these varies by distribution. Here are instructions for
26 Ubuntu 18.04, for example:
27
28 .. code-block::
29
30 $ sudo apt install python3-dev
31
32 Windows
33 *******
34
35 From `Build Tools for Visual Studio 2022 <https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2022>`_, install MSVC C++ build tools for your architecture and a Windows SDK. (latest versions recommended)
36
37 Installation
38 ------------
39
40 Mypyc is shipped as part of the mypy distribution. Install mypy like
41 this (you need Python 3.10 or later):
42
43 .. code-block::
44
45 $ python3 -m pip install -U 'mypy[mypyc]'
46
47 On some systems you need to use this instead:
48
49 .. code-block::
50
51 $ python -m pip install -U 'mypy[mypyc]'
52
53 Example program
54 ---------------
55
56 Let's start with a classic micro-benchmark, recursive fibonacci. Save
57 this file as ``fib.py``:
58
59 .. code-block:: python
60
61 import time
62
63 def fib(n: int) -> int:
64 if n <= 1:
65 return n
66 else:
67 return fib(n - 2) + fib(n - 1)
68
69 t0 = time.time()
70 fib(32)
71 print(time.time() - t0)
72
73 Note that we gave the ``fib`` function a type annotation. Without it,
74 performance won't be as impressive after compilation.
75
76 .. note::
77
78 `Mypy documentation
79 <https://mypy.readthedocs.io/en/stable/index.html>`_ is a good
80 introduction if you are new to type annotations or mypy. Mypyc uses
81 mypy to perform type checking and type inference, so some familiarity
82 with mypy is very useful.
83
84 Compiling and running
85 ---------------------
86
87 We can run ``fib.py`` as a regular, interpreted program using CPython:
88
89 .. code-block:: console
90
91 $ python3 fib.py
92 0.4125328063964844
93
94 It took about 0.41s to run on my computer.
95
96 Run ``mypyc`` to compile the program to a binary C extension:
97
98 .. code-block:: console
99
100 $ mypyc fib.py
101
102 This will generate a C extension for ``fib`` in the current working
103 directory. For example, on a Linux system the generated file may be
104 called ``fib.cpython-37m-x86_64-linux-gnu.so``.
105
106 Since C extensions can't be run as programs, use ``python3 -c`` to run
107 the compiled module as a program:
108
109 .. code-block:: console
110
111 $ python3 -c "import fib"
112 0.04097270965576172
113
114 After compilation, the program is about 10x faster. Nice!
115
116 .. note::
117
118 ``__name__`` in ``fib.py`` would now be ``"fib"``, not ``"__main__"``.
119
120 You can also pass most
121 `mypy command line options <https://mypy.readthedocs.io/en/stable/command_line.html>`_
122 to ``mypyc``.
123
124 Deleting compiled binary
125 ------------------------
126
127 You can manually delete the C extension to get back to an interpreted
128 version (this example works on Linux):
129
130 .. code-block::
131
132 $ rm fib.*.so
133
134 Using setup.py
135 --------------
136
137 You can also use ``setup.py`` to compile modules using mypyc. Here is an
138 example ``setup.py`` file::
139
140 from setuptools import setup
141
142 from mypyc.build import mypycify
143
144 setup(
145 name='mylib',
146 packages=['mylib'],
147 ext_modules=mypycify([
148 'mylib/__init__.py',
149 'mylib/mod.py',
150 ]),
151 )
152
153 We used ``mypycify(...)`` to specify which files to compile using
154 mypyc. Your ``setup.py`` can include additional Python files outside
155 ``mypycify(...)`` that won't be compiled.
156
157 Now you can build a wheel (.whl) file for the package::
158
159 python3 setup.py bdist_wheel
160
161 The wheel is created under ``dist/``.
162
163 You can also compile the C extensions in-place, in the current directory (similar
164 to using ``mypyc`` to compile modules)::
165
166 python3 setup.py build_ext --inplace
167
168 You can include most `mypy command line options
169 <https://mypy.readthedocs.io/en/stable/command_line.html>`_ in the
170 list of arguments passed to ``mypycify()``. For example, here we use
171 the ``--disallow-untyped-defs`` flag to require that all functions
172 have type annotations::
173
174 ...
175 setup(
176 name='frobnicate',
177 packages=['frobnicate'],
178 ext_modules=mypycify([
179 '--disallow-untyped-defs', # Pass a mypy flag
180 'frobnicate.py',
181 ]),
182 )
183
184 .. note:
185
186 You may be tempted to use `--check-untyped-defs
187 <https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-check-untyped-defs>`_
188 to type check functions without type annotations. Note that this
189 may reduce performance, due to many transitions between type-checked and unchecked
190 code.
191
192 Recommended workflow
193 --------------------
194
195 A simple way to use mypyc is to always compile your code after any
196 code changes, but this can get tedious, especially if you have a lot
197 of code. Instead, you can do most development in interpreted mode.
198 This development workflow has worked smoothly for developing mypy and
199 mypyc (often we forget that we aren't working on a vanilla Python
200 project):
201
202 1. During development, use interpreted mode. This gives you a fast
203 edit-run cycle.
204
205 2. Use type annotations liberally and use mypy to type check your code
206 during development. Mypy and tests can find most errors that would
207 break your compiled code, if you have good type annotation
208 coverage. (Running mypy is pretty quick.)
209
210 3. After you've implemented a feature or a fix, compile your project
211 and run tests again, now in compiled mode. Usually nothing will
212 break here, assuming your type annotation coverage is good. This
213 can happen locally or in a Continuous Integration (CI) job. If you
214 have CI, compiling locally may be rarely needed.
215
216 4. Release or deploy a compiled version. Optionally, include a
217 fallback interpreted version for platforms that mypyc doesn't
218 support.
219
220 This mypyc workflow only involves minor tweaks to a typical Python
221 workflow. Most of development, testing and debugging happens in
222 interpreted mode. Incremental mypy runs, especially when using the
223 mypy daemon, are very quick (often a few hundred milliseconds).
224
225 Next steps
226 ----------
227
228 You can sometimes get good results by just annotating your code and
229 compiling it. If this isn't providing meaningful performance gains, if
230 you have trouble getting your code to work under mypyc, or if you want
231 to optimize your code for maximum performance, you should read the
232 rest of the documentation in some detail.
233
234 Here are some specific recommendations, or you can just read the
235 documentation in order:
236
237 * :ref:`using-type-annotations`
238 * :ref:`native-classes`
239 * :ref:`differences-from-python`
240 * :ref:`performance-tips`