1

I am trying to write a blender script for which I need matplotlib.pyplot. When trying to import matplotlib.pyplot as plt I get the following error:

ImportError: No module named 'tkinter'

However, using the installed anaconda version, the import is no problem. The common solution to run

sudo apt-get install python3-tk

does not solve the problem. I tried to add the Path to tkinter with:

sys.path.append('/usr/lib/python3.4/tkinter/')

sys.path.append('/home/<username>/anaconda3/lib/python3.6/tkinter/')

Both commands did not resolve the error.

McLawrence
  • 4,975
  • 7
  • 39
  • 51
  • Did you try to install tkinter using pip: "pip install tkinter" – Tin Luu Aug 31 '17 at 10:51
  • I was not aware that this is possible. Using `tkinter` or `python3-tk` both result in `Could not find a version that satisfies the requirement tkinter (from versions: ) No matching distribution found for tkinter` – McLawrence Aug 31 '17 at 10:53
  • It's tkinter, not tkiter – Tin Luu Aug 31 '17 at 10:56
  • Sorry. Just a typo. But same error message from `pip`. I read that, since it is a `C++` package, it cannot be installed via `pip` – McLawrence Aug 31 '17 at 10:57
  • I am using anaconda distro, and it run well without above issue :) – Tin Luu Aug 31 '17 at 11:04
  • The `pip` command ran well or the import of `pyplot`. My problem is not with the anaconda distribution but with Blenders own python distribution. – McLawrence Aug 31 '17 at 11:07
  • First check your versions. Blender 2.78 uses python 3.5, trying to use modules built for python 3.4 and/or 3.6 will cause issues.There are several [ways](https://blender.stackexchange.com/q/5287/935) to access third party python modules, if it is only your use you may find removing blenders copy of python the easiest so that it uses the system python. – sambler Sep 01 '17 at 02:12
  • Does the system python also need to be 3.5 to work with blender? What would be the alternative, if deleting was not an option? – McLawrence Sep 01 '17 at 04:53

3 Answers3

1

I managed to import tkinter (and use it with matplotlib.pyplot) from Blender as I explained here:

https://stackoverflow.com/a/56050748/4511978

Hope it helps!
Andres

fr_andres
  • 5,927
  • 2
  • 31
  • 44
0

On Linux, you shouldn’t need to mess around with pip to install tkinter, since it’s a standard Python module. Also the Blender package should use the system Python, so it will have access to all system Python modules.

But ... tkinter is a GUI framework, and trying to use this will conflict with Blender’s own GUI.

Another option might be to use Matplotlib in offscreen-plotting mode, which doesn’t need a GUI.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
0

Try switching the backend from tkinter to agg using -

matplotlib.use('agg')

markroxor
  • 5,928
  • 2
  • 34
  • 43