ModuleNotFoundError: No module named 'numpy' AWS SageMaker Studio Lab - numpy

I installed the numpy with Conda, pip and pip3, also tried installing from the requirements.txt file but I am still getting the following issue. Has anyone encountered this before and could please kindly suggest something?
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_732/1818554654.py in
1 import os
----> 2 import numpy as np
3 import matplotlib.pyplot as plt
4 import pandas as pd
5 import random
ModuleNotFoundError: No module named 'numpy'

numpy is built-in the default:Python environment in Studio Lab. If you open a new notebook with the File -> New -> Notebook, and choose the default:Python kernel, you should be able to import numpy without having to install the package.
If you would like to have a reproducible environment with a set of packages, see this sample repo on creating custom environments.

Numpy isn't preinstalled as a module you will need to install it yourself using pip or conda. Using notebooks just include a code block with the following:
!python -m pip install numpy
Suggestion to compile a list of required modules in a 'requirements.txt' file and install using:
!python -m pip install -r requirements.txt
Just as an FYI, the base package list is as follows:
Python 3.9 bzip2 build-essential curl git libgl1-mesa-glx nano rsync
unzip wget ca-certificates pip ipykernel-6.4
Have a look at this link for more info.
SageMaker - Manage your environment

Related

How can I fix the error "Could not find a version that satisfies the requirement torch" when trying to install PyTorch using pip?

I tried importing 'PyTorch' as 'torch' as most recent, but visual studios/cmd cannot locate nor download the package.
# Import the required modules import torch import torch.nn as nn import torch.nn.functional as F
same goes with
import tensorflow as tk
I wasn't able to download the package:
not able to find the file.
I tried both:
pip install --upgrade pip
and:
pip install torch==1.0.2
I tried all types such the basic pip install [package] to connecting the wsl.file through path
Installing older torch needs --find-index flag to pip, like torch 1.5 with cuda 10.1:
pip install torch==1.5.0+cu101 torchvision==0.6.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html
Reference:
https://pytorch.org/get-started/previous-versions/

How to plot an image using PIL or Matplotlib

I am trying to plot an image in python using either PIL or Matplotlib. I have tried both
import matplotlib.pyplot as plt
and
from PIL import Image
But I get the same error:
ImportError: cannot import name '_imaging' from 'PIL'
I have updated pillow and matplotlib packages but no success
You can read the image using open cv and then plot it using matplotlib.
img_name = cv2.imread("image.jpeg")
plt.imshow(img_name)
plt.show()
This usually happens when folk use python and pip from different packages, distributions or versions. I mean you install a package with pip into one package/distribution/version and then try and use it from a different package/distribution/version of Python.
So, if you normally start Python using:
python
then run:
type python
Whereas if you normally start Python using:
python3
then run:
type python3
On my machine I get:
type python3
python3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
so now I know which Python I am using.
Now you need to see which pip you are using. So, if you normally start pip with:
pip
then run:
type pip
Whereas if you normally start pip with:
pip3
then run:
type pip3
On my machine I get:
type pip3
pip3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
So now you can see that my pip3 is installing packages for python3 exactly where it can find them.

Install graphiz on AWS Sagemaker

I'm on a Jupyter notebook using Python3 and trying to plot a tree with code like this:
import xgboost as xgb
from xgboost import plot_tree
plot_tree(model, num_trees=4)
On the last line I get:
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/plotting.py in to_graphviz(booster, fmap, num_trees, rankdir, yes_color, no_color, **kwargs)
196 from graphviz import Digraph
197 except ImportError:
--> 198 raise ImportError('You must install graphviz to plot tree')
199
200 if not isinstance(booster, (Booster, XGBModel)):
ImportError: You must install graphviz to plot tree
How do I install graphviz so I can see the plot_tree?
I was finally able to learn that Conda has a package which can install it for you. I was able to get it installed by running the command:
!conda install python-graphviz --yes
Note the --yes is only needed if the installation needs to verify adding/changing other packages since the Jupyter notebook is not interactive once it is running.

ModuleNotFoundError for pandas_datareader: Jupyter Notebook using different packages from conda environment

I am using Anaconda windows v5.3.
I am getting the error:
ModuleNotFoundError: No module named 'pandas_datareader'
When I tried to print out the packages used by Jupyter Notebook, I realized that pandas_datareader is not in, and a different version of pandas (0.23.0) is used:
import pkg_resources
for i in pkg_resources.working_set:
print(i)
Output
...
pandocfilters 1.4.2
pandas 0.23.0
packaging 17.1
openpyxl 2.5.3
...
This differs from the library installed in the pyfinance environment:
>conda list
# Name Version Build
pandas 0.20.3 py36_0
pandas-datareader 0.4.0 py36_0
Hence, pandas_datareader seem to work in the python shell in the command prompt, but not in jupyter notebook. Are there anyways to sync jupyter notebook environment to the conda environment?
I realized to sync jupyter notebook you just have to do:
conda install jupyter

Installed BeautifulSoup but still get no module named bs4

I'm using a Jupyter notebook, Python 3.5, and a virtual environment.
Within my virtual env I did:
(venv) > pip install BeautifulSoup4
Which seemed to run fine b/c the terminal output was:
Downloading beautifulsoup4-4.6.0-py2-none-any.whl (86kB)
100% |████████████████████████████████| 92kB 297kB/s
Installing collected packages: BeautifulSoup4
Successfully installed BeautifulSoup4-4.6.0
But when I run my Jupyter notebook I get:
ImportError Traceback (most recent call last)
<ipython-input-1-5fe69888b1a1> in <module>()
5 from itertools import groupby
6 import pickle
----> 7 import bs4
8 import matplotlib.pyplot as plt
9 get_ipython().run_line_magic('matplotlib', 'inline')
ImportError: No module named 'bs4'
And I can't change the line import bs4 to from bs4 import BeautifulSoup which I've seen on other SO posts b/c its read-only and I'm not supposed to
Since you are using Python 3, I think you needed to do
pip3 install BeautifulSoup4
Just pip install would have been for the Python 2 package.
Other things to check:
First make sure you ran both the BeautifulSoup install in your virtualenv, and the python3 prompt or Jupyter notebook in your virtualenv. If you did the install in your root environment and the notebook is run in your virtualenv, or vice versa, you might have a mismatch in your site-packages directory.
Ensure that your package indeed installed by typing pip3 list at a command prompt, and noting that it appeared in the list.
Check that you Jupyter notebook is indeed running a Python 3.5 kernel (upper right hand corner of the browser window)
Check that the path where the package is installed is in your sys.path
Open a Python3 prompt or in Jupyter and run
import sys
print (sys.path)
I had the same problem, bs4 was installed in my virtual environment along with Jupyter Lab. Jupyter would throw the ModuleNotFound Error but if I opened the notebook in Visual Studio Code and chose the .venv as the interpreter, it worked.
I got it to work in Jupyter Lab by uninstalling from the virtual environment and installing with sudo apt-get install python3-bs4 instead.