-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve virtualenv and framework builds #7022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfb1960
Document the use of pythonw in the docs
jenshnielsen 9e09425
wip update virtualenv faq
jenshnielsen 92cc18e
refresh docs
jenshnielsen fea3d54
point to osx faq rather than venv faq
jenshnielsen c223990
Typo
jenshnielsen fc4744d
Remove mention of wx phoenix. We will readd it when there is an offic…
jenshnielsen 00f7ab5
Add ref in long version to short version
jenshnielsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ The Matplotlib FAQ | |
troubleshooting_faq.rst | ||
environment_variables_faq.rst | ||
virtualenv_faq.rst | ||
osx_framework.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
.. _osxframework-faq: | ||
|
||
****************************** | ||
Working with Matplotlib on OSX | ||
****************************** | ||
|
||
.. contents:: | ||
:backlinks: none | ||
|
||
|
||
.. _osxframework_introduction: | ||
|
||
Introduction | ||
============ | ||
|
||
On OSX, two different types of Python Builds exist: a regular build and a | ||
framework build. In order to interact correctly with OSX through the native | ||
GUI frameworks you need a framework build of Python. | ||
At the time of writing the ``macosx`` and ``WXAgg`` backends require a | ||
framework build to function correctly. This can result in issues for | ||
a python installation not build as a framework and may also happen in | ||
virtual envs and when using (Ana)Conda. | ||
From Matplotlib 1.5 onwards the ``macosx`` backend | ||
checks that a framework build is available and fails if a non framework | ||
build is found. WX has a similar check build in. | ||
|
||
Without this check a partially functional figure is created. | ||
Among the issues with it is that it is produced in the background and | ||
cannot be put in front of any other window. Several solutions and work | ||
arounds exist see below. | ||
|
||
Short version | ||
============= | ||
|
||
VirtualEnv | ||
---------- | ||
|
||
If you are on Python 3, use | ||
`venv <https://docs.python.org/3/library/venv.html>`_ | ||
instead of `virtualenv <https://virtualenv.pypa.io/en/latest/>`_:: | ||
|
||
python -m venv my-virtualenv | ||
source my-virtualenv/bin/activate | ||
|
||
Otherwise you will need one of the workarounds below. | ||
|
||
Conda | ||
----- | ||
|
||
The default python provided in (Ana)Conda is not a framework | ||
build. However, the Conda developers have made it easy to install | ||
a framework build in both the main environment and in Conda envs. | ||
To use this install python.app ``conda install python.app`` and | ||
use ``pythonw`` rather than ``python`` | ||
|
||
|
||
Long version | ||
============ | ||
|
||
Unfortunately virtualenv creates a non | ||
framework build even if created from a framework build of Python. | ||
As documented above you can use venv as an alternative on Python 3. | ||
|
||
The issue has been reported on the virtualenv bug tracker `here | ||
<https://github.com/pypa/virtualenv/issues/54>`__ and `here | ||
<https://github.com/pypa/virtualenv/issues/609>`__ | ||
|
||
Until this is fixed, one of the following workarounds can be used: | ||
|
||
``PYTHONHOME`` Function | ||
----------------------- | ||
|
||
The best known work around is to use the non | ||
virtualenv python along with the PYTHONHOME environment variable. | ||
This can be done by defining a function in your ``.bashrc`` using | ||
|
||
.. code:: bash | ||
|
||
function frameworkpython { | ||
if [[ ! -z "$VIRTUAL_ENV" ]]; then | ||
PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python "$@" | ||
else | ||
/usr/local/bin/python "$@" | ||
fi | ||
} | ||
|
||
This function can then be used in all of your virtualenvs without having to | ||
fix every single one of them. | ||
|
||
With this in place you can run ``frameworkpython`` to get an interactive | ||
framework build within the virtualenv. To run a script you can do | ||
``frameworkpython test.py`` where ``test.py`` is a script that requires a | ||
framework build. To run an interactive ``IPython`` session with the framework | ||
build within the virtual environment you can do ``frameworkpython -m IPython`` | ||
|
||
``PYTHONHOME`` Script | ||
^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
An alternative work around borrowed from the `WX wiki | ||
<http://wiki.wxpython.org/wxPythonVirtualenvOnMac>`_, is to use the non | ||
virtualenv python along with the PYTHONHOME environment variable. This can be | ||
implemented in a script as below. To use this modify ``PYVER`` and | ||
``PATHTOPYTHON`` and put the script in the virtualenv bin directory i.e. | ||
``PATHTOVENV/bin/frameworkpython`` | ||
|
||
.. code:: bash | ||
|
||
#!/bin/bash | ||
|
||
# what real Python executable to use | ||
PYVER=2.7 | ||
PATHTOPYTHON=/usr/local/bin/ | ||
PYTHON=${PATHTOPYTHON}python${PYVER} | ||
|
||
# find the root of the virtualenv, it should be the parent of the dir this script is in | ||
ENV=`$PYTHON -c "import os; print(os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..')))"` | ||
|
||
# now run Python with the virtualenv set as Python's HOME | ||
export PYTHONHOME=$ENV | ||
exec $PYTHON "$@" | ||
|
||
With this in place you can run ``frameworkpython`` as above but will need to add this script | ||
to every virtualenv | ||
|
||
PythonW Compiler | ||
^^^^^^^^^^^^^^^^ | ||
|
||
In addition | ||
`virtualenv-pythonw-osx <https://github.com/gldnspud/virtualenv-pythonw-osx>`_ | ||
provides an alternative workaround which may be used to solve the issue. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth another mention of
venv
here in case someone skipped straight to the long version?