6

I just started learning Python's libraries for data analysis (Numpy, Pandas and Matplotlib) but have already stumbled across my first problem - getting the Matplotlib to work with virtualenv.

When working with Python I always create a new virtual environment, get my desired Python version and libraries/dependancies into this environment. This time, the course required that I use Python3, so I installed it via HomeBrew.

The challenge:

  • Matplotlib needs to interact with OS
  • to make this happen it needs the framework build of Python (the system one)
  • ...which is not possible if it's inside of a virtualenv, which makes it use the virtualenv build of Python

The workaround that Is supposed to be common is described at this link but I am unsure how to use this (the OSX section).

My understanding of the solution:

  1. get Python version that I wish to use, install it system wide, NOT in a virtualenv
  2. create a virtualenv, get dependencies that I need, this creates the virtualenv Python build
  3. somehow trick the system into using virtualenv dependancies with system build of Python
  4. this is done with the shell script(?) which seems to modify certain variables in shell/terminal config file(s)

Questions:

  1. am I correct with the above "explanation to myself"?
  2. what is the correct way to do this? from within the virtualenv, from outside of it...?
  3. after this is done, how do I execute my Python scripts? with my virtualenv activated or not?

Many thanks!

Alexander Starbuck
  • 1,139
  • 4
  • 18
  • 31
  • 1
    Possible duplicate of [What is the simplest way to make matplotlib in OSX work in a virtual environment?](http://stackoverflow.com/questions/38090455/what-is-the-simplest-way-to-make-matplotlib-in-osx-work-in-a-virtual-environment) – jbndlr Sep 07 '16 at 10:52
  • 1
    What is the proper ettiquette if possible duplicate? Remove the original Q, answer myself, point to the solution...? Thanks! – Alexander Starbuck Sep 07 '16 at 11:25
  • 1
    I think you should leave your question, since it is nicely written and will be found by others searching for the same issue. Maybe add a link to the solution to the question itself? – jbndlr Sep 07 '16 at 11:36
  • 1
    Use [conda](https://www.continuum.io/downloads). You can download the Python 3 version and the skip `virtualenv` step. `matplotlib` comes pre-bundled. If later you need a virtual, use can easily [`conda create`](http://conda.pydata.org/docs/commands/conda-create.html) new ones. – pylang Sep 07 '16 at 11:56

1 Answers1

1

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 used this steps to make the matplotlib running in the virtual environment.

gsnedders
  • 5,532
  • 2
  • 30
  • 41
sufi
  • 157
  • 3
  • 10