Skip to content
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

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

Closed
vbabiy opened this issue Mar 14, 2011 · 34 comments
Closed

Comments

@vbabiy
Copy link

vbabiy commented Mar 14, 2011

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?


@vbabiy
Copy link
Author

vbabiy commented Mar 14, 2011

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


Original Comment By: dan mackinlay

@gregglind
Copy link

+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 added a commit to gregglind/virtualenv that referenced this issue Jan 1, 2012
*  makes Framework bundle virtualenvs play nicer with
   OSX widgets
*  based on work at https://github.com/gldnspud/virtualenv-pythonw-osx
*  Needs code review (fragile, path based, uses tmpfiles)

TODO:

*  What is the general fix for this for non-Framework installs?
@gregglind
Copy link

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 added a commit to gregglind/virtualenv that referenced this issue Jan 2, 2012
shouldfix:  creating a virtualenv from an OSX
Framework build should no longer require XCode to
run successfully.

implementaion:  if ``install_name_tool`` missing,
download binaries from macosxforge, and install
them to a temp directory

rejected:  download and compile .c source.

TODOS:

*   review for python2/3 issues
*   cleanup of tempfiles?  Assume the system will do it?
*   test for robustness
*   review implications of not having XCode for packages, if any
*   integrate with pypa#54 pull or its descendents.  (trivial)

Possible Test Code:

    #!/bin/sh

    deactivate # virtualenv on system (Framework) python

    # assuming I have XCode or ``install_name_tool`` in path.
    python virtualenv.py t_install_name_tool  # creates with no issue
    t_install_name_tool/bin/python -c ""

    # remove it from path
    sudo mv /usr/bin/install_name_tool{,2}

    # should create with no issue, using tmpfile
    python virtualenv.py t_tmp_install_name_tool
    t_tmp_install_name_tool/bin/python -c ""

    # clean up
    sudo mv /usr/bin/install_name_tool{2,}
    command rm -rf t_{,tmp_}install_name_tool
gorakhargosh pushed a commit to gorakhargosh/virtualenv that referenced this issue Mar 13, 2012
shouldfix:  creating a virtualenv from an OSX
Framework build should no longer require XCode to
run successfully.

implementaion:  if ``install_name_tool`` missing,
download binaries from macosxforge, and install
them to a temp directory

rejected:  download and compile .c source.

TODOS:

*   review for python2/3 issues
*   cleanup of tempfiles?  Assume the system will do it?
*   test for robustness
*   review implications of not having XCode for packages, if any
*   integrate with pypa#54 pull or its descendents.  (trivial)

Possible Test Code:

    #!/bin/sh

    deactivate # virtualenv on system (Framework) python

    # assuming I have XCode or ``install_name_tool`` in path.
    python virtualenv.py t_install_name_tool  # creates with no issue
    t_install_name_tool/bin/python -c ""

    # remove it from path
    sudo mv /usr/bin/install_name_tool{,2}

    # should create with no issue, using tmpfile
    python virtualenv.py t_tmp_install_name_tool
    t_tmp_install_name_tool/bin/python -c ""

    # clean up
    sudo mv /usr/bin/install_name_tool{2,}
    command rm -rf t_{,tmp_}install_name_tool

The original patch was cleaned up, tested against
at Python 2.5, 2.6, 2.7, 3.2, and verified to work.

Signed-off-by: Yesudeep Mangalapilly <yesudeep@google.com>
@nils-werner
Copy link

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

@nils-werner
Copy link

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

@stlk
Copy link

stlk commented Nov 29, 2015

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/

@terrycojones
Copy link

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.

@thomasdziedzic
Copy link

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.

@mturilli
Copy link

+1

@arsenico13
Copy link

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

@tjanson
Copy link

tjanson commented Jan 31, 2016

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.

@nils-werner
Copy link

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)

@RobertCoop
Copy link

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

@grantstephens
Copy link

+1

kaiyuzhao referenced this issue in kaiyuzhao/SFrame Apr 28, 2016
1. Exposed matplotlib scatter plot API to the user via kwargs
2. added large scale scatter plot support
3. parameterized heatmap streaming aggregator

In progress:
1. bar chart is still in progress

TODOs:

1. need to remove numpy dependency
2. need to support customizable view style
3. need to support user defined point size for large scale plot
@iannesbitt
Copy link

+1

1 similar comment
@bmaher74
Copy link

+1

@cdmxx
Copy link

cdmxx commented May 30, 2016

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

@wwp3
Copy link

wwp3 commented Jun 2, 2016

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

@cdmxx
Copy link

cdmxx commented Jun 2, 2016

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

import matplotlib
matplotlib.use('TkAgg')

@jenshnielsen
Copy link

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 )

@stlk
Copy link

stlk commented Jul 15, 2016

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

@jucor
Copy link

jucor commented Aug 22, 2016

+1

@mattayes
Copy link

mattayes commented Oct 20, 2016

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

@jenshnielsen
Copy link

It's not

@nils-werner
Copy link

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'"

@pfmoore
Copy link
Member

pfmoore commented Oct 25, 2016

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.

@tjanson
Copy link

tjanson commented Oct 25, 2016

@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.)

@jenshnielsen
Copy link

@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

@pfmoore
Copy link
Member

pfmoore commented Oct 25, 2016

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?

@aleksijohansson
Copy link

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

@ashishprajapati
Copy link

@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.

@dendisuhubdy
Copy link

@stlk that was helpful thank you

@stale
Copy link

stale bot commented Jan 22, 2019

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.

@stale stale bot added the wontfix label Jan 22, 2019
@stale stale bot closed this as completed Jan 30, 2019
@rex201899
Copy link

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!

@pypa pypa locked and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests