2

context

  • machine: 64-bit Mac
  • OS: macOS 10.10.5

error msg

following course [Jerry Kurata 'Tensorflow: Getting Started'] ran the following:

import tensorflow as tf
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation

num_house = 160
np.random.seed(42)
house_size = np.random.randint(low=1000, high=3500, size=num_house)

np.random.seed(42)
house_price = house_size * 100.0 + np.random.randint(low=20000, high=70000, size=num_house)

plt.plot(house_size, house_price, "bx")
plt.xlabel("price")
plt.ylabel("size")
plt.show

received this error

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

attempt to fix

Zach Valenta
  • 1,783
  • 1
  • 20
  • 35

1 Answers1

11

see the above answer Installation Issue with matplotlib Python and reference one of the comments:

Some users may not want to change the backend for all of their scripts. This page -- matplotlib.org/faq/usage_faq.html#what-is-a-backend -- tells another way: include the statement import matplotlib as mpl then mpl.use('TkAgg') right after that, then do the import for pyplot.

and set the matplotlib backend in the imports, like so

import tensorflow as tf
import numpy as np
import math

## SET BACKEND
import matplotlib as mpl
mpl.use('TkAgg')

import matplotlib.pyplot as plt
import matplotlib.animation as animation

num_house = 160
np.random.seed(42)
house_size = np.random.randint(low=1000, high=3500, size=num_house)

np.random.seed(42)
house_price = house_size * 100.0 + np.random.randint(low=20000, high=70000, size=num_house)

plt.plot(house_size, house_price, "bx")
plt.xlabel("price")
plt.ylabel("size")
plt.show

in this way, you won't need to touch the matplotlib in $HOME

Zach Valenta
  • 1,783
  • 1
  • 20
  • 35
  • I installed matplotlib for Python3 following these instructions: https://stackoverflow.com/questions/8605847/how-to-install-matplotlib-with-python3-2 and I had the same error. I just added the 2 lines ` ## SET BACKEND import matplotlib as mpl mpl.use('TkAgg') ` and it solved the issue. thank you! – enri Jul 24 '18 at 13:47