Virtualenv: optionally create pythonw link in virtualenv for framework python builds on osx

Created on 14 Mar 2011  ·  34Comments  ·  Source: pypa/virtualenv

http://groups.google.com/group/python-virtualenv/browse_thread/thread/83fa4a12d22a30c8/744e19c194f1618a?#744e19c194f1618a details how wxpython might be enabled for python on OSX in virtualenvs by analogy with the way it currently works for win32 and cygwin installation modes.

Ian's previous discussion along these lines is here:
http://groups.google.com/group/python-virtualenv/browse_thread/thread/b119a3c26aa49238
and the code that does it at the moment is listed here:
http://github.com/gldnspud/virtualenv-pythonw-osx/

Can we get this baked into the core distribution? What would need to change about the approach?


enhancement

Most helpful comment

Try changing the matplotlib backend to get matplotlib to work in a virtual environment:

There are (at least) two solutions I've used, with some additional methods included below. Both outlined here should to the same thing with a slightly different approach:

If you already installed matplotlib using pip on your virtual environment, you can...

_1. Add a new matplotlibrc file for your virtual environment_
For example, in your virtual environment run:

$ cd ~/.matplotlib
$ nano matplotlibrc #to create file using nano

Write backend: TkAgg in the file and save upon exit. You should be good to go.

_OR_

_2. Edit the matplotlibrc file referenced by your virtual environment_
In your virtual environment, open a python shell and run:

import matplotlib
matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'

Note/Update: if you get a syntax error in your virtual environment, you might need to simply run:

import matplotlib
matplotlib.matplotlib_fname()

This displays the matplotlibrc file referenced by the matplotlib package in your virtual environment. Follow the path and open the matplotlibrc file. Edit the file's backend tag to read backend: TkAgg and save

[Also - _A third approach_]
Haven't tried before, but according to documentation you should be able to define the backend upon import of matplotlib if you don't want to change the backend in the script itself:

import matplotlib
matplotlib.use('TkAgg')

(see sources below for further explanation and additional methods)

Sources:
[1] http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
[2] http://matplotlib.org/users/customizing.html#customizing-matplotlib
[3] http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx
[4] http://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv

All 34 comments

note, possibly duplicates #47, but the issue there is a bit more muddier.


Original Comment By: dan mackinlay

+1 on this.

What is the ".Python" directory that wants linking in
http://github.com/gldnspud/virtualenv-pythonw-osx/ ? I don't see any .Python my venv.

Instead, I see this:

 (py27) $ laf /Users/glind/venvs/py27//include/python2.7
 /Users/glind/venvs/py27//include/python2.7@ -> /usr/local/Cellar/python/2.7.2/include/python2.7
 (py27) $ which python
 /Users/glind/venvs/py27/bin/python

This bug manifests in various ways:

http://stackoverflow.com/questions/3692928/why-doesnt-the-save-button-work-on-a-matplotlib-plot
http://code.google.com/p/iterm2/issues/detail?id=1680

The reason there was no .Python was because in my initial I was venving from a brew install python. Only brew install python --framework and other Framework installs get the .Python dynlib.

A fix for this in sight? I still have the issue with shy Matplotlib windows

This issue is 4,5 years old and requires ugly hacks to make matplotlib run on Mac OSX. Can we please fix this?

I second this. I installed python 2.7 and then created virtualenv using following commands:

brew install python --framework
virtualenv env
source env/bin/activate
pip install matplotlib

But I get following message when I try to import matplotlib.pyplot.

Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
>>> import matplotlib.pyplot as plt
...
    from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

Workaround mentioned by @nils-werner works. But its huge blocker for beginners who want to work with matplotlib(not only) in jupyter notebook in virtualenv.

I wrote detailed guide how to work around this issue, http://blog.rousek.name/2015/11/29/adventure-with-matplotlib-virtualenv-and-macosx/

Ugh....... I just hit this as well, following an upgrade to matplotlib 1.5 (which now fails to import a backend, as documented above by @stlk). I'm using a brew installed python 3.5.

Was going through an intro to visualization using python chapter, and I can't even begin writing the first code sample in jupyter because I keep hitting this issue on a mac... I'm using the system python that comes with mac + virtualenv.

+1

+1
Oh god....
issue from 2011.. still not fixed.

For the mean time, the utility that’s linked in this issue (and on the FAQ) still worked for me:

$ # install the utility
$ pip install git+https://github.com/gldnspud/virtualenv-pythonw-osx.git
$ # enter the virtualenv with virtualenvwrapper (or manually)
$ workon my-venv
$ # double-check that this is your venv Python binary
$ which python
/Users/macbook/.virtualenvs/my-venv/bin/python
$ # fix it, using magic
$ fix-osx-virtualenv `which python`/../..

Yes, it’s quite simple, but took me a while to figure out. I have no idea what this does, but as a result Matplotlib works by simply invoking it using that venv Python binary – no need for a wrapper shell function.

I know this solution but:

  • You need to repeat (and remember how to do so) it for all your virtualenvs
  • It makes it impossible to build certain external modules or when using CFFI or something (will need to go check what it was exactly)

It doesn't feel like an ideal solution, but an easy workaround is to use virtualenvwrapper and then use the hooks to configure things. Install virtualenv-pythonw-osx and virtualevnwrapper, source the virtualenvwrapper.sh file in order to create the hook scripts, then add the following to the postactivate script:

if [ ! -d $VIRTUAL_ENV/Python.app ]; then
  echo Fixing OSX Python display issues...
  fix-osx-virtualenv $VIRTUAL_ENV
fi

This will automatically fix any virtual environment when you activate them via the workon command

+1

+1

+1

