{"id":148382,"date":"2023-10-18T16:47:30","date_gmt":"2023-10-18T20:47:30","guid":{"rendered":"http:\/\/www.bu.edu\/tech\/?page_id=148382"},"modified":"2024-01-31T09:49:36","modified_gmt":"2024-01-31T14:49:36","slug":"pypy","status":"publish","type":"page","link":"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/special\/pypy\/","title":{"rendered":"PyPy"},"content":{"rendered":"<p><a name=\"top\"><\/a><br \/>\n<a href=\"https:\/\/www.pypy.org\/\">PyPy<\/a> is an alternate implementation of the Python interpreter. The regular Python interpreter (nicknamed &#8220;CPython&#8221;) is written in the C language.\u00a0 The PyPy interpreter is written in a dialect of Python (&#8220;RPython&#8221;) and includes a number of interesting features, including a just-in-time compiler that makes it faster than the CPython interpreter when running Python code.<\/p>\n<p><strong>Sections<\/strong><\/p>\n<ul>\n<li><a href=\"#features\">Features of PyPy<\/a><\/li>\n<li><a href=\"#install\">Install PyPy<\/a><\/li>\n<li><a href=\"#use\">When to use PyPy<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><a name=\"features\"><\/a><\/p>\n<h2>Features<\/h2>\n<ul>\n<li>Supports Python versions 2.7, 3.9, and 3.10<\/li>\n<li>On average 4.8x faster than the regular CPython interpreter for Python code<\/li>\n<li>Reduced memory usage compared with CPython<\/li>\n<li>Compatibility with <em>most<\/em> existing Python libraries.<\/li>\n<li>Support for <a href=\"https:\/\/github.com\/stackless-dev\/stackless\/wiki\/\"><em>stackless<\/em><\/a>, a Python threading system that allows for hundreds (or more) of threads to be effectively used in a program.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><a name=\"install\"><\/a><\/p>\n<h2>Install PyPy<\/h2>\n<p>The PyPy website has <a href=\"https:\/\/www.pypy.org\/download.html\">pre-compiled binaries<\/a> that can be downloaded and installed into your SCC project storage. However, The easiest way to try out PyPy is to <a href=\"https:\/\/conda-forge.org\/docs\/user\/tipsandtricks.html#using-pypy-as-an-interpreter\">create a conda environment<\/a> for it. Installing libraries (such as numpy, pandas, etc.) through conda will then make sure that versions of libraries compiled specifically for PyPy are installed when available. The highest Python version supported in conda environments is 3.9.\u00a0 As an example, here is how to create a conda environment on the SCC using PyPy for Python 3.9:<\/p>\n<ol>\n<li> Load a miniconda module:\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">module load miniconda\/23.5.2<\/span><\/code><\/pre>\n<\/li>\n<li> Create a new conda environment named <code style=\"color:red\">my-pypy-env<\/code>, specifying the default channel to make sure only compatible PyPy packages are found:\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">conda create -c conda-forge -n <span class=\"placeholder\">my-pypy-env<\/span> pypy python=3.9<\/span><\/code><\/pre>\n<\/li>\n<li> Activate the new conda environment:<\/li>\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">conda activate <span class=\"placeholder\">my-pypy-env<\/span><\/span><\/code><\/pre>\n<\/li>\n<li> Configure this environment to use strict channel priority so that conda-forge is always checked first for packages. This is a one-time setting:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my-pypy-env) [rcs@scc1 ~]<\/span> <span class=\"command\">conda config --env --set channel_priority strict<\/span><\/code><\/pre>\n<\/li>\n<li> Install PyPy compatible libraries:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my-pypy-env) [rcs@scc1 ~]<\/span> <span class=\"command\">conda install -c conda-forge numpy scipy pandas jupyterlab scikit-learn<\/span><\/code><\/pre>\n<\/li>\n<li> Check the version of the Python interpreter:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my-pypy-env) [rcs@scc1 ~]<\/span> <span class=\"command\">python -V<\/span>\r\nPython 3.9.18 | packaged by conda-forge | (c5262994, Oct 26 2023, 08:28:20)\r\n[PyPy 7.3.13 with GCC 12.3.0]\r\n<\/span><\/code><\/pre>\n<\/li>\n<li> Run your Python code with PyPy:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my-pypy-env) [rcs@scc1 ~]<\/span> <span class=\"command\">python <span class=\"placeholder\">your_code.py<\/span><\/span><\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a name=\"use\"><\/a><\/p>\n<h2>When to use PyPy<\/h2>\n<p>PyPy runs Python code faster. If you are running a Python program with a lot of pure Python code, using Python classes, objects, lists, and so on it will likely run significantly faster when executed under PyPy. If your program extensively uses libraries with externally compiled code like pandas, numpy, or scikit-learn, it <em>may<\/em> run faster. The speedup will come from faster execution of Python code, while the calls to the external libraries will not be accelerated.<\/p>\n<p>There are some libraries that use compiled code that are not currently compatible with PyPy. The compatibility issue is based on the way the libraries communicate with the Python interpreter. Some popular libraries that are not compatible with PyPy include PyTorch, Tensorflow, and Numba.<\/p>\n<p>If your Python program does not use libraries that are incompatible with PyPy, then it is worth testing out PyPy to see if it provides a performance benefit. The test would consist of creating a conda environment as described above, installing any required libraries, and trying to run your Python program.<br \/>\n&nbsp;<\/p>\n<p><a href=\"#top\">Back to top<\/a><\/p>\n<p><script>\r\nconst date = new Date(document.lastModified);\r\ndocument.getElementById(\"demo\").innerHTML = date;\r\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyPy is an alternate implementation of the Python interpreter. The regular Python interpreter (nicknamed &#8220;CPython&#8221;) is written in the C language.\u00a0 The PyPy interpreter is written in a dialect of Python (&#8220;RPython&#8221;) and includes a number of interesting features, including a just-in-time compiler that makes it faster than the CPython interpreter when running Python code&#8230;.<\/p>\n","protected":false},"author":1692,"featured_media":0,"parent":148380,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148382"}],"collection":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/users\/1692"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/comments?post=148382"}],"version-history":[{"count":10,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148382\/revisions"}],"predecessor-version":[{"id":150189,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148382\/revisions\/150189"}],"up":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148380"}],"wp:attachment":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/media?parent=148382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}