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!

4
  • 1
    Possible duplicate of What is the simplest way to make matplotlib in OSX work in a virtual environment? Commented Sep 7, 2016 at 10:52
  • 1
    What is the proper ettiquette if possible duplicate? Remove the original Q, answer myself, point to the solution...? Thanks! Commented Sep 7, 2016 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? Commented Sep 7, 2016 at 11:36
  • 1
    Use conda. 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 new ones. Commented Sep 7, 2016 at 11:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.