Anaconda / IPython / matplotlib Issue - python

I've just completed the installation of Anaconda on my MacBook Pro (running OS X 10.10 Yosemite). I installed the recommended package, which includes Python 2.7.
The versions I have of the (I think) pertinent packages are:
Numpy: 1.9.0
Scipy: 0.14.0
IPython: 2.2.0
matplotlib: 1.4.0
I was following the instructions for testing the installation listed here:
https://python4astronomers.github.io/installation/python_install.html#test-the-installation
Everything was fine, except for running IPython with matplotlib integration. Based on the guide on that page (which do not tell you to explicitly import matplotlib), and on my limited knowledge of IPython, I was under the impression that invoking IPython with
ipython --matplotlib
would "automagically" import the matplotlib modules somehow.
However, when I typed the line below into the IPython interpreter session:
print(matplotlib.__version__)
an error message was displayed which indicated that matplotlib was unavailable:
NameError
Traceback (most recent call last)
<ipython-input-1-dc737d9da186> in <module>()
----> 1 print(matplotlib.__version__)
NameError: name 'matplotlib' is not defined
However, IPython does say this when it is invoked:
IPython 2.2.0 -- An enhanced Interactive Python.
...
Using matplotlib backend: MacOSX
I know that IPython develops at a rapid pace, so my question is:
Did I miss something in the installation process (although it was performed using the Anaconda pre-compiled binary installer), or has the was matplotlib integration is achieved with IPython changed, or could the set of instructions I followed for testing the installation be missing something? I checked that site for a way to contact those guys before coming here, but I could not find it.
I've been through period of extraordinary hair loss due to having pulled huge tufts of it out while trying to sort out $PATH issues with Python and related problems while using Python on OS X, so this time I went with Anaconda and am just trying to quash any problems that may arise before I further complicate the situation by installing other modules not included with Anaconda.
Thanks in advance!

All the --matplotlib option does is set IPython up to display images generated by matplotlib so they are not blocking. It doesn't import anything. You still have to import matplotlib manually.

Related

Module not found in Spyder outside of Anaconda

For a training course, I had to install Anaconda and launch Spyder from there. This used to work. However, I want to understand more about Python, because I have little experience with programming environments (I only use R and Stata up to now).
I stumbled on this issue: when I open Spyder directly (presumably I have downloaded a standalone version), most syntax runs well, but I cannot get the 'seaborn' module to work.
I tried pip install seaborn, pip3 install seaborn, conda install seaborn, but to no avail (outside of Anaconda, that is). I also tried updating spyder, homebrew, python and anaconda to the most recent versions. Probably this caused further issues discussed below.
My Python version is 3.9.7 (in terminal), Python3 is 3.9.9 (in terminal and through Anaconda), and through Spyder directly it is 3.9.5. Spyder IDE is 5.2.1, under Anaconda it is 5.1.5. I have no idea why these are all different, where to find the right paths, and how to update the Spyder version of Python and how to add the 'seaborn' module to that environment. It is possible to select a path for the Python interpreter in the Spyder preferences, but which path to take is unclear to me.
In short: how can I get seaborn to work in Spyder without having to rely on Anaconda (which seems a bit bloated to me and is slower to start up)?
Meanwhile, real problems occurred: I can't open Spyder from Anaconda any longer (so seaborn is not totally out of reach). This is the message:
/Users/myname/opt/anaconda3/bin/pythonw: line 3: /Users/myname/opt/anaconda3/python.app/Contents/MacOS/python: No such file or directory
This is the simple syntax I wanted to try:
# Seaborn example
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_theme(style="whitegrid")
rs = np.random.RandomState(365)
values = rs.randn(365, 4).cumsum(axis=0)
dates = pd.date_range("1 1 2016", periods=365, freq="D")
data = pd.DataFrame(values, dates, columns=["A", "B", "C", "D"])
data = data.rolling(7).mean()
sns.lineplot(data=data, palette="tab10", linewidth=2.5)
Giving the error message: ModuleNotFoundError: No module named 'seaborn'
Probably this question is related: installed module in anaconda prompt shell but module not found in spyder?
I am working on an M1 MBA under Monterey.
Many thanks
I found the solution, which is to create a new environment and link to it, on the Spyder FAQ:
https://docs.spyder-ide.org/5/faq.html#using-packages-installer
The video was helpful: https://youtu.be/i7Njb3xO4Fw
It seems I can use the Python version installed through conda (in opt/anaconda3) and base an 'environment' on this, adding modules by first activating the environment and then using conda install.
Meanwhile, I still cannot launch Spyder from Anaconda-Navigator.
/Users/myname/opt/anaconda3/bin/pythonw: line 3:
/Users/myname/opt/anaconda3/python.app/Contents/MacOS/python:
No such file or directory
Probably reinstalling will solve this. But I'm good for now.

