getting_started.rst 6.6 KB

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