How to import in python 3? - python-3.x

I am self-learning Python and all the online courses use labs where all libraries are already imported. Whenever I try to import numpy or pandas or any other library I receive this message:
"Traceback (most recent call last):
File "<pyshell#6>", line 1, in
import numpy as np
ModuleNotFoundError: No module named 'numpy'"
What am I doing wrong?

import-error-no-module-named-numpy
ModuleNotFoundError is thrown when a module could not be found
Support for Python 3 was added in NumPy version 1.5.0
you do not install numpy Correctly
pip uninstall numpy
pip3 install numpy
I strongly recommend you use Virtualenv to install it numpy
pip install virtualenv
go to folder of your code use
virtualenv venv
//Windows
venv\Scripts\activate
//Linux
source venv/bin/activate
you can use conda to install numpy
download conda from here coda GUI installer
Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
If you want to install from conda-forge
conda config --env --add channels conda-forge
The actual install command
conda install numpy

Related

Issue with importing Seaborn

When I try to import seaborn the import give me the below error message:
ModuleNotFoundError Traceback (most recent call last)
in
----> 1 import seaborn
ModuleNotFoundError: No module named 'seaborn'
I have tried the below steps to install:
pip install seaborn
python -m pip install seaborn
!conda install seaborn
%%bash pip install seaborn
pip3 install seaborn
conda install seaborn
sudo pip install utils ...and then install seaborn again
pip uninstall scipy and then install scipy package again.
These are the solutions I found, I closed iTerm and started it again and none of these worked.
Make sure you have Python and pip installed on your computer. Reinstall it if you have it. Try pip install seaborn. Also, make sure you are installing it in the same directory as where you are working. If that issue still persists, there must be a problem with your shell or computer.
PS:
If you are using Juypter Notebook do in a cell:
import sys
!{sys.executable} -m pip install seaborn

Numpy cannot be imported even though it is installed

I am using Linux Mint 19.3 XFCE.
I have installed Numpy through pip3. pip3 was not installed already, and I installed pip3 thorugh apt.
The default version of python3 that came with the OS is 3.6.9. Since I am not supposed to change the default version of Python that comes installed with the OS, I kept that. And I installed a newer version, 3.8.0 with snap.
The command was-
sudo snap install python38
And now, whenever I need to work with the interpreter, I just type python38 into the terminal and get on with it.
I recently installed Numpy with pip3-
pip3 install numpy
and it shows up when I run pip3 freeze
:
It is listed as-
numpy==1.18.1
But when I enter the Python interpreter through typing python38 into my terminal, and type in import numpy, I am shown an error:
import numpy as np
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'numpy'
However, when I try the same with Python 3.6.9, it works. Numpy is improted, works just fine. (This time I enter Python interpreter by typing python3)
Now, how do I permanently solve this? That is, being able to import Numpy when working in Python 3.8 in terminal.
This may be the reason because your pip is configured for the default version of python in the machine(Python 3.6.9 in your case). You may change your default python version.
Or
You could run pip and install your python package for specific python version like
python_version -m pip install your_package
eg
python38 -m pip install numpy

Mac OS X: "ModuleNotFoundError: No module named 'numpy'"

After I have reinstalled Anaconda, I can not import NumPy any more on Python 3:
import numpy as np
Output:
ModuleNotFoundError: No module named 'numpy'
I have tried
pip install numpy
I try to install it again, but I get:
pip install numpy
Output:
Requirement already satisfied: numpy in /anaconda3/lib/python3.7/site-packages (1.16.3)
Screenshot of some of the above
This command worked for me
python3 -m pip install numpy
You probably have multiple NumPy versions installed on your system and need to manually delete the old one.
Open Terminal and run:
pip list
Remember or notate the NumPy version from the list
Then run Python in Terminal or write and execute this code in your IDE:
import numpy
print(numpy)
print(numpy.version)
This should print two lines: numpy package location + numpy version like this:
<module 'numpy' from '/usr/local/lib/python2.7/site-packages/numpy/init.pyc'>
1.16.1
If the NumPy version reported here is different than the one reported by pip list, then go to the NumPy package location printed above and delete that package folder
Try importing NumPy now. If you previously installed it, it should work. If you didn't install it then go and install it now. After installation all should work fine.
You are using a Conda environment to run your program. So you should run:
conda install numpy
If you use IDE like PyCharm, it will be easy to install packages by a mouse click (Install packages).

No module named 'pandas_datareader' in Jupyter (Anaconda) after I run pip3 install pandas_datareader

I am trying to learn pandas and want to load some stocks data. I was following a course which advised me to load pandas.io.data, but this did not work as io.data was depreciated. So I decided to use pandas-datareader instead. But I am struggling to instal it on mac in Anaconda (Jupiter notebook).
First time I run import pandas_datareader as pdweb I got ModuleNotFoundError: No module named 'pandas_datareader'. Not surprising as I never used this before so I run pip3 install pandas_datareader in Terminal which successfully installed itself. However, Jupiter notebook is still giving me the same error. At this point, I tried running in Terminal conda install -c https://conda.anaconda.org/anaconda pandas-datareader but it did not work as -bash: conda: command not found.
Please help. I am looking for a detailed explanation as I am not too techie.
What I run in Jupiter
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
import pandas_datareader as pdweb
import datetime
Result
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-6-da568f513c93> in <module>
2 from pandas import Series, DataFrame
3 import pandas as pd
----> 4 import pandas_datareader as pdweb
5 import datetime
ModuleNotFoundError: No module named 'pandas_datareader'
Just run
conda install -c anaconda pandas-datareader
as per instructions here.
In my experience, if you're using conda, you should never install with pip unless you're sure conda doesn't have it. Try searching anaconda.org to see which -c source to use.
The problem was with conda command not being set up during the installation of Anaconda. I solved this by removing Anaconda with App Cleaner & Uninstaller Pro (free soft) and reinstalling later version.
After reinstallation of Anaconda command below worked like a charm.
conda install -c anaconda pandas-datareader
If you using Anaconda and still have issues installing panads-datareader using the conda command 'conda install -c anaconda pandas-datareader'. or the installation is successful but the import pandas_datareader.data as web still gives an error.
Locate where your 'site-packages % ' directory and cd to the site-packages % dir and then run the installation command 'conda install -c anaconda pandas-datareader '. It will work.
For the install, try using
pip
instead of
pip3

Cannot import python package installed to virtualenv

I installed the python package Pillow to my virtualenv, but cannot import it. pip list | grep Pillow shows that it has been installed, and the virtualenv has been activated, but import Pillow still returns:
ImportError: No module named 'Pillow'
The virtualenv was created with
virtualenv -p python3.5 MainEnv
Pillow was installed with
pip install Pillow
Why can I not import the installed package?
I believe you pip install as Pillow, but import as PIL:
http://www.pythonforbeginners.com/gui/how-to-use-pillow

Resources