Unable to import 'matplotlib.pyplot'

I know this question is asked a lot, I searched for the last three hours for an answer, but couldn't solve my problem.
As soon as I try to:
import matplotlib.pyplot as plt
my Visual Studio Code IDE tells me that: Unable to import 'matplotlib.pyplot'
My current version of Python is:
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
as you can see, I used the Anaconda package, hence matplotlib should be included. I am Using Mac OS 10.15.2
If I enter
import matplotlib.pyplot as plt
it does import that, however, as soon as I try a basic example, like
x = [1,2,3]
y = [2,4,1]
plt.plot(x, y)
I obtain:
[<matplotlib.lines.Line2D object at 0x10c23d950>]
And a blank token in my Dashboard pops up, which I can only force to quit.
In the course of trying to solve this problem, I tried anything I saw that was related to this topic, although I did not understand everything of it. I hope I did not make a real damage.
My last step was completely deinstalling anaconda and reinstalling.
Thanks for taking your time!
The module can be imported, but your IDE says module not found, means your linter (vscode uses pylinter) is not configured correctly.
Start PyLint from correct anaconda environment in Visual Studio Code
First you need to install package matplotlib using conda console in your project
conda install -c conda-forge matplotlib
You also can install package using PIP
python -m pip install -U matplotlib
And Finally import your package in your source code
import matplotlib.pyplot as plt
Or another Way you can import
from matplotlib import pyplot as plt
Having come here with this error myself, especially if you're using VS Code you need to make sure that the version of Python you're running in VS Code is the same one that's running on your OS. Otherwise, you can conda install or pip3 install all you want into the OS, but VS Code and Jupyter Notebooks won't know anything about those modules. There are a few ways to figure this out, but mostly it's making sure that the Python version you see when typing python --version in the terminal matches the interpreter shown in the top-right corner of your VS Code window.

Import package works in jupyter notebook but not in iPython and Anaconda cmd

