Working on a Mac in Atom.
Running this code
from matplotlib import pyplot as plt import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np atom1 = np.random.uniform(-1, 1, 3) atom2 = np.random.uniform(-1, 1, 3) atom3 = np.random.uniform(-1, 1, 3) atom4 = np.random.uniform(-1, 1, 3) colors = ['r', 'g', 'b', 'k'] atoms = np.vstack([atom1, atom2, atom3, atom4]) ax = plt.subplot(111, projection='3d') ax.scatter3D(atoms[:, 0], atoms[:, 1], atoms[:, 2], c=colors) plt.show()
and it always returns
ImportError: cannot import name 'ListedColormap' from partially initialized module 'matplotlib.colors' (most likely due to a circular import) (/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/colors.py)
I recognise that this is a partially initialised module but I don’t know how to fix it. I tried uninstalling and reinstalling mpl and np but it still returns the error message.
Traceback (most recent call last): File "/Users/Audey/Desktop/array.py", line 1, in <module> from matplotlib import pyplot as plt File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/__init__.py", line 109, in <module> from . import _api, _version, cbook, docstring, rcsetup File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/rcsetup.py", line 27, in <module> from matplotlib.colors import Colormap, is_color_like File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/colors.py", line 52, in <module> from PIL.PngImagePlugin import PngInfo File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/PIL/PngImagePlugin.py", line 41, in <module> from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/PIL/ImagePalette.py", line 19, in <module> import array File "/Users/Audey/Desktop/array.py", line 1, in <module> from matplotlib import pyplot as plt File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/pyplot.py", line 49, in <module> import matplotlib.colorbar File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/colorbar.py", line 21, in <module> from matplotlib import _api, collections, cm, colors, contour, ticker File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/collections.py", line 20, in <module> from . import (_api, _path, artist, cbook, cm, colors as mcolors, docstring, File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/artist.py", line 15, in <module> from .cm import ScalarMappable File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/cm.py", line 26, in <module> from matplotlib._cm_listed import cmaps as cmaps_listed File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/_cm_listed.py", line 1, in <module> from .colors import ListedColormap ImportError: cannot import name 'ListedColormap' from partially initialized module 'matplotlib.colors' (most likely due to a circular import) (/Users/Audey/Library/Python/3.10/lib/python/site-packages/matplotlib/colors.py)
The problem, evident from
"/Users/Audey/Desktop/array.py" in your traceback is that you’ve named your script
array is also a standard library module, so Python’s import machinery gets "confused" and where PIL would like to import the standard library module, it gets your module.
Rename your script to something that’s not a standard library module, say,
audeys_array.py. Also take care to see that there isn’t an
array.pyc left behind.
Answered By – AKX
Answer Checked By – Willingham (BugsFixing Volunteer)