Use netcdf4 with Jupyter Notebook - python

I am Currently Working on my Bachelors Degree, where I am to do a Data-Analysis using Jupyter Notebook. Normally I only write "normal" Python and Java code.
For this I need to open .NC Files, and a quick Google search told me to use netcdf4. Within my Anaconda Manager this was not a problem, but I can't seem to get it to work within Jupyter.
Here you can see what I tried. I can do "import sys" and "!{sys.executable} -m pip install netcdf4" (which i got from another Stackoverflow Thread), but "import netcdf4 as nc" doesnt work. (Error Code
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-32-6eba77c50763> in <module>
----> 1 import netcdf4 as nc
ModuleNotFoundError: No module named 'netcdf4'
Can somebody tell me what I am doing wrong?

The netcdf4 package is not installed in your current environment. You could do the following in your notebook:
! pip install netcdf4
or conda install, depending on how you want to intall the package.

I found it out after hours of trial and error.
Of course its "!{sys.executable} -m pip install netcdf4" (netcdf all lowercase letters), but to install its "import netCDF4 as nc" (CDF capitalized).
Those are the days i'd rather have studied economics.

Related

How do I fix the issue "ModuleNotFoundError: No module named 'sklearn'

I'm using a Mac. I have Anaconda installed. When I type import numpy or import matplotlib I don't run into any issues. The only issue I'm having is with sklearn.
I'm fairly new to CS/ DS. Any help/ tips is greatly appreciated.
I've tried uninstalling sklearn and reinstalling. I've pretty much tried every solution on StackOverflow. The only thing I haven't tried is reinstalling anaconda.
import sklearn
Traceback (most recent call last):
File "", line 1, in
import sklearn
ModuleNotFoundError: No module named 'sklearn'
I expect to get no result just like with matplotlib and numpy which means everything works, but in lieu of that I get that output.
Do you have sklearn installed? It should be in some requirement, but it seems that someone needs it as a dependency and does not find it.
pip freeze | grep sklearn
It will tell you if you have it installed.
Do you work in a virtualenv?
If you do not have it installed try to do it and if not, pass the traceback.
if you work on mac you can also try updating xcode and updating yourself pip
xcode-select --install
pip install --upgrade pip

jupyter notebook from GCP instance not able to import wget or fastai

I have a VM instance running on google cloud platform. I followed the instructions here: https://towardsdatascience.com/running-jupyter-notebook-in-google-cloud-platform-in-15-min-61e16da34d52 to setup a Jupyter notebook for the instance. If I open a python3 notebook I can import certain packages (etc., matplotlib), but for some reason if I try to import wget or fastai I receive
ImportError: No module named 'fastai'
or
ImportError: No module named 'wget'
The instance was a prebuilt pytorch deep learning setup which is supposed to come with fastai, and wget works within the SSH terminal before any kind of install. I've tried installing wget and fastai anyway with conda and pip commands but neither make these modules accessible in jupyter. I'm assuming since these modules are prebuilt in this instance I need to access/import them differently somehow?
If in the SSH terminal (without any conda/pip install) I ask whereis wget, I receive:
wget: /usr/bin/wget /opt/anaconda3/bin/wget
/usr/share/man/man1/wget.1.gz /usr/share/info/wget.info.gz
If in the SSH terminal (without any conda/pip install) I ask whereis fastai, I receive:
fastai:
How can I get the import to work?
import fastai
import wget
If in my Jupyter notebook I run:
!pip install wget
import wget
I get:
/home/me/anaconda3/lib/python3.5/site-packages/pip/_vendor/requests/init.py:83:
RequestsDependencyWarning: Old version of cryptography ([1, 3]) may
cause slowdown. warnings.warn(warning, RequestsDependencyWarning)
Requirement already satisfied: wget in
/home/me/anaconda3/lib/python3.5/site-packages (3.2)
--------------------------------------------------------------------------- ImportError Traceback (most recent call
last) in
4 import yaml
5 get_ipython().system('pip install wget')
----> 6 import wget
ImportError: No module named 'wget'
Quick update:
it does seem to be some issue with wget and fastai already being installed. I cannot import, but I can use the modules within my jupyter notebook. So wget.download(stuff) raises a
NameError: name 'wget' is not defined error
but !wget.download(stuff) works.
Still would prefer to be able to import. But as is, is there a way to refer to fastai base classes? Right now if I try
class newClass(LearnerCallback):
new stuff
I get:
NameError: name 'LearnerCallback' is not defined
Which I assume is due to the un-imported fastai. How do I get around this?
Try using:
%conda install wget fastai

Trying to import Levenshtein on (Jupyter ipython + windows)

I'm new to python and Anaconda and I tried to run my code which contains Levenshtein import, however, it shows me this error:
ImportError Traceback (most recent call last)
<ipython-input-1-896847aaaa86> in <module>()
1 import pandas as pd
2 import numpy as np
----> 3 import Levenshtein as lv
4 import math
5 import re
ImportError: No module named 'Levenshtein'
Any recommendations regarding this, I owe you tons of thanks
The error happens because Anaconda does not ship with the Levenshtein package. You'll have to install it.
Usually, you want to install python packages on Jupyter Notebook by using the conda command. I was having trouble installing this package in particular on a Windows, however, simply because Windows Defender held out the process indefinitely.
The way I managed to install it as by using pip in Windows's cmd. First you have to figure out were the python executable from your Notebook is running.
In your notebook:
import sys
{sys.executable}
Copy the path the cell outputs, open the cmd. There, use the following command:
[PASTE THE PATH HERE] -m pip install python-levenshtein
After that you should be able to import Levenshtein.

ModuleNotFoundError Despite being Installed

I'm having trouble loading a package that is installed in my environment and have no idea why..
conda list
fbprophet 0.3.post2 py36_0 conda-forge
When I request for libraries installed in conda, it clearly shows that I have fbprophet installed. Now, when I import it, a ModuleNotFoundError exception is thrown.
from fbprophet import Prophet
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-83be6864a7aa> in <module>()
----> 1 from fbprophet import Prophet
2 import numpy as np
3 import pandas as pd
ModuleNotFoundError: No module named 'fbprophet'
Has anybody run into this issue? What can I do to troubleshoot this?
My suggestion would be creating a virtual environment for your python first, then conda install your package in the virtual environment.
My script was reading from my pip install list where the module fbprophet did not exist. I went back and installed it there and everything worked. Lesson learned.
Always check where your libraries are being read from.
You may have more than one Python installation. Thus you might installed the library in one Python installation and tried to call it from another one. Please let me know if you have more than one Python installation by printing the result os this CMD command where python.

Installing a package using pip or easy_install with ipython / conda

I'm trying to install 'frida' on my Anaconda environment, and although it installs fine, I keep getting an error when importing it from IPython:
In [4]: import frida
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-58e5c65e5010> in <module>()
----> 1 import frida
ImportError: No module named frida
On the normal python shell the import works fine. The problem is only with IPython.
I've tried using easy_install from the conda command line - no go.
I've tried this from an IPython shell:
In [5]: easy_install.main( ["-U","frida"] )
And the package install successfully, but IPython still gave an error when importing it.
pip doesn't find the package.
I seem to be missing something with the IPython packages - How does one install packages to IPython?
If you are working in virtualenv, then installing IPython into virtualenv resolve such problems.
Usually, IPython is giving this advice when it start inside of virtualenv.

Categories

Resources