3

I have a simple import in python code (import matplotlib.pyplot as plt), but I keep getting this error, any idea how to solve it? (Using it on VS Code, on an Apple M1 pro chip).


Traceback (most recent call last):
  File "/Users/mk/Desktop/courses spring 22/EECE 433/assignment 1/code/code.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "/Users/mk/Library/Python/3.8/lib/python/site-packages/matplotlib/__init__.py", line 109, in <module>
    from . import _api, _version, cbook, docstring, rcsetup
  File "/Users/mk/Library/Python/3.8/lib/python/site-packages/matplotlib/rcsetup.py", line 27, in <module>
    from matplotlib.colors import Colormap, is_color_like
  File "/Users/mk/Library/Python/3.8/lib/python/site-packages/matplotlib/colors.py", line 51, in <module>
    from PIL import Image
  File "/Users/mk/Library/Python/3.8/lib/python/site-packages/PIL/Image.py", line 89, in <module>
    from . import _imaging as core
ImportError: dlopen(/Users/mk/Library/Python/3.8/lib/python/site-packages/PIL/_imaging.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '_xcb_connect'
mka1
  • 39
  • 1
  • 3
  • In case @b3z's answer gets deleted here's the content, since I think it is an addition as a comment: I (@b3z) looked it up and found a very similar thread. Maybe [this](https://stackoverflow.com/questions/35006614/what-does-symbol-not-found-expected-in-flat-namespace-actually-mean) helps? – Jason R Stevens CFA Feb 03 '22 at 13:43
  • 1
    The files with extension `.so` are typically c or c++ extensions in Python. The error above means that you don't have the required extension in the directory location to which that last line is pointing. _Typically_ this has to do with system/environment mismatch. Easy things to try: 1) Make sure you're _in your environment_, e.g. `source env/bin/activate` or `pipenv shell` 2) You're on a mac or BSD system based on paths: If this is an ARM-based processor, you may need to use full x86 emulation through Rosetta 2, as a few c-based libs don't compile. I mean it though - just a few. – Jason R Stevens CFA Feb 03 '22 at 13:48
  • I am using the global environment (Python 3.8.9 64-bit), importing numpy and pandas work however matplotlib doesn't. – mka1 Feb 03 '22 at 15:03
  • As a total aside, I'd very much create an environment and not use your global system Python. Pollution w/dependencies is a very real hassle (and completely solved by the language): see the docs -- https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment. If you can't get this all working in a specific environment, [Anaconda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html) has pre-compiled binaries of many packages that "should work". – Jason R Stevens CFA Feb 03 '22 at 15:12
  • I did try it on a virtual environment, but I still got the same error. Will try my luck with Anaconda now, thank you! – mka1 Feb 03 '22 at 16:12

0 Answers0