How to use Python Matplotlib with OSx and virtualenv?
<p>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 <code>virtualenv</code>. </p>

<p>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 <strong>Python3</strong>, so I installed it via <strong>HomeBrew</strong>.</p>

<p><strong>The challenge:</strong></p>

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

<p>The workaround that Is supposed to be common is described at <a href="http://matplotlib.org/faq/virtualenv_faq.html" rel="nofollow">this link</a> but I am unsure how to use this (the OSX section). </p>

<p><strong>My understanding of the solution:</strong></p>

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

<p><strong>Questions:</strong></p>

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

<p>Many thanks!</p>

<p>You are almost correct. From personal experience, Matplotlib doesn't require the system Python to run on macOS. </p>

<p>As such, to install and use Matplotlib you can:</p>

<ol>
<li>Create a new virtual env and activate it with <code>source virtualenv/bin/activate</code>.</li>
<li>Install Matplotlib with <code>pip install matplotlib</code>.</li>
<li>Run your script (which imports Matplotlib) with <code>python script.py</code>.</li>
</ol>

<p>If you need more information take a look at the <a href="http://matplotlib.org/users/installing.html" rel="nofollow">Matplotlib documentation for installation</a>. </p>

<p>I also believe that if you download the source files and install it with <code>python setup.py install</code> while inside the virtual env then you can use the modules just the same as if you were to do <code>pip install matplotlib</code>. </p>

python osx shell matplotlib virtualenv