VSCode + Python on a mac

As my experimentation continues, I wanted to get Visual Studio Code installed on a mac, and wanted to use python as the language of choice – main reason for the mac is to understand and explore the #ML libraries, runtimes, and their support on a mac (both natively and in containers – docker).

Now, Microsoft has a very nice tutorial to get VSCode setup and running on a mac, including some basic configuration (e.g. touchbar support). But when it comes to getting python setup, and running, that is a different matter. Whilst the tutorial is good, it doesn’t actually work and errors out.

Below is the code that Microsoft outlines in the tutorial for python. It essentially is the HelloWorld using packages and is quite simple; but this will fail and won’t work.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x))       # Plot the sine of each x point
plt.show()                   # Display the plot

When you run this, you will see an error that is something like the one outlined below.

2019-01-18 14:23:34.648 python[38527:919087] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fbafa49bf10
2019-01-18 14:23:34.654 python[38527:919087] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fbafa49bf10'
*** First throw call stack:
	0   CoreFoundation                      0x00007fff521a1ecd __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff7e25d720 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff5221f275 -[NSObject(NSObject) __retain_OA] + 0
	3   CoreFoundation                      0x00007fff52143b40 ___forwarding___ + 1486
	4   CoreFoundation                      0x00007fff521434e8 _CF_forwarding_prep_0 + 120
	5   libtk8.6.dylib                      0x000000011523031d TkpInit + 413
	6   libtk8.6.dylib                      0x000000011518817e Initialize + 2622
	7   _tkinter.cpython-37m-darwin.so      0x0000000114fb2a0f _tkinter_create + 1183
	8   python                              0x0000000101836ba6 _PyMethodDef_RawFastCallKeywords + 230
	9   python                              0x00000001019772b1 call_function + 257
	10  python                              0x0000000101974daf _PyEval_EvalFrameDefault + 45215
	11  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	12  python                              0x0000000101835867 _PyFunction_FastCallDict + 231
	13  python                              0x00000001018b9481 slot_tp_init + 193
	14  python                              0x00000001018c3441 type_call + 241
	15  python                              0x0000000101836573 _PyObject_FastCallKeywords + 179
	16  python                              0x000000010197733f call_function + 399
	17  python                              0x0000000101975052 _PyEval_EvalFrameDefault + 45890
	18  python                              0x0000000101836368 function_code_fastcall + 120
	19  python                              0x0000000101977265 call_function + 181
	20  python                              0x0000000101974daf _PyEval_EvalFrameDefault + 45215
	21  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	22  python                              0x0000000101835867 _PyFunction_FastCallDict + 231
	23  python                              0x0000000101839782 method_call + 130
	24  python                              0x00000001018371e2 PyObject_Call + 130
	25  python                              0x00000001019751c6 _PyEval_EvalFrameDefault + 46262
	26  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	27  python                              0x0000000101836a73 _PyFunction_FastCallKeywords + 195
	28  python                              0x0000000101977265 call_function + 181
	29  python                              0x0000000101974f99 _PyEval_EvalFrameDefault + 45705
	30  python                              0x0000000101836368 function_code_fastcall + 120
	31  python                              0x0000000101977265 call_function + 181
	32  python                              0x0000000101974f99 _PyEval_EvalFrameDefault + 45705
	33  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	34  python                              0x0000000101836a73 _PyFunction_FastCallKeywords + 195
	35  python                              0x0000000101977265 call_function + 181
	36  python                              0x0000000101974f99 _PyEval_EvalFrameDefault + 45705
	37  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	38  python                              0x0000000101836a73 _PyFunction_FastCallKeywords + 195
	39  python                              0x0000000101977265 call_function + 181
	40  python                              0x0000000101974daf _PyEval_EvalFrameDefault + 45215
	41  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	42  python                              0x00000001019cc9a0 PyRun_FileExFlags + 256
	43  python                              0x00000001019cc104 PyRun_SimpleFileExFlags + 388
	44  python                              0x00000001019f7edc pymain_main + 9148
	45  python                              0x0000000101808ece main + 142
	46  libdyld.dylib                       0x00007fff7f32bed9 start + 1
	47  ???                                 0x0000000000000003 0x0 + 3
libc++abi.dylib: terminating with uncaught exception of type NSException

[Done] exited with code=null in 1.017 seconds

The main reason this fails is that one has to be a little more explicit with matplot (the library that we are trying to use). Matplot has this concept of backends, which essentially is the runtime dependencies needed to support various execution environments – including both interactive and non-interactive environments.

For matplot to work on a mac, the raster graphics c++ library that it uses is based on something called Anti-Grain Geometry (AGG). And for the library to render, we need to be explicit on which agg to use (there are multiple raster libraries).

In addition on a mac OS X there is a limitation when rendering in OSX windows (presently lacks blocking show() behavior when matplotlib is in non-interactive mode).

To get around this, we explicitly tell matplot to use the specific agg (“TkAgg in our case) and then it will all work. I have a updated code sample below, which adds more points, and also waits for the console input, so one can see what the output looks like.

import matplotlib
from matplotlib import pyplot as plt
import numpy as np

def waitforuser():
    input("Press enter to continue ...")

x = np.linspace(0, 50, 200)  # Create a list of evenly-spaced numbers over the range
y = np.sin(x)




And incase you are wondering what it looks like, below are a few screenshots showing the output.

To get everything working, make sure you setup the Linting, debugger, and the python environment properly. And of course, you can go nuts with containers! Happy coding!

Published by

Amit Bahree

This blog is my personal blog and while it does reflect my experiences in my professional life, this is just my thoughts. Most of the entries are technical though sometimes they can vary from the wacky to even political – however that is quite rare. Quite often, I have been asked what’s up with the “gibberish” and the funny title of the blog? Some people even going the extra step to say that, this is a virus that infected their system (ahem) well. [:D] It actually is quite simple, and if you have still not figured out then check out this link – whats in a name?

2 thoughts on “VSCode + Python on a mac”

  1. Thanks Amit.
    It was driving me crazy. The docs on MS and matplotlib where pointing to nowhere, far from seeing the problem with AGG.
    As usual nothing coming from MS is straightforward.
    VS Code is light years ahead from the rest of their products, but you still have to do a lot to have it working by your own.

    1. If you see many others also having a lot of these issues – I can try and find the owners of the docs around this and nudge them to update. But if you and I aren’t a representative sampling then might leave it. 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.