How to access a module in an environment? - python

I am currently trying to run my python code in a linux terminal and receive the following error:
import matplotlib.pyplot as plt ModuleNotFoundError: No module named 'matplotlib'
When emailing my schools tech help desk when asking them to download the module they stated that my tensorflow/python3 module's environment has the matplotlib module already installed. However being new to linux and python I am unsure how to access this module to run my code. I have set an environment using the following:
$conda activate tesnorflow
which gave me the access needed to use the xml module. Can anyone give me any advice on how I can access this environment correctly to use the matplotlib module. Thank you
import os
import matplotlib.pyplot as plt
import cv2
from matplotlib.widgets import RectangleSelector
from generate_xml import write_xml

Related

matplotlib pyplot error in jupyter notebook

Within Jupyter notebook I can import matplotlib but not the pyplot module:
import matplotlib % works
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
An alternative test also fails:
import matplotlib
matplotlib.pyplot % AttributeError: module 'matplotlib' has no attribute 'pyplot'
However, I can import the pyplot module from the conda command prompt with no errors:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
I got the same results in both the "base" environment and a virtual environment that I created.
Does anyone know what the problem is? I've tried uninstalling and reinstalling the maplotlib package, as well as Jupyter notebook, and conda itself.
This imports matplotlib module/library and then you are trying looks for an attribute or variable defined in matplotlib library named as pyplot; which does not exist. pyplot is just an interface for you to call other relevant interactive state-based functions to plot.
import matplotlib
matplotlib.pyplot
In my opinion you should stick to naming/importing convention defined in documentations of libraries for coherent code.
import matplotlib.pyplot as plt
This is all you need.
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
This means you have to restart your juypter there will be a cmd window that you could have closed I recommend you to restart the juypter and then
import matplotlib.pyplot as plt
if it does not work
open juypter cmd from search bar and write pip install matplotlib after the task is done in jupyter cmd reopen juypter and than use
import matplotlib.pyplot as plt
Not that I understand what the problem was, but what worked for me is uninstalling matplotlib with conda, and re-installing it with pip. Oftentimes, the conda versions are not the latest, and there is probably a problem with the specific version. As of matplotlib 3.6.1 pyplot does work in jupyter (I tried with base), though reinstallation of kiwisolver might be needed as well.

ImportError: no module named miniconda

I've installed miniconda on my mac following the instructions from the python website. However, when I write any new script and try and import miniconda, matplotlib or Pandas, I get the error above.
My script so far
from miniconda import *
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
CORRECTED
my code is now
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
I'm now getting the same import error for pandas and seaborn.
UPDATED:
Turns out the corrected code above works and the issue was with Python runner. The code runs when executed through the terminal. Probably an issue with permissions!
Miniconda is not a module indeed it is a variant of Anaconda python distribution, hence you could not import it. You could get further info about miniconda from the link below.
https://conda.io/miniconda.html

What is the difference between importing matplotlib and matplotlib.pyplot?

I'm still fairly new to python and am wondering if the x.y statement means y is a submodule of x? And if so, doesn't the command:
import matplotlib.pyplot as plt
only import this particular submodule and nothing else? I had to do this in order to get access to the hist function. How does that affect the modules normally imported when calling import matplotlib as plt? Can I get all the modules in matplotlib together under the plt name?
I'm aware that this question is related to what is the difference between importing python sub-modules from NumPy, matplotlib packages But the answer in this question does not tell me if nothing else in matplotlib is imported and how to just import all of matplotlib without worrying about submodules being left out.
Have a look at this codebase tree: matplotlib contains a library of code, while pyplot is only a file of this lib.
import matplotlib
will imports all the files inside this repo. For example to use it:
import matplotlib as mpl
mpl.pyplot.plot(...)
To import pyplot:
from matplotlib import pyplot as plt
# or
import matplotlib.pyplot as plt
plt.plot(...)
One question for you: what console do you use? I guess it's Ipython console or something?
Edit:
To import all:
from matplotlib import *
pyplot(...)
Why do I guess you are using Ipython? Ipython console imports all modules from numpy and some other libraries by default on launch, so that in Ipython console you can simple use: sqrt, instead of import math; math.sqrt, etc. matplotlib is imported in Ipython be default.
I don't know of any way to import all the functions from every submodule. Importing all the functions from a submodule is possible the way you suggested with e.g. from matplotlib.pyplot import *.
Be noted of a potential problem with importing every function; you may override imported functions by defining your own functions with the same name. E.g:
from matplotlib.pyplot import *
def plot():
print "Hello!"
plot()
would output
Hello!
I had conda installed, which has added stuff to ~/.bashrc.
Commenting that made it work for me.

cv2 Module not Found Even though I downloaded it

I am currently in PyCharm and I have the following line of code:
import cv2
Nonetheless, it gives me the error No module named cv2
I went to Preferences > Project Interpreter > + then found and downloaded cv2 just fine. In the Project Interpreter it lists cv2 as installed. I am not sure why it still shows that the module doesn't exist. Is there some way to download cv2 via command line. I am on OS X 10.10.
Here is the code I have so far (all the other imports work just fine):
# Program for OCR
import numpy as np
import cv2
from matplotlib import pyplot as plt
I find that sometimes after installing a module in PyCharm it requires a restart in order for it to work. Also, if you do want to do it in the command line, try pip3 install cv2.

matplotlib does not work in Eclipse

I seem to have a problem that is in parts very similar to the one mentioned here:
Python with eclipse import problem
But unfortunatly just in parts otherwise that would have solved mine as well.
I use Eclipse SDK, Version: 3.7.0 with PyDev 101.
Furthermore I have installed
numpy-1.6.1rc1-win32-superpack-python2.6.exe
and
matplotlib-1.0.1.win32-py2.6.exe
as noted here:
http://matplotlib.sourceforge.net/users/installing.html
I have rebuild all the packages and looks the site-packages are listed.
(by the way as you see it is an Python version installed with ArcGIS )
If I test a script for instance a very simple one like:
import numpy
import matplotlib
import pylab as pl
I get the following error in Eclipse:
import matplotlib
import pylab as pl
from matplotlib.pylab import *
ImportError: No module named pylab
Even though the interpreter for Pydev is pointing to the appropriate version of python and matplotlib is installed properly in there (site-packages) it does not work in Eclipse. In iPython it works perfect.
What still needs to be done to get matplotlib work in Eclipse?
Thanks a lot!
Werner
pylab is in matplotlibs namespace, so this should work:
import matplotlib.pylab as pylab
I found that turning off interactive move and then calling show worked.
import matplotlib.pyplot as plt
#...your code...
plt.ioff()
plt.show()

Categories

Resources