|
| 1 | +.. _virtualenv-faq: |
| 2 | + |
| 3 | +*********************************************** |
| 4 | +Working with Matplotlib in Virtual environments |
| 5 | +*********************************************** |
| 6 | + |
| 7 | +.. contents:: |
| 8 | + :backlinks: none |
| 9 | + |
| 10 | + |
| 11 | +.. _introduction: |
| 12 | + |
| 13 | +Introduction |
| 14 | +============ |
| 15 | + |
| 16 | +When running :mod:`matplotlib` in a virtual environment you may discover a |
| 17 | +few issues. :mod:`matplotlib` itself works problemless in virtual envirnments. |
| 18 | +However, the GUI frameworks that :mod:`matplotlib` depends uses for interactive |
| 19 | +figures have some issues with virtual environments. Everything below assumes |
| 20 | +some familiarity with the Matplotlib backends as found in :ref:`What is a |
| 21 | +backend? <what-is-a-backend>`. |
| 22 | + |
| 23 | +If you only use the ``IPython/Jupyter`` inline and ``notebook/nbagg`` backends |
| 24 | +and non interactive backends you can should not have any isseus and can ignore |
| 25 | +everything below. |
| 26 | + |
| 27 | +GUI Frameworks |
| 28 | +============== |
| 29 | + |
| 30 | +Interactive Matplotlib relies heavily on the interaction with external GUI |
| 31 | +frameworks. |
| 32 | + |
| 33 | +Most GUI frameworks are not pip installable. This makes it tricky to install |
| 34 | +them within a virtual environment. This problem does not exist if you use Conda |
| 35 | +environments where you can install all Conda supported GUI frameworks directly |
| 36 | +into tge environment. In regular virtualenv environment various workarounds |
| 37 | +exist. Some of these are given here: |
| 38 | + |
| 39 | +The ``TKAgg`` backend doesn't require any external dependencies and is normally |
| 40 | +always available. The ``QT4`` framework ``PySide`` and the upcoming `WX Phoenix |
| 41 | +<http://wiki.wxpython.org/ProjectPhoenix>`_ toolkit are ``pip`` installable |
| 42 | +and can be used directly within a virtual environment. |
| 43 | + |
| 44 | +Other frameworks are harder to install into a virtual environment. There are at |
| 45 | +least two possible ways to get access to these in a virtual environment. |
| 46 | + |
| 47 | +You can pass the ``--system-site-packages`` option to virtualenv when creating |
| 48 | +an environment. This adds all system wide packages to the virtual environment. |
| 49 | +However, this breaks the isolation between the virtual environment and the |
| 50 | +system install. If you use `virtualenvwrapper |
| 51 | +<https://virtualenvwrapper.readthedocs.org/>`_ this can be toggled with the |
| 52 | +toggleglobalsitepackages command. |
| 53 | + |
| 54 | +Alternatively you can manually symlink the GUI frameworks into the environment. |
| 55 | +I.e. to use PyQt5 you should symlink ``PyQt5`` and ``sip`` from your system |
| 56 | +site packages directory into the environment taking care that the environment |
| 57 | +and systemwide install uses the same python version. |
| 58 | + |
| 59 | +OSX |
| 60 | +=== |
| 61 | + |
| 62 | +On OSX two different types of Python Builds exists. A regular build and a |
| 63 | +framework build. In order to interact correctly with OSX through some |
| 64 | +gui frameworks you need a framework build of Python. |
| 65 | +At the time of writing the ``macosx``, ``WX`` and ``WXAgg`` backends requires a |
| 66 | +framework build to function correctly. Unfortunately virtualenv creates a non |
| 67 | +framework build even if created from a framework build of Python. From |
| 68 | +Matplotlib 1.5 onwards the ``macosx`` backend checks that a framework build is |
| 69 | +available and fails if a non framework build is found. |
| 70 | + |
| 71 | +The issue has been reported on the virualenv bug tracker `here |
| 72 | +<https://github.com/pypa/virtualenv/issues/54>`__ and `here |
| 73 | +<https://github.com/pypa/virtualenv/issues/609>`__ |
| 74 | + |
| 75 | +Until this is fixed a work around is needed. The best knows work around, |
| 76 | +borrowed from the `WX wiki |
| 77 | +<http://wiki.wxpython.org/wxPythonVirtualenvOnMac>`_, is to use the non |
| 78 | +virualenv python along with the PYTHONHOME environmental variable. This can be |
| 79 | +implemented in a script as below. To use this modify ``PYVER`` and |
| 80 | +``PATHTOPYTHON`` and put the script in the virtualenvs bin directory i.e. |
| 81 | +``PATHTOVENV/bin/frameworkpython`` |
| 82 | + |
| 83 | +.. code:: bash |
| 84 | +
|
| 85 | + #!/bin/bash |
| 86 | +
|
| 87 | + # what real Python executable to use |
| 88 | + PYVER=2.7 |
| 89 | + PATHTOPYTHON=/usr/local/bin/ |
| 90 | + PYTHON=${PATHTOPYTHON}python${PYVER} |
| 91 | +
|
| 92 | + # find the root of the virtualenv, it should be the parent of the dir this script is in |
| 93 | + ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` |
| 94 | +
|
| 95 | + # now run Python with the virtualenv set as Python's HOME |
| 96 | + export PYTHONHOME=$ENV |
| 97 | + exec $PYTHON "$@" |
| 98 | +
|
| 99 | +
|
| 100 | +With this in place you can run ``frameworkpython`` to get a interactive |
| 101 | +framework build within the virtualenv. To run a script you can do |
| 102 | +``frameworkpython test.py`` where ``test.py`` is a script that requires a |
| 103 | +framework build. To run an interactive ``IPython`` session with the framework |
| 104 | +build within the virtual environment you can do ``frameworkpython -m IPython`` |
0 commit comments