@@ -7,135 +7,45 @@ Working with Matplotlib on OSX
77.. contents ::
88 :backlinks: none
99
10- .. highlight :: bash
11-
1210.. _osxframework_introduction :
1311
1412Introduction
1513============
1614
1715On OSX, two different types of Python builds exist: a regular build and a
1816framework build. In order to interact correctly with OSX through the native
19- GUI frameworks you need a framework build of Python. At the time of writing
17+ GUI frameworks, you need a framework build of Python. At the time of writing
2018the ``macosx `` and ``WXAgg `` backends require a framework build to function
21- correctly. This can result in issues for a Python installation not build as a
22- framework and may also happen in virtual envs and when using (Ana)Conda . From
19+ correctly. This can result in issues for a Python installation not build as a
20+ framework and may also happen in virtual envs and when using (Ana)conda . From
2321Matplotlib 1.5 onwards, both backends check that a framework build is available
24- and fail if a non framework build is found.
25-
26- Without this check a partially functional figure is created.
27- Among the issues with it is that it is produced in the background and
28- cannot be put in front of any other window. Several solutions and work
29- arounds exist see below.
22+ and fail if a non framework build is found. (Without this check a partially
23+ functional figure is created. In particular, it is produced in the background
24+ and cannot be put in front of any other window.)
3025
31- Short version
32- =============
33-
34- VirtualEnv
26+ virtualenv
3527----------
3628
37- If you are on Python 3, use
38- `venv <https://docs.python.org/3/library/venv.html >`_
39- instead of `virtualenv <https://virtualenv.pypa.io/en/latest/ >`_::
40-
41- python -m venv my-virtualenv
42- source my-virtualenv/bin/activate
29+ In a virtualenv _, a non-framework build is used even when the environment is
30+ created from a framework build (`virtualenv bug #54 `_, `virtualenv bug #609 `_).
4331
44- Otherwise you will need one of the workarounds below.
32+ The solution is to not use virtualenv, but instead the stdlib's venv _, which
33+ provides similar functionality but without exhibiting this issue.
4534
46- Pyenv
47- -----
48-
49- If you are using pyenv and virtualenv you can enable your python version to be installed as a framework::
35+ If you absolutely need to use virtualenv rather than venv, then from within
36+ the environment you can set the `` PYTHONHOME `` environment variable to
37+ `` $VIRTUAL_ENV ``, then invoke Python using the full path to the framework build
38+ (typically `` /usr/local/bin/ python``).
5039
51- PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install x.x.x
40+ .. _virtualenv : https://virtualenv.pypa.io/
41+ .. _virtualenv bug #54 : https://github.com/pypa/virtualenv/issues/54
42+ .. _virtualenv bug #609 : https://github.com/pypa/virtualenv/issues/609
43+ .. _venv : https://docs.python.org/3/library/venv.html
5244
53- Conda
45+ conda
5446-----
5547
56- The default python provided in (Ana)Conda is not a framework
57- build. However, the Conda developers have made it easy to install
58- a framework build in both the main environment and in Conda envs.
59- To use this install python.app ``conda install python.app `` and
60- use ``pythonw `` rather than ``python ``
61-
62-
63- Long version
64- ============
65-
66- Unfortunately virtualenv creates a non
67- framework build even if created from a framework build of Python.
68- As documented above you can use venv as an alternative on Python 3.
69-
70- The issue has been reported on the virtualenv bug tracker `here
71- <https://github.com/pypa/virtualenv/issues/54> `__ and `here
72- <https://github.com/pypa/virtualenv/issues/609> `__
73-
74- Until this is fixed, one of the following workarounds can be used:
75-
76- ``PYTHONHOME `` Function
77- -----------------------
78-
79- The best known work around is to use the non
80- virtualenv python along with the PYTHONHOME environment variable.
81- This can be done by defining a function in your ``.bashrc `` using ::
82-
83- function frameworkpython {
84- if [[ ! -z "$VIRTUAL_ENV" ]]; then
85- PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python "$@"
86- else
87- /usr/local/bin/python "$@"
88- fi
89- }
90-
91- This function can then be used in all of your virtualenvs without having to
92- fix every single one of them.
93-
94- With this in place you can run ``frameworkpython `` to get an interactive
95- framework build within the virtualenv. To run a script you can do
96- ``frameworkpython test.py `` where ``test.py `` is a script that requires a
97- framework build. To run an interactive ``IPython `` session with the framework
98- build within the virtual environment you can do ``frameworkpython -m IPython ``
99-
100- ``PYTHONHOME `` and Jupyter
101- ^^^^^^^^^^^^^^^^^^^^^^^^^^
102-
103- This approach can be followed even if using `Jupyter <https://jupyter.org/ >`_
104- notebooks: you just need to setup a kernel with the suitable ``PYTHONHOME ``
105- definition. The `jupyter-virtualenv-osx <https://github.com/mapio/jupyter-virtualenv-osx >`_
106- script automates the creation of such a kernel.
107-
108-
109- ``PYTHONHOME `` Script
110- ^^^^^^^^^^^^^^^^^^^^^
111-
112- An alternative work around borrowed from the `WX wiki
113- <https://wiki.wxpython.org/wxPythonVirtualenvOnMac> `_, is to use the non
114- virtualenv python along with the PYTHONHOME environment variable. This can be
115- implemented in a script as below. To use this modify ``PYVER `` and
116- ``PATHTOPYTHON `` and put the script in the virtualenv bin directory i.e.
117- ``PATHTOVENV/bin/frameworkpython `` ::
118-
119- #!/bin/bash
120-
121- # what real Python executable to use
122- PYVER=2.7
123- PATHTOPYTHON=/usr/local/bin/
124- PYTHON=${PATHTOPYTHON}python${PYVER}
125-
126- # find the root of the virtualenv, it should be the parent of the dir this script is in
127- ENV=`$PYTHON -c "import os; print(os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..')))"`
128-
129- # now run Python with the virtualenv set as Python's HOME
130- export PYTHONHOME=$ENV
131- exec $PYTHON "$@"
132-
133- With this in place you can run ``frameworkpython `` as above but will need to add this script
134- to every virtualenv
135-
136- PythonW Compiler
137- ----------------
138-
139- In addition
140- `virtualenv-pythonw-osx <https://github.com/gldnspud/virtualenv-pythonw-osx >`_
141- provides an alternative workaround which may be used to solve the issue.
48+ The default python provided in (Ana)conda is not a framework build. However,
49+ a framework build can easily be installed, both in the main environment and
50+ in conda envs: install python.app (``conda install python.app ``) and use
51+ ``pythonw `` rather than ``python ``
0 commit comments