Problems importing matplotlib.pyplot on Mac OS, Anaconda - python

I successfully installed Anaconda, Python2.7, and matplotlib on my Mac OS laptop.
However, when I import matplotlib.pyplot, I get the following errors:
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 5 2017, 02:28:52)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 32, in <module>
import matplotlib.colorbar
File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
import matplotlib.contour as contour
File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
import matplotlib.font_manager as font_manager
File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
ImportError: dlopen(/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Symbol not found: _inflateValidate
Referenced from: /Users/user.name/anaconda2/lib/libpng16.16.dylib
Expected in: /usr/lib/libz.1.dylib
in /Users/user.name/anaconda2/lib/libpng16.16.dylib
>>>
Here are the Conda versions of the libraries mentioned above in the error messages:
conda list matplotlib
# packages in environment at /Users/user.name/anaconda2:
#
# Name Version Build Channel
matplotlib 2.1.2 py27h6d6146d_0
conda list libpng
# packages in environment at /Users/user.name/anaconda2:
#
# Name Version Build Channel
libpng 1.6.34 he12f830_0
conda list zlib
# packages in environment at /Users/user.name/anaconda2:
#
# Name Version Build Channel
zlib 1.2.11 hf3cbc9b_2
Does anyone know how to fix this problem?

Possible cause: the first time you use/import matplotlib, it (tries to) builds a fontcache directory. This is both slow and error-prone.
See: matplotlib taking time when being imported
If it fails or uses wrong permissions, delete the files/directories involved and restart.

I figured it out myself. Anaconda needed libz, which I had already installed with conda and was placed into the default location /Users/user.name/anaconda2/lib on my Mac. However, Python was seeing the libz that came with my Mac in /usr/lib/.
I fixed the problem by unsetting the environment variable LD_LIBRARY_PATH on my system. That apparently allowed Anaconda to find the correct libz.

Related

VSCode ModuleNotFoundError: No module named <...> in Terminal

When I try to import the MWE below from the Terminal in VSCode, I get
ModuleNotFoundError: No module named 'tqdm'
temp.py:
import tqdm
print('hello')
Here is my Terminal session:
(mainenv) Larrys-MBP-5:Desktop larry$ python3
Python 3.9.7 (v3.9.7:1016ef3790, Aug 30 2021, 16:39:15)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import temp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/larry/Desktop/temp.py", line 1, in <module>
import tqdm
ModuleNotFoundError: No module named 'tqdm'
I'm using a conda environment called mainenv. I can confirm (via conda list) that tqdm is installed:
I can also confirm that VSCode is using mainenv as the interpreter:
I get the same error if I replace tqdm with other packages too, like jsonpickle and tabulate. But if I replace it with numpy or networkx, for example, it works fine.
What's going on here?
Did You Use Pip Or npm? When I try to use npm in vscode for python modules it doesn't work, it works for me when I use pip though.

Issues importing pymssql in Python3 while using conda environment

I am having trouble importing a library called pymssql.
I have read previous solutions regarding this issue on stack overflow. The following are the solutions that did not work:
Error importing pymssql
import pymssql Unicode DecodeError in windows 7
I am running a conda virtual environment with Python 3.7.9. The following is the output when I run python in the terminal.
Python 3.7.9 (default, Aug 31 2020, 07:22:35)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
So, this confirmed that I was using the correct Python version. Next, I tried to import a library called pymssql.
This looked like:
>>> import pymssql
This returned an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/__init__.py", line 3, in <module>
from ._pymssql import *
File "src/pymssql/_pymssql.pyx", line 1, in init pymssql._pymssql
ImportError: dlopen(/usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so, 2): Symbol not found: _iconv
Referenced from: /usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so
Expected in: flat namespace
in /usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so
>>>
I have tried conda install pymssql and pip install pymssql. Neither of these solved the issue.

Properly using Anaconda's Python as the PATH's "python"

I've noticed that while I can point my environment variables to Anaconda's Python interpreter, I don't get a fully working Python environment when I execute python in the command prompt. Python will still launch, but many of its packages are "broken."
PROBLEM:
Anaconda's Python interpreter does launch when called from command prompt, but it isn't able to import some of its packages (numpy being one of them used in this example).
EXAMPLE(WINDOWS CMD):
> where python
C:\Anaconda3\python.exe
Great, python calls the right interpreter. Let's go in there and import numpy!
> python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
from . import multiarray
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
This is weird. Let's try launching "Anaconda Prompt", which appears to just be an activated Anaconda environment.
>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
Everything works fine this way.
SUMMARY:
I'm expecting Anaconda's Python interpreter to launch from the command prompt and behave just the way it would if launched from within the Anaconda Prompt (which appears to just be a command prompt with an activated Anaconda environment).
What can I do to be able to get a fully working Anaconda Python interpreter by just launching python from the Window's command prompt? Maybe what I'm really asking is, what do I need to change in my environment variables to ensure that the Anaconda environment is activated when I call on python from the command prompt?
Activating a conda environment can also change the PATH. I suggest you compare PATH before and after activating the environment.

easysnmp library doesn't work on el-capitan

I'm running OSX ElCapitan and I've brew install easysnmp... nevertheless, when I try to import it on python, I get the following error:
Python 2.7.12 (default, Jun 29 2016, 14:05:02)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from easysnmp import Session
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/easysnmp/__init__.py", line 1, in <module>
from .easy import ( # noqa
File "/usr/local/lib/python2.7/site-packages/easysnmp/easy.py", line 3, in <module>
from .session import Session
File "/usr/local/lib/python2.7/site-packages/easysnmp/session.py", line 8, in <module>
from . import interface
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/easysnmp/interface.so, 2): Symbol not found: _netsnmp_transport_config_compare
Referenced from: /usr/local/lib/python2.7/site-packages/easysnmp/interface.so
Expected in: flat namespace
in /usr/local/lib/python2.7/site-packages/easysnmp/interface.so
easysnmp sites state that it requires net-snmp 5.7 although el capitan is shipped with 5.6... so I've "brew installed" snmp-get 5.7.
Brew doesn't change the symlinks so I'm assuming python is still looking at version 5.6.
Install output :
==> make
==> make install
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.
OS X already provides this software and installing another version in
the parallel can cause all kinds of trouble.
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:
LDFLAGS: -L/usr/local/opt/net-snmp/lib
CPPFLAGS: -I/usr/local/opt/net-snmp/include
==> Summary
🍺 /usr/local/Cellar/net-snmp/5.7.3: 538 files, 12.8M, built in 4 minutes 34 seconds

matplotlib imported but pyplot cannot be imported

I have two pythons in my mac, they are python 3.4 and 2.7
I can import matplotlib in python 3.4 but as I run
from matplotlib import pyplot
I get the error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from matplotlib import pyplot
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/backend_bases.py", line 62, in <module>
import matplotlib.textpath as textpath
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/textpath.py", line 18, in <module>
from matplotlib.mathtext import MathTextParser
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/mathtext.py", line 63, in <module>
import matplotlib._png as _png
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/_png.so, 2): Library not loaded: libpng15.15.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/_png.so
Reason: image not found
I am beginner. Can anybody help me figure out what is going on and what should I do?
This is your problem.
Library not loaded: libpng15.15.dylib
Use this sequence of commands to get to where you want.
pip uninstall matplotlib
rm -rf /tmp/pip-build-root/
brew update
brew install libpng --universal
brew install freetype --universal
pip install matplotlib
(venv) venv ~ python
Python 3.4.3 (default, Mar 23 2015, 04:19:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot
>>>
Simple solution to the issue that worked for me:
1- in terminal type $open -a Finder /usr
2- right click on local and do get info
3- unlock the lock at the bottom
4- click + sign and add your user to the list and give read/write privileges
5- click on the gear sign at the bottom and choose apply to enclosed items to recurse under that directory and assign privileges too all directories beneath it.

Categories

Resources