# pypy / pypy / doc / getting-started-python.rst

 David Malcolm 1e46012 2011-03-14 Maciej Fijalkows… b664434 2013-03-27 David Malcolm 1e46012 2011-03-14 lac 858eaea 2011-04-28 David Malcolm 1e46012 2011-03-14 Dario Bertini 5d78885 2011-04-25 David Malcolm 1e46012 2011-03-14 lac 07cd574 2011-04-29 Carl Friedrich B… 36db01d 2011-04-30 David Malcolm 1e46012 2011-03-14 lac 27092ba 2011-08-16 David Malcolm 1e46012 2011-03-14 lac 07cd574 2011-04-29 Carl Friedrich B… 36db01d 2011-04-30 lac 07cd574 2011-04-29 David Malcolm 1e46012 2011-03-14 David Malcolm 49ee2df 2011-03-14 Alex Gaynor ff887e0 2013-04-28 Ronny Pfannschmi… 3ad80bf 2011-03-22 David Malcolm 49ee2df 2011-03-14 Armin Rigo 5c3b546 2012-05-24 David Malcolm 49ee2df 2011-03-14 Matti Picus 0bd57f2 2013-04-05 Ronny Pfannschmi… 3ad80bf 2011-03-22 David Malcolm 49ee2df 2011-03-14 David Malcolm 1e46012 2011-03-14 Matti Picus 13d908d 2013-04-05 David Malcolm 1e46012 2011-03-14 David Malcolm 49ee2df 2011-03-14 lac 858eaea 2011-04-28 David Malcolm 1e46012 2011-03-14 Carl Friedrich B… 36db01d 2011-04-30 lac 07cd574 2011-04-29 David Malcolm 1e46012 2011-03-14 lac 07cd574 2011-04-29 Carl Friedrich B… 36db01d 2011-04-30 David Malcolm 1e46012 2011-03-14 lac 07cd574 2011-04-29 David Malcolm 1e46012 2011-03-14 lac 07cd574 2011-04-29 David Malcolm 1e46012 2011-03-14 lac 07cd574 2011-04-29 David Malcolm 1e46012 2011-03-14 liquibits 0941291 2013-01-23 David Malcolm 1e46012 2011-03-14 Armin Rigo e46fd79 2013-07-26 David Malcolm 1e46012 2011-03-14 Maciej Fijalkows… 0cd89b0 2012-11-22 Maciej Fijalkows… 1306259 2013-05-09 David Malcolm 1e46012 2011-03-14 Maciej Fijalkows… a0aabbd 2012-06-08 David Malcolm 1e46012 2011-03-14 Maciej Fijalkows… 5e8590c 2012-02-09 David Malcolm 1e46012 2011-03-14 Maciej Fijalkows… 5e8590c 2012-02-09 David Malcolm 1e46012 2011-03-14 Carl Friedrich B… 36db01d 2011-04-30 David Malcolm 1e46012 2011-03-14 Armin Rigo 365c988 2011-08-28 David Malcolm 1e46012 2011-03-14 Carl Friedrich B… 36db01d 2011-04-30 David Malcolm 1e46012 2011-03-14 Antonio Cuni 26626ef 2011-05-13 David Malcolm 1e46012 2011-03-14 Antonio Cuni 26626ef 2011-05-13 Amaury Forgeot d… a8cd905 2012-09-07 Antonio Cuni 26626ef 2011-05-13 Maciej Fijalkows… 1306259 2013-05-09 Antonio Cuni 26626ef 2011-05-13 David Malcolm 1e46012 2011-03-14 Alex Perry b74c0e8 2011-03-22 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Carl Friedrich B… 36db01d 2011-04-30 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Alexander Hesse 5770ebe 2013-01-05 David Malcolm 1e46012 2011-03-14 Armin Rigo e9e7c0e 2011-05-07 David Malcolm 1e46012 2011-03-14 Carl Friedrich B… 541308f 2011-04-27  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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 ============================================== Getting Started with PyPy's Python Interpreter ============================================== .. contents:: PyPy's Python interpreter is a very compliant Python interpreter implemented in RPython. When compiled, it passes most of CPythons core language regression tests_ and comes with many of the extension modules included in the standard library including ctypes. It can run large libraries such as Django_ and Twisted_. There are some small behavioral differences with CPython and some missing extensions, for details see CPython differences_. .. _Django: http://djangoproject.com .. _Twisted: http://twistedmatrix.com .. _CPython differences: cpython_differences.html To actually use PyPy's Python interpreter, the first thing to do is to download a pre-built PyPy_ for your architecture. .. _download a pre-built PyPy: http://pypy.org/download.html Translating the PyPy Python interpreter --------------------------------------- (**Note**: for some hints on how to translate the Python interpreter under Windows, see the windows document_) .. _windows document: windows.html You can translate the whole of PyPy's Python interpreter to low level C code, or CLI code_. If you intend to build using gcc, check to make sure that the version you have is not 4.2 or you will run into this bug_. .. _this bug: https://bugs.launchpad.net/ubuntu/+source/gcc-4.2/+bug/187391 1. First download a pre-built PyPy_ for your architecture which you will use to translate your Python interpreter. It is, of course, possible to translate with a CPython 2.6 or later, but this is not the preferred way, because it will take a lot longer to run -- depending on your architecture, between two and three times as long. 2. Install build-time dependencies. On a Debian box these are:: [user@debian-box ~]$sudo apt-get install \ gcc make python-dev libffi-dev libsqlite3-dev pkg-config \ libz-dev libbz2-dev libncurses-dev libexpat1-dev \ libssl-dev libgc-dev python-sphinx python-greenlet On a Fedora-16 box these are:: [user@fedora-or-rh-box ~]$ sudo yum install \ gcc make python-devel libffi-devel lib-sqlite3-devel pkgconfig \ zlib-devel bzip2-devel ncurses-devel expat-devel \ openssl-devel gc-devel python-sphinx python-greenlet The above command lines are split with continuation characters, giving the necessary dependencies first, then the optional ones. * pkg-config (to help us locate libffi files) * libz-dev (for the optional zlib module) * libbz2-dev (for the optional bz2 module) * libsqlite3-dev (for the optional sqlite3 module via cffi) * libncurses-dev (for the optional _minimal_curses module) * libexpat1-dev (for the optional pyexpat module) * libssl-dev (for the optional _ssl module) * libgc-dev (for the Boehm garbage collector: only needed when translating with --opt=0, 1 or size) * python-sphinx (for the optional documentation build. You need version 1.0.7 or later) 3. Translation is time-consuming -- 45 minutes on a very fast machine -- and RAM-hungry. As of March 2011, you will need **at least** 2 GB of memory on a 32-bit machine and 4GB on a 64-bit machine. If your memory resources are constrained, or your machine is slow you might want to pick the optimization level_ 1 in the next step. A level of 2 or 3 or jit gives much better results, though. But if all you want to do is to test that some new feature that you just wrote translates, level 1 is enough. Let me stress this again: at --opt=1 you get the Boehm GC, which is here mostly for historical and for testing reasons. You really do not want to pick it for a program you intend to use. The resulting pypy-c is slow. 4. Run:: cd pypy/goal python ../../rpython/bin/rpython --opt=jit targetpypystandalone.py possibly replacing --opt=jit with another optimization level_ of your choice. Typical example: --opt=2 gives a good (but of course slower) Python interpreter without the JIT. .. _optimization level: config/opt.html If everything works correctly this will create an executable pypy-c in the current directory. Type pypy-c --help to see the options it supports - mainly the same basic options as CPython. In addition, pypy-c --info prints the translation options that where used to produce this particular executable. The executable behaves mostly like a normal Python interpreter:: \$ ./pypy-c Python 2.7.3 (7e4f0faa3d51, Nov 22 2012, 10:35:18) [PyPy 2.0.0 with GCC 4.7.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. And now for something completely different: RPython magically makes you rich and famous (says so on the tin)'' >>>> 46 - 4 42 >>>> from test import pystone >>>> pystone.main() Pystone(1.1) time for 50000 passes = 0.220015 This machine benchmarks at 227257 pystones/second >>>> pystone.main() Pystone(1.1) time for 50000 passes = 0.060004 This machine benchmarks at 833278 pystones/second >>>> Note that pystone gets faster as the JIT kicks in. This executable can be moved around or copied on other machines; see Installation_ below. The translate.py script takes a very large number of options controlling what to translate and how. See translate.py -h. The default options should be suitable for mostly everybody by now. Find a more detailed description of the various options in our configuration sections_. .. _configuration sections: config/index.html Translating with non-standard options ++++++++++++++++++++++++++++++++++++++++ It is possible to have non-standard features enabled for translation, but they are not really tested any more. Look, for example, at the objspace proxies_ document. .. _objspace proxies: objspace-proxies.html Installation ++++++++++++ A prebuilt pypy-c can be installed in a standard location like /usr/local/bin, although some details of this process are still in flux. It can also be copied to other machines as long as their system is "similar enough": some details of the system on which the translation occurred might be hard-coded in the executable. PyPy dynamically finds the location of its libraries depending on the location of the executable. The directory hierarchy of a typical PyPy installation looks like this:: ./bin/pypy ./include/ ./lib_pypy/ ./lib-python/2.7 ./site-packages/ The hierarchy shown above is relative to a PREFIX directory. PREFIX is computed by starting from the directory where the executable resides, and "walking up" the filesystem until we find a directory containing lib_pypy and lib-python/2.7. The archives (.tar.bz2 or .zip) containing PyPy releases already contain the correct hierarchy, so to run PyPy it's enough to unpack the archive, and run the bin/pypy executable. To install PyPy system wide on unix-like systems, it is recommended to put the whole hierarchy alone (e.g. in /opt/pypy2.0) and put a symlink to the pypy executable into /usr/bin or /usr/local/bin If the executable fails to find suitable libraries, it will report debug: WARNING: library path not found, using compiled-in sys.path and then attempt to continue normally. If the default path is usable, most code will be fine. However, the sys.prefix will be unset and some existing libraries assume that this is never the case. .. _pyinteractive.py interpreter: Running the Python Interpreter Without Translation --------------------------------------------------- The pyinteractive.py interpreter +++++++++++++++++++++ To start interpreting Python with PyPy, install a C compiler that is supported by distutils and use Python 2.5 or greater to run PyPy:: cd pypy python bin/pyinteractive.py After a few seconds (remember: this is running on top of CPython), you should be at the PyPy prompt, which is the same as the Python prompt, but with an extra ">". Now you are ready to start running Python code. Most Python modules should work if they don't involve CPython extension modules. **This is slow, and most C modules are not present by default even if they are standard!** Here is an example of determining PyPy's performance in pystones:: >>>> from test import pystone >>>> pystone.main(10) The parameter is the number of loops to run through the test. The default is 50000, which is far too many to run in a non-translated PyPy version (i.e. when PyPy's interpreter itself is being interpreted by CPython). pyinteractive.py options +++++++++++++ To list the PyPy interpreter command line options, type:: cd pypy python bin/pyinteractive.py --help pyinteractive.py supports most of the options that CPython supports too (in addition to a large amount of options that can be used to customize pyinteractive.py). As an example of using PyPy from the command line, you could type:: python pyinteractive.py -c "from test import pystone; pystone.main(10)" Alternatively, as with regular Python, you can simply give a script name on the command line:: python pyinteractive.py ../../lib-python/2.7/test/pystone.py 10 See our configuration sections_ for details about what all the commandline options do. .. _Mono: http://www.mono-project.com/Main_Page .. _CLI backend: cli-backend.html .. _Boehm-Demers-Weiser garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ .. _clr: clr-module.html .. _CPythons core language regression tests: http://buildbot.pypy.org/summary?category=applevel&branch=%3Ctrunk%3E .. include:: _ref.txt 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.