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

Matplotlib Python is not installed as a framework... #218

Closed
xanderdunn opened this issue Dec 4, 2015 · 17 comments
Closed

Matplotlib Python is not installed as a framework... #218

xanderdunn opened this issue Dec 4, 2015 · 17 comments

Comments

@xanderdunn
Copy link

The full error I get at runtime:

ERROR: LoadError: LoadError: LoadError: PyError (:PyObject_Call) <type 'exceptions.RuntimeError'>
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",)

I'm on Mac OSX 10.11. I've tried a number of things:

I've installed python with brew install python --framework

I've tried env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 2.7.10

I've tried creating the script shown at the bottom of this Matplotlib FAQ page and then setting it:

ENV["PYTHON"] = "/path/to/frameworkpython"
Pkg.build("PyCall")

After each of these, I made sure the Python I had just installed/configured was selected and I ran rm(Pkg.dir("PyCall","deps","PYTHON")); Pkg.build("PyCall"). I always get the same error.

No matter what I try, I'm unable to import a local Python file that imports matplotlib.

Note that I'm not calling @pyimport matplotlib in a Julia file, I'm importing an existing Python file that has its own import matplotlib as plt statement. I'm loading the local file with this function:

function loadlocalpython(path::AbstractString)
    println("Loading Python module $path")
    @pyimport imp
    (path, name) = dirname(path), basename(path)
    (name, ext) = rsplit(name, '.', limit=2)

    (file, path, data) = imp.find_module(name, [path])
    convnet = imp.load_module(name, file, path, data)
end

Running the same python file from the command line python myfile.py, of course works fine.

@stevengj
Copy link
Member

stevengj commented Dec 5, 2015

@randy3k, you wrote the initial note about framework installs in #122, do you have any ideas?

@xanderdunn, in the meantime you could just use the Anaconda matplotlib via the Conda.jl package, via (in a fresh Julia session):

ENV["PYTHON"]=""
Pkg.build("PyCall")
using PyPlot

which will download and install the Anaconda Matplotlib in a private Julia directory.

@randy3k
Copy link

randy3k commented Dec 5, 2015

I don't have any idea.
@xanderdunn Do @pyimport matplotlib or using PyPlot work at the first place?

@xanderdunn
Copy link
Author

Thanks to both of you for looking at this. Yes, using PyPlot works fine and @pyimort matplotlib works fine.

I noticed that the error messages being spit out were always referencing the same matplotlib.py file on disk, regardless of which Python installation PyCall was using. I figured out this is because I had the system's pip package directory in my PYTHONPATH! 😫 So, regardless of the Python being used, it always found that matplotlib installation first. With that all cleared out of my PYTHONPATH, the Conda.jl method that @stevengj mentioned above works.

On first try, system Python and Pyenv were still giving the same error, but I need to methodically step through them again.

@xanderdunn
Copy link
Author

Yeah, I still get the error:

ERROR: LoadError: LoadError: LoadError: PyError (:PyObject_Call) <type 'exceptions.RuntimeError'>
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",)
  File "/path/to/my/file.py", line 18, in <module>
    import matplotlib.pyplot as plt
  File "/Users/admin/.pyenv/versions/2.7.10/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/admin/.pyenv/versions/2.7.10/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/admin/.pyenv/versions/2.7.10/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in <module>
    from matplotlib.backends import _macosx

It's correctly referring to the matplotlib installed via the pyenv 2.7.10's pip.

I uninstalled the pyenv's 2.7.10 installation and reinstalled it with env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 2.7.10 for this.

Same with uninstalling brew's python 2 and reinstalling it with --framework.

@randy3k
Copy link

randy3k commented Dec 6, 2015

I have tested your code with pyenv 2.7.6, 2.7.10 and 3.4.3. They all work fine.

julia> loadlocalpython("test.py")
Loading Python module test.py
PyObject <module 'test' from 'test.pc'>

with test.py containing only one line import matplotlib.

@randy3k
Copy link

randy3k commented Dec 6, 2015

Wait, I can actually reproduce the error if test.py is

import matplotlib.pyplot

In fact, the error should be reproduced in Julia by running

@pyimport matplotlib.pyplot as plt

I found a solution. It is because python within Julia fails to find a backend. The following will fix it:

echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc

@xanderdunn
Copy link
Author

@randy3k Oh, wow! Thank you so much! This solved it!

@randy3k
Copy link

randy3k commented Dec 7, 2015

I guess PyPlot also sets the backend automatically.

using PyCall
using PyPlot
loadlocalpython("test.py")

@stevengj
Copy link
Member

stevengj commented Dec 7, 2015

PyPlot is a bit smarter about picking a backend than if you do a raw @pyimport matplotlib.pyplot as plt. I general, I would always recommend using PyPlot over directly importing pyplot.

@aysebilgegunduz
Copy link

Problem Cause In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). Set the back end of macosx that is differ compare with other windows or linux os.

I resolve this issue following ways:

  • I assume you have installed the pip matplotlib, there is a directory in you root called ~/.matplotlib.

  • Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg

@ghost
Copy link

ghost commented Dec 20, 2016

@randy3k Thank you !

@maragh
Copy link

maragh commented May 15, 2017

@aysebilgegunduz Thank you!

@Gailbobo
Copy link

@aysebilgegunduz super helpful. Totally made my day!

@marcunzueta
Copy link

@randy3k Yeah! Worked for me too! Thank you ;)

@JonathanLehner
Copy link

it works, but would be nice to know why it works

@AaronWWK
Copy link

What is that file? Use TextEdit to crate a new file? And just add the line to the file?

@mucahitdemir
Copy link

Problem Cause In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). Set the back end of macosx that is differ compare with other windows or linux os.

I resolve this issue following ways:

  • I assume you have installed the pip matplotlib, there is a directory in you root called ~/.matplotlib.
  • Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg

Hey! I could not solve my problem with this issue? Could you make it more explicit?
Best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants