Easy install python modules for Jupyter notebooks? - python

Lately I've been learning a bit about Python. Jupyter notebooks seem like a good idea in theory, in practice I'm having some difficulty setting up the python modules within a Jupyter environment.
For example, I found this neat geographical data article and I want to follow along. There is a very nice github project that I downloaded to follow along.
Start up Jupyter and everything looks in order until I run the first code block:
import pandas as pd
import re
from datetime import datetime
from dateutil.parser import parse
import missingno as msno
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt
import json
This immediately generates an error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-422e89229f53> in <module>
----> 1 import pandas as pd
2 import re
3 from datetime import datetime
4 from dateutil.parser import parse
5 import missingno as msno
ModuleNotFoundError: No module named 'pandas'
There are quite a few modules and I guess I can go into the command line to add them each individually. That doesn't seem efficient. Is there some other 'Jupyter' or 'Conda' way to do this?

Within Jupyter notebooks you can execute command line methods using ! at the start of the line e.g.
! conda install pandas --yes
More detail on the inner working is discussed in this question:
Running interactive command line code from Jupyter notebook
Thanks to #PaulH for pointing out the need to add the --yes flag.

just run this in Jupyter:
!pip install <the name of the module>, <the name of the module>, <the name of the module>...
this will install the module you choose.
For example :
!pip install pandas, re, ...

Related

Facing ImportError: cannot import name 'ModelParameters' (Python)

I am using the module ydata_synthetic (github link) for generating synthetic timeseries datasets. After installing the package, I ran the following commands:
from os import path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from ydata_synthetic.synthesizers import ModelParameters
from ydata_synthetic.preprocessing.timeseries import processed_stock
from ydata_synthetic.synthesizers.timeseries import TimeGAN
I am receiving the following error:
ImportError Traceback (most recent call last)
<ipython-input-11-9f2f25e511c0> in <module>
4 import matplotlib.pyplot as plt
5
----> 6 from ydata_synthetic.synthesizers import ModelParameters
7 from ydata_synthetic.preprocessing.timeseries import processed_stock
8 from ydata_synthetic.synthesizers.timeseries import TimeGAN
ImportError: cannot import name 'ModelParameters'
How can I solve this error?
Please make sure, that the virtual environment, where you installed the package is the same one where you try to execute your code. In case of venv or anaconda, you need to explicitly activate it in the console/terminal, then use pip to install your package and then also use that activated environment to execute your python script. If you are using an IDE, you need to set your IDE python interpreter to that specific new environment you created and installed the packe in.
Unexpected solution I found:
Use Python 3.7 (not 3.6, or 3.8+).
It seems like much of the code requires features that were new to 3.7 (eg namedtuple with default values), but the codebase has not yet been adjusted to run with python 3.8 or later.

No module named 'netCDF4'

I am currently working with netCDF data in the Spyder IDE, however when I try to import 'Dataset' from the module
from netCDF4 import Dataset
it is giving me the following error: "No module named 'netCDF4'". I already installed the module but I guess it is not in the same environment where I installed spyder. How can I fix this? If it helps solving my question. When I try to install netCDF4 in the spyder console using 'pip install netCDF4' it gives the following error:
"/Applications/Spyder.app/Contents/MacOS/python: No module named pip
Note: you may need to restart the kernel to use updated packages."
I looked up the path of the module: '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/netCDF4'
and I guess the path for spyder is:
'/Applications/Spyder.app/Contents/MacOS/python'
How can I make sure that they are both located in the same environment?
I tried to look up the issue, however nothing really solved my problem.
Would be glad if anyone could help me out.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from netCDF4 import Dataset
from cftime import numb2date
import tifffile as tifffile
# define arrays
XNR=[] # number
Xdt=[] # time
YGP=[] # GPP
YPP=[] # PPFD
# read csv file and extract column 0 (timestemp), PPFD (column 54) and GPP (column 312)
# take a file from FLUXNET for validation of the GPP model
with open(r'/Users/janoschbeer/Documents/Studium/ESS/ES_Observ/Assignments/Lab6/FLX_SJ-Adv_FLUXNET2015_SUBSET_2011-2014_1-4/FLX_SJ-Adv_FLUXNET2015_SUBSET_DD_2011-2014_1-4.csv') as csv:
lines = csv.readlines()
for i in range(2, len(lines)):
tmp=lines[i].split(",")
XNR.append(float(i))
Xdt.append(float(tmp[0]))
YGP.append(float(tmp[312]))
YPP.append(float(tmp[54]))

How do I fix: ImportError: cannot import name 'PandaRequest'?

This is my first time using a jupyter notebook.
I was trying to import the panda module:
import panda as pd
but I get the following error:
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/panda/__init__.py in <module>()
----> 1 from request import PandaRequest
ImportError: cannot import name 'PandaRequest'
How to fix this?
Edit: I wanted to use pandas but typed panda instead. :'D
You have installed and imported the panda module, which is the a Python implementation of the Panda REST interface and which has incomplete dependencies.
What you probably want to do is install pandas (with the final s) and import that.
I got the same problem and come up with the following solutions:
import pandas as pd
instead of
import panda as pd
It is called pandas.
Execute pip install panda. You don't have the Panda module.
Edit: My mistake, it looks like there's an error within the Panda module itself. Does it rely on PandaRequest?

"ImportError: cannot import name" with fresh Anaconda install

I am receiving the following error in an iPython notebook:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-06ac68ebf148> in <module>()
5 import pandas as pd
6 import numpy as np
----> 7 from sklearn import neighbours
8
9 start_time = time.time()
ImportError: cannot import name neighbours
Whilst trying to run the following:
# Import required packages
import time
import pandas as pd
import numpy as np
from sklearn import neighbours
I get the same issue on the command line. My suspicions:
I believe my problem has something to do with my system path or environment variables. I've given this various attempts and have followed other answers (e.g. Python ImportError- what is wrong here?) but in vain thus far. My current system Path variable includes the location of my python script; the environment PATH variable includes the location of the Anaconda libraries: "...\Anaconda2\Library\bin;". Does this appear correct?
Many solutions refer to circular dependencies, which I do not believe I have in the one module.
Other solutions are to do with the name of the script. Mine is named 'my_solution', so I do not believe this is an issue.
My sklearn folder can be found in \AppData\Local\Continuum\Anaconda2\Lib\site-packages\sklearn
I have recently uninstalled a standalone Python application. I have also reinstalled Anadonda and the scikit-learn, numpy and scipy packages multiple times.
I am using Python 2.7.11 through Anaconda 2.5.0 on Windows 7.
You want the American spelling of neighbours, i.e.
from sklearn import neighbors

ImportError: cannot import name DataFrame

I am using Pandas for the first time and trying to import Pandas, DataFrame and pandas.io.data as such
import pandas as pd
from pandas import DataFrame
import pandas.io.data
I get the error
"ImportError: cannot import name DataFrame"
and
"ImportError: No module named io.data"
I have tried the following to find the source of error on Ubuntu 14.04:
$pip show pandas
-> Version: 0.14.0
Location: /home/new-pc/anaconda/lib/python2.7/site-packages
$$PATH
-> bash: /home/new-pc/anaconda/bin
Not sure where else to look for errors. Thanks for all the inputs.
You must be mixing Python installations, with several python programs/distributions (Anaconda), and maybe python version (2 and 3)
The following commands should not produce any error:
sudo apt-get install python
sudo pip install pandas
python -c'import pandas as pd; from pandas import DataFrame; import pandas.io.data'
It will happen if you call your file pandas.py and try to run it. You should rename the file then.
It will also occur if you save a pandas.py file in the Python directory. Check if you have any files called pandas.py there and rename/move those files to another location.
Try:
from pandas_datareader import data
as shown in the example here
Check if you have saved any file with same names as in pandas libraries, such as saving file name as DataFrame.py. Always avoid naming the program as same as library name check all the location.Once you remove/rename your python script will run without any issue.

Categories

Resources