18

I just discovered that matplotlib has issue with virtual environments. I tried the solutions in the FAQs but they didn't work. An ideal solution should only involve pip commands, but it might be hard or unrealistic to request that. Anyway, I tried what they had on the OS X section to make a framework bash file in your venv folder and run command through it.

$ frameworkpython krls.py

when I did that I got a permission deniel error:

Permission denied

I am sort of scared of sudoing commands that I am not 100% what they do...anyway, it seemed the bash script isn't doing anything too dangerous so I went ahead and sudo-ed it. However, the response of my terminal was weird, it said:

sudo frameworkpython krsl.py
sudo: frameworkpython: command not found

which means it doesn't recognize frameworkpython as a command. Without the sudo it says:

frameworkpython krsl.py -bash: /Users/my_name/path/venv/bin/frameworkpython: Permission denied

which seems it recognizes frameworkpython as a command (?) but it didn't execute it due to permissions? It seems strange to me. Anyone any ideas?


I also tried:

$ pip install TKAgg
Collecting TKAgg
  Could not find a version that satisfies the requirement TKAgg (from versions: )
No matching distribution found for TKAgg

but it didn't work.

So I tried the next option which is using PySide which also didn't work and gave a giant error output:

$ pip install pyside
Collecting pyside
  Using cached PySide-1.2.4.tar.gz
Building wheels for collected packages: pyside
  Running setup.py bdist_wheel for pyside ... error
  Complete output from command /Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/tmpUR9qFCpip-wheel- --python-tag cp27:
  Removing /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/pyside_package
  running bdist_wheel
  running build
  Python architecture is 64bit
  error: Failed to find cmake. Please specify the path to cmake with --cmake parameter.

  ----------------------------------------
  Failed building wheel for pyside
  Running setup.py clean for pyside
Failed to build pyside
Installing collected packages: pyside
  Running setup.py install for pyside ... error
    Complete output from command /Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-UVA_F4-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/my_name/path/venv/bin/../include/site/python2.7/pyside:
    Removing /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/pyside_package
    running install
    running build
    Python architecture is 64bit
    error: Failed to find cmake. Please specify the path to cmake with --cmake parameter.

    ----------------------------------------
Command "/Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-UVA_F4-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/my_name/path/venv/bin/../include/site/python2.7/pyside" failed with error code 1 in /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/

After that then I tried the next option using WX Phonix. Unfortunately, I went through their site and couldn't find how to do it.

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
  • Have you tried installing `wheel` via pip? (and upgrading pip/wheel/setuptools) – sytech Jun 29 '16 at 04:44
  • @Gator_Python I did a pip list and it says I do have wheel (0.29.0). I tried upgrading it with `pip install wheel --upgrade` but it seemed to fail because it said `Requirement already up-to-date:` – Charlie Parker Jun 29 '16 at 05:13
  • 3
    The *easiest* may be to use [anaconda](https://docs.continuum.io/anaconda/install#anaconda-for-os-x-graphical-install) - I know that's not exactly an ideal answer, but I'll keep looking. Anaconda would be a separate environment. It includes [all these packages](https://docs.continuum.io/anaconda/pkg-docs) – sytech Jun 29 '16 at 05:38
  • @Gator_Python I did try that but the issue I had with that is that I am using and developing a library as I go so I was using a virtual env with `python my_project/setup.py develop` to get that to work but I ran that command while in anaconda and it seemed that python still couldn't find my library... – Charlie Parker Jun 29 '16 at 18:21
  • 1
    @CharlieParker did you use the following steps to set up and activate your Anaconda environment? – juanpa.arrivillaga Jul 01 '16 at 19:16
  • I did try it but I wasn't able to make `python my_project/setup.py develop` work though :( (which I guess is crucial for me) – Charlie Parker Jul 01 '16 at 20:28
  • Not a complete solution but based on your **sudo** command error looks like it's a user env issue. Try running same sudo command with **-E** flag: `sudo frameworkpython krsl.py` – VDR Jul 08 '16 at 07:25

7 Answers7

13

Update:

With Python3, you can use the built-in implementation of virtualenv via -m venv:

python -m venv <name of virtualenv>
source <name of virtualenv>/bin/activate

Python3's builtin implementation builds the virtualenv such that Python is set up as a framework, so no need to configure anything to get it to work. More details here.


Original Answer:

As stated in the FAQS you reference, you will need to create a frameworkpython scripts in venv/bin. It looks like your system is unable to find frameworkpython so it is either not on your systems $PATH or it is not executable.

Make sure you make this scripts executable via chmod +x venv/bin/frameworkpython!

From Linux/Unix docs (I added the bold):

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files.

Your system will only look for executable files accessible via the PATH environment variable, skipping over non-executable files.


Creating the frameworkpython script is necessary because matplotlib requires a framework build of python. From the link above:

Unfortunately virtualenv creates a non framework build even if created from a framework build of Python.

Here's another post with more details on framework builds

Pedro Cattori
  • 2,735
  • 1
  • 25
  • 43
  • why did I have to change the executable thing of the script? seems odd, doesn't it? – Charlie Parker Jul 08 '16 at 14:43
  • Updated above. Short answer: `$PATH` only finds executable files – Pedro Cattori Jul 08 '16 at 14:47
  • Why don't I need to run the 'frameworkpython' script every time I activate my environment? It seems the file only changes path things but I don't run that script every time I activate my environment. – Charlie Parker Aug 30 '16 at 19:19
  • you run `frameworkpython` instead of just `python` for running scripts that depend on `matplotlib`. you don't need to run `frameworkpython` at all if you are not running a script that depends on `matplotlib` (or that requires a framework version of python for another reason) – Pedro Cattori Sep 02 '16 at 04:52
  • For future readers, the python3 way of doing it is beyond a doubt the easiest. Please try that! Highly recommend it. – Charlie Parker Apr 13 '17 at 16:54
6

If you are using Python 2.x then, use these commands in virtual environment:

import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt

This makes the matplotlib work in the virtual environment too.

I hope this helps.

sufi
  • 157
  • 3
  • 10
3

Much like your setup, I am using matplotlib from within a virtual environment on OSX.

I see that you have spent quite some effort to get things running and I am not certain whether or not this will actually solve your problem, since no two setups are the same. However, I drop my configuration such that you can compare and possibly find something that helps you solving this issue.

Disclaimer: I use virtualenvwrapper and have stored my venvs under ~/.virtualenvs/.

  1. Copy the frameworkpython file to ~/.virtualenvs/YOUR_VENV/bin

  2. Make sure to change the PATHTOPYTHON variable within the frameworkpython bash script to refer to the correct location. For my setup, I have changed it to /usr/bin/ (while the original setting was /usr/local/bin)

  3. Set executable flag chmod +x frameworkpython

  4. Install libs using pip as usual after switching to virtual environment using virtualenvwrapper: workon YOUR_VENV

Sourcing frameworkpython as regular user (no root, pls!) and importing matplotlib yields:

$ ./frameworkpython
Python 2.7.0 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib as mpl
>>> mpl.__version__
'1.5.1'
>>> import matplotlib.pyplot as plt
>>> ^D
jbndlr
  • 4,965
  • 2
  • 21
  • 31
  • it seems it was a permissions issue. Why do you think I had to change the flag? – Charlie Parker Jul 08 '16 at 14:43
  • On your second point 2), how do I find out what is the correct location to refer to my python? I recently made a new environment with python 3.5 and I am unable to make the frameworkpython thing work. It says that the command `frameworkpython` does not exist meaning it can't find it. I see the frameworkpython file and it only changes environment things but when I run it it crashes my whole terminal, its a bit weird. Did you get it to work with python 3.5? – Charlie Parker Aug 30 '16 at 19:18
  • Sorry, I did not test this for python 3.x -- did you check for the FAQs and the contents of ``frameworkpython``? – jbndlr Sep 04 '16 at 07:12
  • it seems its also important to choose the correct version of python. I had to change `PYVER=3.5`. For some reason even though I had the right python path set in `PATHTOPYTHON`, it still didn't work when `PYVER=2.7`. With 2.7 it gave the error `ImportError: No module named site` not sure why, but it went away when I used a different version of python. – Charlie Parker Jan 18 '17 at 18:27
