Get dependency file to host Jupyter notebook on Binder? - python

I am trying to export my dependencies to a requirements.txt or environment.yml file for pip or conda respectively - in order to host a Binder notebook.
How can I get only the dependencies that my notebook is using?
When I tried pip freeze or conda env export, I get all my installed packages, which leads to errors when building the Docker environment.

You might want to try watermark:
Install:
pip install watermark
Then, in your notebook:
# Load the extension
%load_ext watermark
# import your libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Determine the dependency version
%watermark -p numpy,matplotlib,seaborn
The results will look like this:
numpy 1.14.3
matplotlib 2.2.2
seaborn 0.9.0

Related

can't import seaborn on Jupyter notebook

I have installed numpy, pandas and matplot lib
but installation of seaorn i not possible. I ve tried updating Numpy, installing seaborn through the cmd command but in vain. I restarted the kernel each time.
import seaborn as sns
I keep recieving ;
ImportError: DLL load failed while importing _arpack: The specified procedure could not be found.
I have used:
!pip install seaborn
pip install numpy --upgrade --user
pip import seaborn as sns df = sns.load_dataset(penguins) sns.pairplot(df, hue=species)
It seems like your seaborn and/or numpy installation is broken or at least there is some versions conflict.
Try to run these commande from the command line :
pip unistall seaborn numpy
pip install mkl numpy seaborn
After that, you can run this code in your Jupyter notebook :
import seaborn as sns
df = sns.load_dataset("penguins")
sns.pairplot(df, hue="species")
the problem was fixed by updating matplotlib on the conda shell.
conda update matplotlib

Networkx & Jupyter Notebook using pip AttributeError

Hi everyone, I'm trying to create a network visualization with python using the networkx package. Unfortunately I'm getting a "AttributeError: module 'scipy.sparse' has no attribute 'coo_array'" at the end of my code. I've tried to fix it but every help in the internet points towards upgrading pip or conda which I've done and still get the same error. I'm using jupyter notebooks - any help would be greatly appreciated!
#Prerequisites
import sys
!{sys.executable} -m pip install --user networkx
!{sys.executable} -m pip install --user numpy
!{sys.executable} -m pip install --user pandas
pip install notebook --upgrade
conda upgrade notebook
import networkx as nx
import numpy as np
import pandas as pd
from pathlib import Path
from pandas import DataFrame
#Read in Source File - NB this must match the schema requirements
df_InputData = pd.read_excel("/Users/paulkruse/Desktop/FR/Fr1.xlsx")
Src_Column = 'Source ID'
Tgt_Column = 'Target ID'
print(df_InputData);
#Nodes are positioned using the Fruchterman-Reingold force-directed algorithm.
Q = nx.Graph()
arr_SrcTgt= np.array(df_InputData[[Src_Column, Tgt_Column]])
print(arr_SrcTgt);
Q.add_edges_from(arr_SrcTgt)
dict_Coords = nx.spring_layout(Q)
It seems that your scipy version is out of date. The most recent version 1.8.1 certainly has coo_array function. One way to resolve this should be to upgrade scipy. If you're doing this from inside jupyter notebook, then use
!pip install --user scipy==1.8.1
Make sure to restart kernel after installation.

Why does installing Tensorflow with conda ruin matplotlib?

Starting from a new anaconda environment, it looks like this (summarized, not a full paste):
[myenv]$ python
Python 3.8.8 ...
>> import matplotlib.pyplot as plt
>>
[myenv]$ conda install tensorflow-gpu
...
[myenv]$ python
>> import tensorflow
>> tensorflow.__version__
'2.4.1'
>> import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
>>
[myenv]$ conda install matplotlib
...
[myenv]% python
>> import tensorflow
>> import matplotlib.pyplot as plt
<tons of traceback>
RunTimeError: the sip module implements API v12.0 to v12.4 but the PyQt5.core module requires API v12.5
How can I have tensorflow and matplotlib at the same time?
Do you have the complete traceback? Based on the Error you posted, it seems like its a dependency issue on the sip module. I'll recommend using you own virtualenv instead of anacondas.
FOR CREATING YOUR OWN VIRTUALENV:
open terminal
go to the directory you want your project to be
pip install virtualenv
virtualenv venv
source venv/bin/activate (for linux or mac)
pip install tensorflow
pip install matplotlib
pip install <whatever library you want>

Jupyter lab installing/importing packages

