Roland's homepage

My random knot in the Web

Switching IPython to Python 3

More and more, IPython has become my tool of choice as the ultimate calculator and to try out concepts and code for use in my programs.

While I generally use the terminal-based interactive shell, notebooks offer more capabilities for integrating text, formulas and graphics.

With Python 2.7 effectively mostly frozen except for some backports of 3.x features I figured it is time to start moving to Python 3. Most of my smaller scripts have already been ported, and my larger applications like lamprop will follow.

Prequisites

Until recently, FreeBSD’s package infrastructure couldn’t deal with installing the same Python package for multiple Python versions. This was fixed in the alpha versions of pkg 1.4.0 which at the time of writing of this article was contained in the ports-mgmt/pkg-devel port. After updating the package setting in /etc/make.conf,

WITH_PKG=devel

the following command was used to update the pkg program to the development version:

portmaster -o ports-mgmt/pkg-devel ports-mgmt/pkg

This update allowed me to install Python 3.4 version of some packages alongside Python 2.7 versions.

Nice to have

First among those that I installed for Python 3.4 was numpy. While not a requirement for IPython, I use it often.

As of now, I haven’t installed matplotlib for Python 3.4 yet. The reason for that is mostly a personal preference for gnuplot based on long-time familiarity with it.

Requirements

Using the FreeBSD package management tool pkg, the dependencies for IPython are as follows

> pkg info -d py27-ipython-2.3.0
py27-ipython-2.3.0:
        py27-tornado-4.0.2
        py27-pyzmq-14.3.1
        py27-pexpect-3.3
        python27-2.7.8_6
        gcc-4.8.3_2
        py27-setuptools27-5.5.1
        py27-Jinja2-2.7.3
        py27-sqlite3-2.7.8_5

This IPython is compiled with the following options

_OPTIONS_READ=py27-ipython-2.2.0
_FILE_COMPLETE_OPTIONS_LIST=EXAMPLES HTML_NOTEBOOK X11 ZMQ
OPTIONS_FILE_UNSET+=EXAMPLES
OPTIONS_FILE_SET+=HTML_NOTEBOOK
OPTIONS_FILE_UNSET+=X11
OPTIONS_FILE_SET+=ZMQ

We examine the dependencies of the packages that IPython uses

> pkg info -r py27-tornado-4.0.2 py27-pyzmq-14.3.1 py27-pexpect-3.3 py27-Jinja2-2.7.3 py27-sqlite3-2.7.8_5
py27-tornado-4.0.2:
        py27-ipython-2.3.0
py27-pyzmq-14.3.1:
        py27-ipython-2.3.0
py27-pexpect-3.3:
        py27-ipython-2.3.0
py27-Jinja2-2.7.3:
        pelican-3.3.0_1
        py27-sphinx-1.2.3
        py27-ipython-2.3.0
py27-sqlite3-2.7.8_5:
        py27-ipython-2.3.0

Only the Jinja package is used by other installed programs. The other dependencies are only used by IPython. Let’s see which ports are the origin of these packages:

> pkg info -o py27-tornado-4.0.2 py27-pyzmq-14.3.1 py27-pexpect-3.3 py27-Jinja2-2.7.3 py27-sqlite3-2.7.8_5
py27-tornado-4.0.2             www/py-tornado
py27-pyzmq-14.3.1              net/py-pyzmq
py27-pexpect-3.3               misc/py-pexpect
py27-Jinja2-2.7.3              devel/py-Jinja2
py27-sqlite3-2.7.8_5           databases/py-sqlite3

Preparations

Now we need to check if all these packages support Python 3.4. Looking at the port Makefiles, all of them should build with Python 3.4. Before installing the new ports, we remove the Python 2.7 versions of IPython and those ports that solely depend upon it:

# pkg delete py27-ipython-2.3.0 py27-tornado-4.0.2 py27-pyzmq-14.3.1 py27-pexpect-3.3
Checking integrity... done (0 conflicting)
Deinstallation has been requested for the following 4 packages (of 0 packages in the universe):

Installed packages to be REMOVED:
        py27-ipython-2.3.0
        py27-tornado-4.0.2
        py27-pyzmq-14.3.1
        py27-pexpect-3.3

The operation will free 22 MB.

Proceed with deinstalling packages? [y/N]: y
[1/4] Deinstalling py27-ipython-2.3.0...
[1/4] Deleting files for py27-ipython-2.3.0: 100%
[2/4] Deinstalling py27-tornado-4.0.2...
[2/4] Deleting files for py27-tornado-4.0.2: 100%
[3/4] Deinstalling py27-pyzmq-14.3.1...
[3/4] Deleting files for py27-pyzmq-14.3.1: 100%
[4/4] Deinstalling py27-pexpect-3.3...
[4/4] Deleting files for py27-pexpect-3.3: 100%

The sqlite package for Python 2.7 is left because it is useful.

Installation

Installing these requirements for Python 3.4 is done by running the command

make PYTHON_VERSION=python3.4 install clean

in each of the port directories www/py-tornado, net/py-pyzmq, misc/py-pexpect, devel/py-Jinja2 and databases/py-sqlite3.

After that the same make command is run in /usr/ports/devel/ipython. As mentioned before, I also installed the port math/py-numpy for Python 3.4.

Configuration

My ~/.ipython/profile_default/ipython_config.py used to import true division for Python 2.7. Since this is the standard for Python 3.4, this is not necessary anymore. The configuration was changed:

diff --git a/shared/ipython_config.py b/shared/ipython_config.py
index e921e92..f5914e6 100644
--- a/shared/ipython_config.py
+++ b/shared/ipython_config.py
@@ -4,8 +4,7 @@

c = get_config()

-c.InteractiveShellApp.exec_lines = ['from __future__ import division',
-                                    '%precision %.4g', '%clear']
+c.InteractiveShellApp.exec_lines = ['%precision %.4g', '%clear']
c.TerminalIPythonApp.display_banner = False
c.TerminalInteractiveShell.automagic = False
c.TerminalInteractiveShell.confirm_exit = False

←  Automated local backups Updating FreeBSD from source  →