3

Extending @Gator_Python's and @juanpa.arrivillaga's suggestions, I recommend installing the latest version of anaconda. It is a cross-platform, pip-like package manager that also handles virtual environments. It also installs large packages like numpy, pandas and matplotlib with little headache.

Setup: From the commandline, just create a virtual environment and install from there. Here is an example of how to set up conda (substitute the appropriate environment name and python version number respectively):

conda update conda
conda update anaconda
conda create -n <envname> python=<version> matplotlib
source activate <envname>
conda info --envs                  # show current env
conda list                         # see installed env packages

This should place you in an isolated, clean environment. You can see a minimal list of packages installed in this environment. Make sure pip and setuptools are listed (they are installed automatically). For any other packages you need, use either conda install <package> or pip install <package>, in that order. Install your project:

cd <path\to\project>
python setup.py develop

Removal: When you are ready to remove your environment:

source deactivate
conda env remove -n <envname>

Note: alternatives to setup.py develop are:

pylang
  • 40,867
  • 14
  • 129
  • 121
2

To get pyside working:

brew install cmake

brew is a pretty standard OSX dependency.

You are right in identifying that this is a framework issue Differences between Framework and non-Framework builds of Python on Mac OS X To get this running, you will need the framework python running.

And even more correct in not running sudo commands w/o understanding them. Why you might be able to see it, but not be able to run it could be due to not having the environment established correctly as sudo. How to keep Environment Variables when Using SUDO

if you run which frameworkpython it should show you the path to the executable on the file system. I would check the file permissions on this file, in particular check the ownership.

ls -l $(which frameworkpython)

The ouptut of which frameworkpython should be the path that could be executed with root, but I would avoid it, as any other dependancies might not be loaded correctly, or can have the wrong permissions set, it might work now, but will get you in trouble later on.

/path/to/frameworkpython krls.py

When I ran into this issue this blog was an awesome resource.

Community
  • 1
  • 1
Luke Exton
  • 3,506
  • 2
  • 19
  • 33
0
sudo apt-get install tk-dev libpng-dev libffi-dev dvipng texlive-latex-base

Then uninstalled and reinstalled matplotlib:

pip uninstall matplotlib

pip install matplotlib
Serenity
  • 35,289
  • 20
  • 120
  • 115
user3480223
  • 34
  • 1
  • 9
0

To echo Pedro Cattori's answer, the easiest way that has worked every time for me is to simply use python 3 with a virtual env as in:

python -m venv my-virtualenv
source my-virtualenv/bin/activate

or

python3 -m venv my-virtualenv
source my-virtualenv/bin/activate

from original FAQ page: http://matplotlib.org/faq/osx_framework.html#osxframework-faq

Everything else I've tried has resulted in ridiculous amount of code and time spent figuring out whats wrong. With that it always works every time without me further doing much more beyond pip installing matplotlib.

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323