I am trying to install plotnine for a notebook I am using. I have done the following:
Created a conda environment using python 3.6, and adding plotnine
Launching jupyter lab with the above environment activated
In the notebook, I added the following line:
!conda install -c conda-forge --yes plotnine
However, my output makes no sense. First it says that all requested packages have been installed, and then it says it cannot find the module
!conda install -c conda-forge --yes plotnine
from plotnine import *
Solving environment: done
# All requested packages already installed.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-386ef81e08ff> in <module>()
11 get_ipython().system('conda install -c conda-forge --yes plotnine')
12 ######
---> 13 from plotnine import * # python clone of ggplot2
14 matplotlib.rcParams['figure.figsize'] = [12, 8]
15 matplotlib.rcParams['lines.linewidth'] = 2
ImportError: No module named 'plotnine'
In case there is a known conflict, here is the entire import statement:
import gsc # proprietary module
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from ipywidgets import interact, FloatSlider
from util_demo import *
# adding installation of plotnine, which is not included by default
# import sys
!conda install -c conda-forge --yes plotnine
######
from plotnine import * # python clone of ggplot2
matplotlib.rcParams['figure.figsize'] = [12, 8]
matplotlib.rcParams['lines.linewidth'] = 2
matplotlib.rcParams['xtick.labelsize'] = 24
matplotlib.rcParams['ytick.labelsize'] = 24
matplotlib.rcParams['legend.fontsize'] = 24
matplotlib.rcParams['axes.labelsize'] = 24
EDIT: I also checked sys.path within the jupyter notebook and get the following. I do not see anything about conda here. Should I update either PATH or PYTHONPATH?
['',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/extensions',
'/Users/adamg/.ipython']
I had the same problem. It looks like for me, my notebook in Jupyter Lab was running the base kernel and not the kernel of the virtual environment. Type
import sys
sys.executable
into your notebook. For me, I got the result
'/anaconda3/bin/python'
instead of the desired
'/anaconda3/envs/myenv/bin/python'
I solved it by following the instructions in the iPython documentation. In summary, you need to install a new iPython kernel for your new environment. Run:
conda install -n myenv ipython
conda activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
Then, to run Jupyter Lab in the new environment:
conda activate myenv
jupyter lab
And you should be able to select the kernel "Python (myenv)" when you open a new notebook (also in the top right of an existing notebook).
#try pip install first and rerun; the code line below should solve it:
pip install plotnine

Error loading IPython notebook

Once I opened a notebook file with Jupyter (it asks me to convert the file) I never can open it in the standard IPython notebook anymore. I get the following error:
Error loading notebook
Bad Request
2014-12-21 04:13:03.203 [NotebookApp] WARNING | Unreadable Notebook: /FunIT experiment.ipynb global name 'NBFormatError' is not defined
WARNING:tornado.access:400 GET /api/notebooks/FunIT%20experiment.ipynb?_=1419153182928 (::1) 3.11ms referer=http://localhost:8888/notebooks/FunIT%20experiment.ipynb
An example of such corrupted files is this one:
https://www.dropbox.com/s/7io99wpoivxmc8f/FunIT%20experiment.ipynb?dl=0
How can I revert this process? I need to open these files with the standard IPython notebook (v2.2.0).
This problem has to do with incompatibility of the notebook and your IPython version. In my current version of IPython:
ipython --version
2.3.1
When I try to open the file (FunIT\ experiment.ipynb):
ipython notebook FunIT\ experiment.ipynb
I get the following error message
Error loading notebook
Unreadable Notebook: FunIT experiment.ipynb Unsupported nbformat
version 4
The error message indicates that the notebook format is not supported. Let's install the development version, https://github.com/ipython/ipython. I used virtual Environment, http://docs.python-guide.org/en/latest/dev/virtualenvs/, but it's not necessary.
Install virtual environment
pip install virtualenv
mkdir test
cd test
virtualenv venv
source venv/bin/activate
Ipython Notebook development installation
git clone --recursive https://github.com/ipython/ipython.git
cd ipython
pip install -e ".[notebook]" --user
Now I have the current development version.
ipython --version
3.0.0-dev
And I can open the file with ipython notebook
ipython notebook FunIT\ experiment.ipynb
Here is a snippet of the code:
import pandas as pd
import numpy as np
from pandas.tools.pivot import pivot_table
#from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
%pylab inline
#from sklearn.neighbors.kde import KernelDensity
import seaborn as sns
import scipy.stats as st
sns.set()
Upgrading IPython fixed it for me:
pip install ipython --upgrade
This works for me perfectly:
pip install jupyter

Categories

Resources