Just hit this bug, too. The workaround suggested are not exactly elegant. Would be great to get fixed!

Try changing the matplotlib backend to get matplotlib to work in a virtual environment:

There are (at least) two solutions I've used, with some additional methods included below. Both outlined here should to the same thing with a slightly different approach:

If you already installed matplotlib using pip on your virtual environment, you can...

_1. Add a new matplotlibrc file for your virtual environment_
For example, in your virtual environment run:

$ cd ~/.matplotlib
$ nano matplotlibrc #to create file using nano

Write backend: TkAgg in the file and save upon exit. You should be good to go.

_OR_

_2. Edit the matplotlibrc file referenced by your virtual environment_
In your virtual environment, open a python shell and run:

import matplotlib
matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'

Note/Update: if you get a syntax error in your virtual environment, you might need to simply run:

import matplotlib
matplotlib.matplotlib_fname()

This displays the matplotlibrc file referenced by the matplotlib package in your virtual environment. Follow the path and open the matplotlibrc file. Edit the file's backend tag to read backend: TkAgg and save

[Also - _A third approach_]
Haven't tried before, but according to documentation you should be able to define the backend upon import of matplotlib if you don't want to change the backend in the script itself:

import matplotlib
matplotlib.use('TkAgg')

(see sources below for further explanation and additional methods)

Sources:
[1] http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
[2] http://matplotlib.org/users/customizing.html#customizing-matplotlib
[3] http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx
[4] http://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv

@wwp3 Great write-up, thanks! I have had good luck with this option:

import matplotlib
matplotlib.use('TkAgg')

Working on improving the Matplotlib docs for this issue @stlk any objections to using part of your guide for the fix to the Jupyter notebook issue (We already have the terminal python/ipython covered )

@jenshnielsen Feel free to use it, I'm happy it's helpful.

+1

Is this solved? If so, can someone close the issue?

It's not

Absolutely not. It is worse than ever before as it crashes on import. Steps to reproduce

virtualenv test
source test/bin/activate
pip install matplotlib
python -c "import matplotlib.pyplot; print 'import succeeded'"

Is there a PR that fixes this? If so, have the people encountering the issue tested it and confirms that it resolves the problem? If there isn't, is anyone working on one? It's going to be more or less impossible for anyone who can't reproduce the issue (which needs, I guess, an OSX installation at a minimum to do) to develop a fix, so we're relying on contributions here.

@nils-werner thanks for the clear description of how to reproduce. If you could add environment details (all I see in the thread is "OSX", but nothing specific about whether it's all versions, what version of virtualenv and python and how they were installed, whether certain other things need to be installed, how to install them, etc) that would be helpful as well.

Then hopefully someone with access to a suitable environment can produce a PR and reference it here. At that stage, we can review the PR and take it from there.

@pfmoore I have been using the workaround in my earlier comment, which still consistently works for me. – Though I’m not sure _how_ that works. Also see the following comment by @nils-werner.

(For the record: OS X 10.11.6, Python 3.5.2, matplotlib-1.5.3.)

@nils-werner The only reason it's worse it that we explicitly check for a framework build before running the OSX backend in Matplotlib https://github.com/matplotlib/matplotlib/blob/master/src/_macosx.m#L3071

The alternative is a soft failure where the gui is only partially functional.

WXPython has a similar check build in which prevents you from using WXPython from a non framework build.

The best workaround is to use venv from the standard library if you are using python3
Unlike virtualenv that works correctly.
http://matplotlib.org/devdocs/faq/osx_framework.html#introduction

OK, so I guess if no-one is working on a PR, the resolution here is either:

  1. If you're on Python 3, use stdlib venv
  2. Otherwise, use the workaround noted.

Cool. I'm not sure if it's worth leaving the issue open in that case - there's a clear workaround, and any code fix would be a new PR anyway. I suggest we close the issue - comments?

Any updates on this? It would be nice to have this fixed instead of having to use workarounds.

@wwp3 Great, thanks! I successfully fixed it with this option:

  1. Add a new matplotlibrc file for your virtual environment
    For example, in your virtual environment run:

$ cd ~/.matplotlib
$ nano matplotlibrc #to create file using nano
Write backend: TkAgg in the file and save upon exit. You should be good to go.

@stlk that was helpful thank you

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

Try changing the matplotlib backend to get matplotlib to work in a virtual environment:

There are (at least) two solutions I've used, with some additional methods included below. Both outlined here should to the same thing with a slightly different approach:

If you already installed matplotlib using pip on your virtual environment, you can...

_1. Add a new matplotlibrc file for your virtual environment_
For example, in your virtual environment run:

$ cd ~/.matplotlib
$ nano matplotlibrc #to create file using nano

Write backend: TkAgg in the file and save upon exit. You should be good to go.

__OR__

_2. Edit the matplotlibrc file referenced by your virtual environment_
In your virtual environment, open a python shell and run:

import matplotlib
matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'

Note/Update: if you get a syntax error in your virtual environment, you might need to simply run:

import matplotlib
matplotlib.matplotlib_fname()

This displays the matplotlibrc file referenced by the matplotlib package in your virtual environment. Follow the path and open the matplotlibrc file. Edit the file's backend tag to read backend: TkAgg and save

[Also - _A third approach_]
Haven't tried before, but according to documentation you should be able to define the backend upon import of matplotlib if you don't want to change the backend in the script itself:

import matplotlib
matplotlib.use('TkAgg')

(see sources below for further explanation and additional methods)

Sources:
[1] http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
[2] http://matplotlib.org/users/customizing.html#customizing-matplotlib
[3] http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx
[4] http://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv

Your first solution is so great and have solved my problem. Thx!

Was this page helpful?
0 / 5 - 0 ratings