When searching about this issue, I came across some questions asking the opposite, i.e., package opens in iPython but not in Jupyter Notebook. But in my case, its the opposite. That's why I posted this question.
I added path\to\anaconda3 and path\to\anaconda3\Lib\site-packages in the environment variable, but it doesn't solve the issue.
I can see the packages in the site-packages folder:
But I just can't import some of the packages in iPython:
or with python in the anaconda cmd:
But it works fine in Jupyter Notebook:
What do/can I do to fix this?
Here's some more info if it helps:
(base) C:\Users\h473>where python
C:\Users\h473\AppData\Local\Continuum\anaconda3\python.exe
(base) C:\Users\h473>where conda
C:\Users\h473\AppData\Local\Continuum\anaconda3\Library\bin\conda.bat
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\conda.exe
(base) C:\Users\h473>where pip
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\pip.exe
P.S.: It doesn't happen for all packages, only some packages, as shown for pandas, numpy and matplotlib in the screenshot below.
When you are using matplotlib (and seaborn is built on top of it) it needs to use a so called backend that is used to display the actual GUI with the plot in it once you execute for example matplotlib.pyplot.show().
When you are running a Jupyter Notebook with matplotlib in inline mode (default I think, but not sure), then a Jupyter specific backend is used (module://ipykernel.pylab.backend_inline). That makes sense, since the plots should not appear in separate windows, but be displayed inside the notebook itself.
When you are in an interactive python or iPython session however, Qt5 was used as
import matplotlib
print(matplotlib.rcParams["backend"]) # this prints the backend that would be loaded when trying anything with pyplot
has revealed. Since you get the error youa re getting, it looks like your QT5 installation is broken. You can try to reinstall them using the conda commands, but for now you could also fall back to using a different backend, that you need to specify before trying to load seaborn:
import matplotlib
matplotlib.use("TkAgg") #use backend TkAgg
import seaborn
You can also change the default backend being loaded to TkAgg by creating a matplotlibrc file in C:\Users\<your name>\.matplotlib\ with
backend : TkAgg
in it.

ImportError: No module named 'matplotlib'

Brand new to Python (typically program in MSDN C#) and I'm trying to make use of the matplotlib to generate some graphics from .csv files
I've downloaded & installed Python as well as Anaconda onto my Windows 10 machine, the versions are Python 3.5.2 and Anaconda 4.1.1
I open up the Python "notepad" interface and do
import matplotlib.pyplot as plt
plt.plot([1,2,3],[3,2,1])
plt.show()
but when I run the code I get the error:
ImportError: No module named 'matplotlib'
I've looked at some other posts for this but they all seem to be in regard to Mac OSX or Linux. Some have pointed to multiple installs of matplotlib, but I haven't turned up such a situation so far. What might be causing this, or how can I troubleshoot it?
**Edit:
The path returned to me from the import sys recommended in the comments gave me this response
['C:\Users\a.watts.ISAM-NA\Desktop',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\python35.zip',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\DLLs',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib\site-packages',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib\site-packages\setuptools-26.1.1-py3.5.egg']
You essentially have 2 versions of python on your system - the standard one you downloaded and the one that ships with Anaconda. When you are running code in the IDLE you are using the standard version (in C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\python.exe) where matplotlib isn't installed which is why you are getting the error.
You need to use the Anaconda version (C:\Users\a.watts.ISAM-NA\AppData\Local\continuum\anaconda3\python.exe) that comes with the scientific stuff already setup. It looks like your system is using this one from the cmd so if you run scripts from there it should use the Anaconda version. If you want to use something more interactive you can also use spyder - the Anaconda version of the IDLE - or run jupyter notebook from cmd to get a browser based platform for interactive development

ipython (version 3.2.1) support in LightTable (version 0.7.2)

I've just started using Light Table and can't get iPython working under OSX 10.10. Relevant version numbers are iPython 3.2.1, Python 3.4.3 or 2.7.10, and LightTable version 0.7.2 (installed via a Homebrew Cask and .zip file).
Evaluating Python code works as I'd expect and I get nice tab-completion, but I can't get any graphical output. The "Connection Bar" shows a python connection rather than ipython even though ipython is available on the command line and I can successfully run ipython notebook and have it pop up a web-browser (this requires a working copy of pyzmq).
Neither Python 2.7 nor 3.4 work, and I've tried the official LT .zip as well as installing as brew cask install lighttable.
Changing my User.behaviours to explicitly reference the "correct" virtualenv:
[:app :lt.plugins.python/python-exe "/Users/smason/.virtualenvs/py3/bin/python"]
[:app :lt.plugins.python/ipython-exe "/Users/smason/.virtualenvs/py3/bin/ipython"]
doesn't seem to affect things (verified by getting appropriate errors when misspelling binary, and by running print(sys.version) and looking at output in console).
The problem was that iPython support with Light Table is pretty out of date. It is using features that were exposed in pre-1.0 versions of iPython and it's not been updated while iPython's API has changed.
The most minimal change to Light Table that fixed this for me is in:
https://github.com/LightTable/Python/pull/31
If you're patching your own install you would want to modify:
LightTable.app/Contents/Resources/app.nw/plugins/python/py-src/ltipy.py
but with iPython 4 being released yesterday more code needs to change—hopefully (if you're reading this) I'll get around to making this larger change.

Categories