virtualenv icon indicating copy to clipboard operation
virtualenv copied to clipboard

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

Open vbabiy opened this issue 15 years ago • 34 comments

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?


  • Bitbucket: https://bitbucket.org/ianb/virtualenv/issue/65
  • Originally Reported By: dan mackinlay
  • Originally Created At: 2010-09-22 09:09:54

vbabiy avatar Mar 14 '11 21:03 vbabiy

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


Original Comment By: dan mackinlay

vbabiy avatar Mar 14 '11 21:03 vbabiy

+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

gregglind avatar Dec 30 '11 15:12 gregglind

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.

gregglind avatar Jan 01 '12 23:01 gregglind

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

nils-werner avatar Mar 06 '15 11:03 nils-werner

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

nils-werner avatar Nov 24 '15 10:11 nils-werner

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/

stlk avatar Nov 29 '15 08:11 stlk

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.

terrycojones avatar Nov 29 '15 22:11 terrycojones

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.

thomasdziedzic avatar Nov 30 '15 05:11 thomasdziedzic

+1

mturilli avatar Dec 14 '15 13:12 mturilli

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

arsenico13 avatar Jan 03 '16 17:01 arsenico13

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.

tjanson avatar Jan 31 '16 00:01 tjanson

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)

nils-werner avatar Jan 31 '16 14:01 nils-werner

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

RobertCoop avatar Feb 10 '16 19:02 RobertCoop

+1

grantstephens avatar Mar 31 '16 07:03 grantstephens

+1

iannesbitt avatar May 07 '16 22:05 iannesbitt

+1

bmaher74 avatar May 12 '16 01:05 bmaher74

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

visualmotive avatar May 30 '16 19:05 visualmotive

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 avatar Jun 02 '16 06:06 wwp3

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

import matplotlib
matplotlib.use('TkAgg')

visualmotive avatar Jun 02 '16 13:06 visualmotive

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 avatar Jul 15 '16 16:07 jenshnielsen