25

I have always wondered if it were possible to run PyPy in the Jupyter notebook. I recently tried installing PyPy on my local machine, and it ran really well - 100X speedup in an agent-based simulation written in pure Python. However, I miss the interactivity in the Jupyter notebook. Is it possible to make the IPython kernel use PyPy rather than CPython?

ericmjl
  • 13,541
  • 12
  • 51
  • 80

5 Answers5

18

Provided you have a system-wide / user installation of jupyter. You can follow:

pypy3 -m venv PyPy3
source PyPy3/bin/activate  # in POSIX, or...
PyPy3\Scripts\activate.bat  # in Windows
pypy3 -m pip install ipykernel
ipython kernel install --user --name=PyPy3

Now exit the virtual environment and verify installation:

jupyter kernelspec list

Open Jupyter notebook or lab interface.

jadelord
  • 1,511
  • 14
  • 19
  • 1
    Confirmed to work with macOS 10.14.4 and pypy3 7.1.1-beta0 from Homebrew. Jupyter was installed from Anaconda installer and launched with Anaconda-Navigator. Thank you. – noɥʇʎԀʎzɐɹƆ Sep 28 '19 at 18:11
  • 1
    Thanks @jadelord. It should be the accepted answer! Simple and very convenient. – paugier Jan 28 '20 at 05:13
  • Thanks @paugier (small world ;) ). [You might also find these aliases useful](https://gist.github.com/ashwinvis/9de22a0190a16717691783d22c2d0a46#file-python_aliases-sh-L11-L16) – jadelord Feb 05 '20 at 12:38
  • 2021 UPDATE HERE (WINDOWS): I've used this code except I had an issue with the second line: source PyPy3/bin/activate I had to replace it with: PyPy3\Scripts\activate.bat Source: https://docs.python.org/3/library/venv.html – Mtrinidad May 24 '21 at 01:17
12

You can install Jupyter with pypy:

pypy-pip install jupyter

The are problems on Mac OS X. If the install fails complaining a about gnureadline. Try this:

pypy-pip install --no-deps jupyter

Than start with:

pypy-ipython notebook

My pypy-ipython looks like this:

#!/usr/local/bin/pypy

# -*- coding: utf-8 -*-
import re
import sys

from IPython import start_ipython

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

In a notebook:

In [1]: import sys

In [2]: sys.version

Out[2]:

'2.7.9 (295ee98b6928, May 31 2015, 07:28:49)\n[PyPy 2.6.0 with GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]'

The notebook requires Python 2.7 or 3.3+. PyPy for Python3.3 should be out soon.

My pypy-pip this executable file /usr/local/bin//pypy-pip with this content:

#!/usr/local/bin/pypy
# EASY-INSTALL-ENTRY-SCRIPT: 'pip','console_scripts','pip'
__requires__ = 'pip'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pip', 'console_scripts', 'pip')()
    )
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Thanks for the answer, Mike! How do I get the tool `pypy-pip`? I currently already have pip and setuptools installed for pypy and pypy3, set up in roughly the same way as the anaconda Python (in a separate directory under my home folder). – ericmjl Nov 23 '15 at 20:30
  • 3
    My apologies for posting this second one fast... but I realized I can do `pypy -m pip install package_name` or `pypy3 -m pip install package_name` only after posting my comment. – ericmjl Nov 23 '15 at 20:31
  • 1
    Added the content of `pypy-pip`. – Mike Müller Nov 23 '15 at 20:35
  • 1
    Doesn't this make the whole thing run under PyPy instead of just the kernel? – Cristian Ciupitu Mar 30 '16 at 02:36
  • 1
    Yes, here the whole things is the kernel. The OP wants to use PyPy in the Notebook. You can also [run different kernels with one Notebook server](http://stackoverflow.com/questions/30492623/using-both-python-2-x-and-python-3-x-in-ipython-notebook/30493155#30493155). – Mike Müller Mar 30 '16 at 06:27
  • 1
    @MikeMüller The link you shared doesn't mention pypy. Can you add an answer here that gives details on how to just add a pypy kernel capability to an existing non-pypy jupyter installation? – nealmcb Jan 21 '19 at 00:37
2

Maybe we can follow the official doc:

pypy3 -m pip install ipykernel
pypy3 -m ipykernel install --user
PaleNeutron
  • 2,543
  • 4
  • 25
  • 43
1

The first answer worked great for me on Ubuntu, but did not work on Fedora - zeromq failed to compile

The following worked for me, based on the first answer:

$ cd <portable-pypy-directory>
$ bin/python3 virtualenv/virtualenv.py <new-venv-dir>
$ <new-venv-dir>/bin/activate
$ pip install jupyter
$ ipython kernel install --user --name=PyPy3
Zeev
  • 11
  • 2
  • For a year or two portable pypy is not needed unless you want older versions. The downloads at https://downloads.python.org/pypy/ are already "portable", and for an even better experience use conda since they compile packages for linux (x86 and arm64) and macOS, and hopefully soon for windows – mattip May 26 '21 at 16:08
  • https://conda-forge.org/blog/posts/2020-03-10-pypy/ – mattip May 26 '21 at 16:08
0

(Tested in Ubuntu Linux)
To install pypy, make a virtual environment and run jupyter notebook as with python:
Download pypy version (3.8 example here)
in a terminal run following commands to decompress

tar xf pypy3.8-v7.3.9-linux64.tar.bz2 

upgrade pip and wheel

./pypy3.8-v7.3.9-linux64/bin/pypy -m ensurepip --default-pip
./pypy3.8-v7.3.9-linux64/bin/pypy -mpip install -U pip wheel

create virtual env the following way is crucial

./pypy3.8-v7.3.9-linux64/bin/pypy -m venv my-pypy-env   

activate environment

source my-pypy-env/bin/activate

pip install jupyter and any other library

pip install jupyter

open jupyter as always

jupyter notebook
Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55